Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ Style/ExplicitBlockArgument:
Style/GuardClause:
Exclude:
- lib/tiny_admin/router.rb

Style/StringLiterals:
Enabled: true
EnforcedStyle: double_quotes
32 changes: 16 additions & 16 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
# frozen_string_literal: true

source 'https://rubygems.org'
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

gemspec

group :development, :test do
gem 'rails', '~> 7.1'
gem "rails", "~> 7.1"

gem 'sqlite3', '< 2.0'
gem 'tilt'
gem 'warden'
gem 'webrick'
gem "sqlite3", "< 2.0"
gem "tilt"
gem "warden"
gem "webrick"

gem 'rbs'
gem "rbs"

# Testing
gem 'capybara'
gem 'capybara-screenshot'
gem 'rspec-rails'
gem 'simplecov', require: false
gem "capybara"
gem "capybara-screenshot"
gem "rspec-rails"
gem "simplecov", require: false

# Linters
# gem 'fasterer'
gem 'rubocop'
gem 'rubocop-packaging'
gem 'rubocop-performance'
gem 'rubocop-rspec'
gem "rubocop"
gem "rubocop-packaging"
gem "rubocop-performance"
gem "rubocop-rspec"

# Tools
# gem 'overcommit', '~> 0.59'
gem 'pry-rails'
gem "pry-rails"
end
16 changes: 8 additions & 8 deletions lib/tiny_admin.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# frozen_string_literal: true

require 'phlex'
require 'roda'
require 'zeitwerk'
require "phlex"
require "roda"
require "zeitwerk"

require 'forwardable'
require 'singleton'
require 'yaml'
require "forwardable"
require "singleton"
require "yaml"

loader = Zeitwerk::Loader.for_gem
loader.setup
Expand All @@ -25,10 +25,10 @@ def configure_from_file(file)
end

def route_for(section, reference: nil, action: nil, query: nil)
root_path = settings.root_path == '/' ? nil : settings.root_path
root_path = settings.root_path == "/" ? nil : settings.root_path
route = [root_path, section, reference, action].compact.join("/")
route << "?#{query}" if query
route[0] == '/' ? route : route.prepend('/')
route[0] == "/" ? route : route.prepend("/")
end

def settings
Expand Down
6 changes: 3 additions & 3 deletions lib/tiny_admin/actions/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ def evaluate_options(options)
@params = context.request.params
@repository = context.repository
@pagination = options[:pagination] || 10
@current_page = (params['p'] || 1).to_i
@query_string = params_to_s(params.except('p'))
@current_page = (params["p"] || 1).to_i
@query_string = params_to_s(params.except("p"))
end

def prepare_filters(fields)
filters = (options[:filters] || []).map { _1.is_a?(Hash) ? _1 : { field: _1 } }
filters = filters.to_h { |filter| [filter[:field], filter] }
values = params['q'] || {}
values = params["q"] || {}
fields.each_with_object({}) do |(name, field), result|
result[field] = { value: values[name], filter: filters[name] } if filters.key?(name)
end
Expand Down
16 changes: 8 additions & 8 deletions lib/tiny_admin/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
module TinyAdmin
class Authentication < BasicApp
route do |r|
r.get 'unauthenticated' do
r.get "unauthenticated" do
if current_user
r.redirect TinyAdmin.settings.root_path
else
render_login
end
end

r.post 'unauthenticated' do
r.post "unauthenticated" do
warning = TinyAdmin.settings.helper_class.label_for(
'Failed to authenticate',
options: ['authentication.unauthenticated']
"Failed to authenticate",
options: ["authentication.unauthenticated"]
)
render_login(warnings: [warning])
end

r.get 'logout' do
r.get "logout" do
logout_user
r.redirect TinyAdmin.settings.root_path
end
Expand All @@ -33,9 +33,9 @@ def render_login(notices: nil, warnings: nil, errors: nil)

page = prepare_page(login, options: %i[no_menu compact_layout])
page.messages = {
notices: notices || flash['notices'],
warnings: warnings || flash['warnings'],
errors: errors || flash['errors']
notices: notices || flash["notices"],
warnings: warnings || flash["warnings"],
errors: errors || flash["errors"]
}
render(inline: page.call)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tiny_admin/basic_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def authentication_plugin

plugin :flash
plugin :not_found
plugin :render, engine: 'html'
plugin :render, engine: "html"
plugin :sessions, secret: SecureRandom.hex(64)

plugin authentication_plugin, TinyAdmin.settings.authentication
Expand Down
6 changes: 3 additions & 3 deletions lib/tiny_admin/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ def initialize(name:, title:, type:, options: {})
end

def apply_call_option(target)
messages = (options[:call] || '').split(',').map(&:strip)
messages = (options[:call] || "").split(",").map(&:strip)
messages.inject(target) { |result, msg| result&.send(msg) } if messages.any?
end

def translate_value(value)
if options && options[:method]
method, *args = options[:method].split(',').map(&:strip)
method, *args = options[:method].split(",").map(&:strip)
if options[:converter]
Object.const_get(options[:converter]).send(method, value, options: args || [])
else
Expand All @@ -32,7 +32,7 @@ def translate_value(value)
class << self
def create_field(name:, title: nil, type: nil, options: {})
field_name = name.to_s
field_title = field_name.respond_to?(:humanize) ? field_name.humanize : field_name.tr('_', ' ').capitalize
field_title = field_name.respond_to?(:humanize) ? field_name.humanize : field_name.tr("_", " ").capitalize
new(name: field_name, title: title || field_title, type: type || :string, options: options || {})
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tiny_admin/plugins/active_record_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def list(page: 1, limit: 10, sort: nil, filters: nil)
def apply_filters(query, filters)
filters.each do |field, filter|
value = filter&.dig(:value)
next if value.nil? || value == ''
next if value.nil? || value == ""

query =
case field.type
Expand Down
2 changes: 1 addition & 1 deletion lib/tiny_admin/plugins/no_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def authenticate_user! # rubocop:disable Naming/PredicateMethod
end

def current_user
'admin'
"admin"
end

def logout_user
Expand Down
14 changes: 7 additions & 7 deletions lib/tiny_admin/plugins/simple_auth.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# frozen_string_literal: true

require 'digest'
require "digest"

module TinyAdmin
module Plugins
module SimpleAuth
class << self
def configure(app, opts = {})
@@opts = opts || {} # rubocop:disable Style/ClassVars
@@opts[:password] ||= ENV.fetch('ADMIN_PASSWORD_HASH', nil) # NOTE: fallback value
@@opts[:password] ||= ENV.fetch("ADMIN_PASSWORD_HASH", nil) # NOTE: fallback value

Warden::Strategies.add(:secret) do
def authenticate!
secret = params['secret'] || ''
secret = params["secret"] || ""
return fail(:invalid_credentials) if Digest::SHA512.hexdigest(secret) != @@opts[:password]

success!(app: 'TinyAdmin')
success!(app: "TinyAdmin")
end
end

Expand All @@ -29,15 +29,15 @@ def authenticate!

module InstanceMethods
def authenticate_user!
env['warden'].authenticate!
env["warden"].authenticate!
end

def current_user
env['warden'].user
env["warden"].user
end

def logout_user
env['warden'].logout
env["warden"].logout
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions lib/tiny_admin/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Router < BasicApp
route do |r|
TinyAdmin.settings.load_settings

r.on 'auth' do
r.on "auth" do
r.run Authentication
end

Expand All @@ -25,7 +25,7 @@ class Router < BasicApp
# :nocov:
end

r.post '' do
r.post "" do
r.redirect TinyAdmin.settings.root_path
end

Expand All @@ -48,7 +48,7 @@ def store

def render_page(page)
if page.respond_to?(:messages=)
page.messages = { notices: flash['notices'], warnings: flash['warnings'], errors: flash['errors'] }
page.messages = { notices: flash["notices"], warnings: flash["warnings"], errors: flash["errors"] }
end
render(inline: page.call)
end
Expand Down Expand Up @@ -99,7 +99,7 @@ def setup_collection_routes(req, slug, options:)
)

# Index
if options[:only].include?(:index) || options[:only].include?('index')
if options[:only].include?(:index) || options[:only].include?("index")
req.is do
if authorization.allowed?(current_user, :resource_index, slug)
context = Context.new(
Expand Down Expand Up @@ -134,7 +134,7 @@ def setup_member_routes(req, slug, options:)
)

# Show
if options[:only].include?(:show) || options[:only].include?('show')
if options[:only].include?(:show) || options[:only].include?("show")
req.is do
if authorization.allowed?(current_user, :resource_show, slug)
context = Context.new(
Expand Down
8 changes: 4 additions & 4 deletions lib/tiny_admin/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class Settings
%i[page_not_found] => Views::Pages::PageNotFound,
%i[record_not_found] => Views::Pages::RecordNotFound,
%i[repository] => Plugins::ActiveRecordRepository,
%i[root_path] => '/admin',
%i[root_path] => "/admin",
%i[root page] => Views::Pages::Root,
%i[root title] => 'TinyAdmin',
%i[root title] => "TinyAdmin",
%i[sections] => []
}.freeze

Expand Down Expand Up @@ -78,11 +78,11 @@ def load_settings
end

@store ||= TinyAdmin::Store.new(self)
self.root_path = '/' if root_path == ''
self.root_path = "/" if root_path == ""

if authentication[:plugin] <= Plugins::SimpleAuth
logout_path = "#{root_path}/auth/logout"
authentication[:logout] ||= TinyAdmin::Section.new(name: 'logout', slug: 'logout', path: logout_path)
authentication[:logout] ||= TinyAdmin::Section.new(name: "logout", slug: "logout", path: logout_path)
end
store.prepare_sections(sections, logout: authentication[:logout])
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tiny_admin/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def add_resource_section(slug, section)
repository: to_class(section[:repository] || settings.repository)
)

hidden = section[:options] && (section[:options].include?(:hidden) || section[:options].include?('hidden'))
hidden = section[:options] && (section[:options].include?(:hidden) || section[:options].include?("hidden"))
TinyAdmin::Section.new(name: section[:name], slug: slug) unless hidden
end

Expand Down
2 changes: 1 addition & 1 deletion lib/tiny_admin/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def round(value, options: [])
end

def strftime(value, options: [])
value&.strftime(options&.first || '%Y-%m-%d %H:%M')
value&.strftime(options&.first || "%Y-%m-%d %H:%M")
end

def to_date(value, options: [])
Expand Down
6 changes: 3 additions & 3 deletions lib/tiny_admin/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def params_to_s(params)
result.push(["#{param}=#{value}"])
end
end
list.join('&')
list.join("&")
end

def prepare_page(page_class, slug: nil, attributes: nil, options: nil, params: nil)
Expand Down Expand Up @@ -40,9 +40,9 @@ def to_class(klass)
end

def humanize(string)
return '' unless string
return "" unless string

string.respond_to?(:humanize) ? string.humanize : string.tr('_', ' ').capitalize
string.respond_to?(:humanize) ? string.humanize : string.tr("_", " ").capitalize
end
end
end
2 changes: 1 addition & 1 deletion lib/tiny_admin/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module TinyAdmin
VERSION = '0.10.1'
VERSION = "0.10.1"
end
Loading
Loading