Skip to content

Commit 6beee64

Browse files
authored
Merge pull request #63 from blocknotes/misc-improvements
Misc improvements
2 parents 6accf55 + c720254 commit 6beee64

File tree

17 files changed

+100
-33
lines changed

17 files changed

+100
-33
lines changed

lib/tiny_admin/plugins/simple_auth.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ module Plugins
77
module SimpleAuth
88
class << self
99
def configure(app, opts = {})
10-
@@opts = opts || {} # rubocop:disable Style/ClassVars
11-
@@opts[:password] ||= ENV.fetch("ADMIN_PASSWORD_HASH", nil) # NOTE: fallback value
10+
opts ||= {}
11+
password_hash = opts[:password] || ENV.fetch("ADMIN_PASSWORD_HASH", nil)
1212

1313
Warden::Strategies.add(:secret) do
14-
def authenticate!
14+
define_method(:authenticate!) do
1515
secret = params["secret"] || ""
16-
return fail(:invalid_credentials) if Digest::SHA512.hexdigest(secret) != @@opts[:password]
16+
return fail(:invalid_credentials) if Digest::SHA512.hexdigest(secret) != password_hash
1717

1818
success!(app: "TinyAdmin")
1919
end

lib/tiny_admin/router.rb

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ def setup_page_route(req, slug, page_data)
7676

7777
def setup_resource_routes(req, slug, options:)
7878
req.on slug do
79-
setup_collection_routes(req, slug, options: options)
80-
setup_member_routes(req, slug, options: options)
79+
repository = options[:repository].new(options[:model])
80+
setup_collection_routes(req, slug, options: options, repository: repository)
81+
setup_member_routes(req, slug, options: options, repository: repository)
8182
end
8283
end
8384

84-
def setup_collection_routes(req, slug, options:)
85-
repository = options[:repository].new(options[:model])
85+
def setup_collection_routes(req, slug, options:, repository:)
8686
action_options = options[:index] || {}
8787

8888
# Custom actions
@@ -112,8 +112,7 @@ def setup_collection_routes(req, slug, options:)
112112
end
113113
end
114114

115-
def setup_member_routes(req, slug, options:)
116-
repository = options[:repository].new(options[:model])
115+
def setup_member_routes(req, slug, options:, repository:)
117116
action_options = (options[:show] || {}).merge(record_not_found_page: TinyAdmin.settings.record_not_found)
118117

119118
req.on String do |reference|
@@ -149,10 +148,10 @@ def setup_member_routes(req, slug, options:)
149148

150149
def setup_custom_actions(req, custom_actions = nil, options:, repository:, slug:, reference: nil)
151150
(custom_actions || []).each_with_object({}) do |custom_action, result|
152-
action_slug, action = custom_action.first
153-
action_class = to_class(action)
151+
action_slug, action_config = custom_action.first
152+
action_class, http_method = parse_action_config(action_config)
154153

155-
req.get action_slug.to_s do
154+
req.public_send(http_method, action_slug.to_s) do
156155
authorize!(:custom_action, action_slug.to_s) do
157156
context = Context.new(
158157
actions: {},
@@ -171,6 +170,16 @@ def setup_custom_actions(req, custom_actions = nil, options:, repository:, slug:
171170
end
172171
end
173172

173+
def parse_action_config(config)
174+
if config.is_a?(Hash)
175+
action_class = to_class(config[:action] || config["action"])
176+
http_method = (config[:method] || config["method"] || "get").to_sym
177+
[action_class, http_method]
178+
else
179+
[to_class(config), :get]
180+
end
181+
end
182+
174183
def authorization
175184
TinyAdmin.settings.authorization_class
176185
end

lib/tiny_admin/settings.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,18 @@ def convert_value(key, value)
109109
value.each_key do |key2|
110110
path = [key, key2]
111111
if (DEFAULTS[path].is_a?(Class) || DEFAULTS[path].is_a?(Module)) && self[key][key2].is_a?(String)
112-
self[key][key2] = Object.const_get(self[key][key2])
112+
self[key][key2] = resolve_class(self[key][key2], setting: "#{key}.#{key2}")
113113
end
114114
end
115115
elsif value.is_a?(String) && (DEFAULTS[[key]].is_a?(Class) || DEFAULTS[[key]].is_a?(Module))
116-
self[key] = Object.const_get(self[key])
116+
self[key] = resolve_class(self[key], setting: key.to_s)
117117
end
118118
end
119+
120+
def resolve_class(class_name, setting:)
121+
Object.const_get(class_name)
122+
rescue NameError => e
123+
raise NameError, "TinyAdmin: invalid class '#{class_name}' for setting '#{setting}' - #{e.message}"
124+
end
119125
end
120126
end

lib/tiny_admin/store.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def prepare_sections(sections, logout:)
3030
end
3131
list << item if item
3232
end
33-
navbar << logout if logout
33+
@navbar << logout if logout
3434
end
3535

3636
private

lib/tiny_admin/views/actions/index.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ def view_template
4848
end
4949
}
5050

51-
render TinyAdmin::Views::Components::Widgets.new(widgets)
51+
context = { slug: slug, records: records, params: params }
52+
render TinyAdmin::Views::Components::Widgets.new(widgets, context: context)
5253
}
5354
end
5455
end

lib/tiny_admin/views/actions/show.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ def view_template
3535
}
3636
end
3737

38-
render TinyAdmin::Views::Components::Widgets.new(widgets)
38+
context = { slug: slug, record: record, reference: reference, params: params }
39+
render TinyAdmin::Views::Components::Widgets.new(widgets, context: context)
3940
}
4041
end
4142
end

lib/tiny_admin/views/attributes.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ module Views
55
module Attributes
66
def update_attributes(attributes)
77
attributes.each do |key, value|
8-
send("#{key}=", value)
8+
setter = "#{key}="
9+
unless respond_to?(setter)
10+
raise ArgumentError, "#{self.class.name} does not support attribute '#{key}'"
11+
end
12+
13+
send(setter, value)
914
end
1015
end
1116
end

lib/tiny_admin/views/components/field_value.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ def initialize(field, value, record:)
1414

1515
def view_template
1616
translated_value = field.translate_value(value)
17+
display_value = field.apply_call_option(record) || translated_value
1718
value_class = field.options[:options]&.include?("value_class") ? "value-#{value}" : nil
1819
if field.options[:link_to]
1920
a(href: TinyAdmin.route_for(field.options[:link_to], reference: translated_value)) {
2021
span(class: value_class) {
21-
render_value(field.apply_call_option(record) || translated_value)
22+
render_value(display_value)
2223
}
2324
}
2425
else
2526
span(class: value_class) {
26-
render_value(translated_value)
27+
render_value(display_value)
2728
}
2829
end
2930
end

lib/tiny_admin/views/components/pagination.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ def view_template
1818
if pages <= 10
1919
pages_range(1..pages)
2020
elsif current <= 4 || current >= pages - 3
21-
pages_range(1..(current <= 4 ? current + 2 : 4), with_dots: true)
22-
pages_range((current > pages - 4 ? current - 2 : pages - 2)..pages)
21+
pages_range(1..(current <= 4 ? (current + 2) : 4), with_dots: true)
22+
pages_range((current > pages - 4 ? (current - 2) : (pages - 2))..pages)
2323
else
2424
pages_range(1..1, with_dots: true)
2525
pages_range((current - 2)..(current + 2), with_dots: true)
@@ -39,7 +39,7 @@ def pages_range(range, with_dots: false)
3939
range.each do |page|
4040
li(class: page == current ? "page-item active" : "page-item") {
4141
href = query_string.empty? ? "?p=#{page}" : "?#{query_string}&p=#{page}"
42-
a(class: "page-link", href: href) { page }
42+
a(class: "page-link", href: href) { page.to_s }
4343
}
4444
end
4545
dots if with_dots

lib/tiny_admin/views/components/widgets.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ module TinyAdmin
44
module Views
55
module Components
66
class Widgets < BasicComponent
7-
def initialize(widgets)
7+
def initialize(widgets, context: {})
88
@widgets = widgets
9+
@context = context
910
end
1011

1112
def view_template
@@ -20,7 +21,7 @@ def view_template
2021
div(class: "col") {
2122
div(class: "card") {
2223
div(class: "card-body") {
23-
render widget.new
24+
render build_widget(widget)
2425
}
2526
}
2627
}
@@ -29,6 +30,20 @@ def view_template
2930
end
3031
}
3132
end
33+
34+
private
35+
36+
def build_widget(widget)
37+
key_params = [:key, :keyreq]
38+
if widget.instance_method(:initialize).arity != 0 ||
39+
widget.instance_method(:initialize).parameters.any? { |type, _| key_params.include?(type) }
40+
widget.new(context: @context)
41+
else
42+
widget.new
43+
end
44+
rescue ArgumentError
45+
widget.new
46+
end
3247
end
3348
end
3449
end

0 commit comments

Comments
 (0)