Skip to content

Commit 909a981

Browse files
authored
Merge pull request #19 from blocknotes/feat/internal-improvements-2
feat: internal improvements 2
2 parents a0c1add + ec1442e commit 909a981

File tree

21 files changed

+154
-130
lines changed

21 files changed

+154
-130
lines changed

lib/tiny_admin/actions/index.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Index < BasicAction
1313
:repository,
1414
:sort
1515

16-
def call(app:, context:, options:, actions:)
16+
def call(app:, context:, options:)
1717
evaluate_options(options)
1818
fields = repository.fields(options: fields_options)
1919
filters = prepare_filters(fields, filters_list)
@@ -22,7 +22,7 @@ def call(app:, context:, options:, actions:)
2222
prepare_page(Views::Actions::Index) do |page|
2323
setup_pagination(page, settings.components[:pagination], total_count: total_count)
2424
page.update_attributes(
25-
actions: actions,
25+
actions: context.actions,
2626
fields: fields,
2727
filters: filters,
2828
prepare_record: ->(record) { repository.index_record_attrs(record, fields: fields_options) },
@@ -40,7 +40,7 @@ def evaluate_options(options)
4040
@repository = context.repository
4141
@filters_list = options[:filters]
4242
@pagination = options[:pagination] || 10
43-
@sort = options[:sort] || ['id']
43+
@sort = options[:sort]
4444

4545
@current_page = (params['p'] || 1).to_i
4646
@query_string = params_to_s(params.except('p'))
@@ -60,7 +60,7 @@ def setup_pagination(page, pagination_component_class, total_count:)
6060
return if pages <= 1 || !pagination_component_class
6161

6262
page.pagination_component = pagination_component_class.new
63-
page.pagination_component.update(current: current_page, pages: pages, query_string: query_string)
63+
page.pagination_component.update_attributes(current: current_page, pages: pages, query_string: query_string)
6464
end
6565
end
6666
end

lib/tiny_admin/actions/show.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@
33
module TinyAdmin
44
module Actions
55
class Show < BasicAction
6-
attr_reader :repository
7-
8-
def call(app:, context:, options:, actions:)
9-
@repository = context.repository
6+
def call(app:, context:, options:)
107
fields_options = attribute_options(options[:attributes])
8+
repository = context.repository
119
record = repository.find(context.reference)
10+
prepare_record = ->(record_data) { repository.show_record_attrs(record_data, fields: fields_options) }
1211

1312
prepare_page(Views::Actions::Show) do |page|
1413
page.update_attributes(
15-
actions: actions,
14+
actions: context.actions,
1615
fields: repository.fields(options: fields_options),
17-
prepare_record: ->(record_data) { repository.show_record_attrs(record_data, fields: fields_options) },
16+
prepare_record: prepare_record,
1817
record: record,
1918
title: repository.show_title(record)
2019
)

lib/tiny_admin/context.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ module TinyAdmin
44
class Context
55
include Singleton
66

7-
attr_accessor :reference, :repository, :request, :router, :settings, :slug
7+
attr_accessor :actions,
8+
:navbar,
9+
:pages,
10+
:reference,
11+
:repository,
12+
:request,
13+
:resources,
14+
:router,
15+
:settings,
16+
:slug
817
end
918
end

lib/tiny_admin/field.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ class Field
77
def initialize(name:, title:, type:, options: {})
88
@type = type
99
@name = name
10-
@title = title || name
10+
@title = title
1111
@options = options
1212
end
1313

14+
def apply_call_option(target)
15+
messages = (options[:call] || '').split(',').map(&:strip)
16+
messages.inject(target) { |result, msg| result&.send(msg) } if messages.any?
17+
end
18+
1419
class << self
1520
def create_field(name:, title: nil, type: nil, options: {})
1621
field_name = name.to_s
17-
new(
18-
name: field_name,
19-
title: title || field_name.respond_to?(:humanize) ? field_name.humanize : field_name.tr('_', ' ').capitalize,
20-
type: type || :string,
21-
options: options
22-
)
22+
field_title = field_name.respond_to?(:humanize) ? field_name.humanize : field_name.tr('_', ' ').capitalize
23+
new(name: field_name, title: title || field_title, type: type || :string, options: options || {})
2324
end
2425
end
2526
end

lib/tiny_admin/plugins/active_record_repository.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ def find(reference)
4242
raise BaseRepository::RecordNotFound, e.message
4343
end
4444

45-
def list(page: 1, limit: 10, sort: ['id'], filters: nil)
46-
query = model.all.order(sort)
45+
def list(page: 1, limit: 10, sort: nil, filters: nil)
46+
query = sort ? model.all.order(sort) : model.all
4747
query = apply_filters(query, filters) if filters
4848
page_offset = page.positive? ? (page - 1) * limit : 0
4949
records = query.offset(page_offset).limit(limit).to_a

lib/tiny_admin/router.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ class Router < BasicApp
2929
r.redirect settings.root_path
3030
end
3131

32-
context.settings.pages.each do |slug, data|
32+
context.pages.each do |slug, data|
3333
setup_page_route(r, slug, data)
3434
end
3535

36-
context.settings.resources.each do |slug, options|
36+
context.resources.each do |slug, options|
3737
setup_resource_routes(r, slug, options: options || {})
3838
end
3939

@@ -88,12 +88,12 @@ def setup_collection_routes(router, options:)
8888
)
8989

9090
# Index
91-
actions = options[:only]
92-
if !actions || actions.include?(:index) || actions.include?('index')
91+
if options[:only].include?(:index) || options[:only].include?('index')
9392
router.is do
93+
context.actions = custom_actions
9494
context.request = request
9595
index_action = TinyAdmin::Actions::Index.new
96-
render_page index_action.call(app: self, context: context, options: action_options, actions: custom_actions)
96+
render_page index_action.call(app: self, context: context, options: action_options)
9797
end
9898
end
9999
end
@@ -114,12 +114,12 @@ def setup_member_routes(router, options:)
114114
)
115115

116116
# Show
117-
actions = options[:only]
118-
if !actions || actions.include?(:show) || actions.include?('show')
117+
if options[:only].include?(:show) || options[:only].include?('show')
119118
router.is do
119+
context.actions = custom_actions
120120
context.request = request
121121
show_action = TinyAdmin::Actions::Show.new
122-
render_page show_action.call(app: self, context: context, options: action_options, actions: custom_actions)
122+
render_page show_action.call(app: self, context: context, options: action_options)
123123
end
124124
end
125125
end
@@ -132,6 +132,7 @@ def setup_custom_actions(router, custom_actions, repository:, options:)
132132
action_class = action.is_a?(String) ? Object.const_get(action) : action
133133

134134
router.get action_slug.to_s do
135+
context.actions = {}
135136
context.request = request
136137
custom_action = action_class.new
137138
render_page custom_action.call(app: self, context: context, options: options)

lib/tiny_admin/settings.rb

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class Settings
2525
:components,
2626
:extra_styles,
2727
:helper_class,
28-
:navbar,
2928
:page_not_found,
3029
:record_not_found,
3130
:repository,
@@ -35,8 +34,6 @@ class Settings
3534
:scripts,
3635
:style_links
3736

38-
attr_reader :pages, :resources
39-
4037
def [](key)
4138
send(key)
4239
end
@@ -57,39 +54,15 @@ def load_settings
5754
end
5855
end
5956

60-
@pages ||= {}
61-
@resources ||= {}
57+
context.pages ||= {}
58+
context.resources ||= {}
6259
@sections ||= []
6360
@root_path = '/' if @root_path == ''
64-
if @authentication[:plugin] == Plugins::SimpleAuth
65-
@authentication[:logout] ||= ['logout', "#{root_path}/auth/logout"]
66-
end
67-
@navbar = prepare_navbar(sections, logout: authentication[:logout])
68-
end
6961

70-
def prepare_navbar(sections, logout:)
71-
items = sections.each_with_object({}) do |section, list|
72-
slug = section[:slug]
73-
case section[:type]&.to_sym
74-
when :url
75-
list[slug] = [section[:name], section[:url], section[:options]]
76-
when :page
77-
page = section[:page]
78-
pages[slug] = page.is_a?(String) ? Object.const_get(page) : page
79-
list[slug] = [section[:name], route_for(slug)]
80-
when :resource
81-
repository = section[:repository] || settings.repository
82-
resources[slug] = {
83-
model: section[:model].is_a?(String) ? Object.const_get(section[:model]) : section[:model],
84-
repository: repository.is_a?(String) ? Object.const_get(repository) : repository
85-
}
86-
resources[slug].merge! section.slice(:resource, :only, :index, :show, :collection_actions, :member_actions)
87-
hidden = section[:options] && (section[:options].include?(:hidden) || section[:options].include?('hidden'))
88-
list[slug] = [section[:name], route_for(slug)] unless hidden
89-
end
62+
if @authentication[:plugin] <= Plugins::SimpleAuth
63+
@authentication[:logout] ||= { name: 'logout', path: "#{root_path}/auth/logout" }
9064
end
91-
items['auth/logout'] = logout if logout
92-
items
65+
context.navbar = prepare_navbar(sections, logout: authentication[:logout])
9366
end
9467

9568
private
@@ -106,5 +79,44 @@ def convert_value(key, value)
10679
self[key] = Object.const_get(self[key])
10780
end
10881
end
82+
83+
def prepare_navbar(sections, logout:)
84+
items = sections.each_with_object({}) do |section, list|
85+
slug = section[:slug]
86+
case section[:type]&.to_sym
87+
when :url
88+
list[slug] = add_url_section(slug, section)
89+
when :page
90+
list[slug] = add_page_section(slug, section)
91+
when :resource
92+
list[slug] = add_resource_section(slug, section)
93+
end
94+
end
95+
items['auth/logout'] = logout if logout
96+
items
97+
end
98+
99+
def add_url_section(_slug, section)
100+
section.slice(:name, :options).tap { _1[:path] = section[:url] }
101+
end
102+
103+
def add_page_section(slug, section)
104+
page = section[:page]
105+
context.pages[slug] = page.is_a?(String) ? Object.const_get(page) : page
106+
{ name: section[:name], path: route_for(slug), class: context.pages[slug] }
107+
end
108+
109+
def add_resource_section(slug, section)
110+
repository = section[:repository] || settings.repository
111+
context.resources[slug] = {
112+
model: section[:model].is_a?(String) ? Object.const_get(section[:model]) : section[:model],
113+
repository: repository.is_a?(String) ? Object.const_get(repository) : repository
114+
}
115+
resource_options = section.slice(:resource, :only, :index, :show, :collection_actions, :member_actions)
116+
resource_options[:only] ||= %i[index show]
117+
context.resources[slug].merge!(resource_options)
118+
hidden = section[:options] && (section[:options].include?(:hidden) || section[:options].include?('hidden'))
119+
{ name: section[:name], path: route_for(slug) } unless hidden
120+
end
109121
end
110122
end

lib/tiny_admin/support.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@ module TinyAdmin
44
class Support
55
class << self
66
def call(value, options: [])
7-
value && options&.any? ? options.inject(value) { |result, message| result&.send(message) } : value
7+
options.inject(value) { |result, message| result&.send(message) } if value && options&.any?
88
end
99

1010
def downcase(value, options: [])
1111
value&.downcase
1212
end
1313

1414
def format(value, options: [])
15-
value && options&.any? ? Kernel.format(options.first, value) : value
15+
Kernel.format(options.first, value) if value && options&.any?
1616
end
1717

1818
def round(value, options: [])
1919
value&.round(options&.first&.to_i || 2)
2020
end
2121

2222
def strftime(value, options: [])
23-
value ? value.strftime(options&.first || '%Y-%m-%d %H:%M') : ''
23+
value&.strftime(options&.first || '%Y-%m-%d %H:%M')
2424
end
2525

2626
def to_date(value, options: [])
27-
value ? value.to_date.to_s : value
27+
value.to_date.to_s if value
2828
end
2929

3030
def upcase(value, options: [])

lib/tiny_admin/utils.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ module Utils
55
def params_to_s(params)
66
list = params.each_with_object([]) do |(param, value), result|
77
if value.is_a?(Hash)
8-
result.concat(value.map { |k, v| "#{param}[#{k}]=#{v}" })
8+
values = value.map { |key, val| "#{param}[#{key}]=#{val}" }
9+
result.concat(values)
910
else
1011
result.push(["#{param}=#{value}"])
1112
end
@@ -18,13 +19,13 @@ def prepare_page(page_class, options: nil)
1819
page.options = options
1920
page.head_component = settings.components[:head]&.new
2021
page.flash_component = settings.components[:flash]&.new
21-
page.navbar_component = settings.components[:navbar]&.new(
22+
page.navbar_component = settings.components[:navbar]&.new
23+
page.navbar_component&.update_attributes(
2224
current_slug: context&.slug,
2325
root_path: settings.root_path,
2426
root_title: settings.root[:title],
25-
items: options&.include?(:no_menu) ? [] : settings.navbar
27+
items: options&.include?(:no_menu) ? [] : context&.navbar
2628
)
27-
2829
yield(page) if block_given?
2930
end
3031
end

lib/tiny_admin/views/actions/index.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ def template
3030

3131
if filters&.any?
3232
div(class: 'col-3') {
33-
filters_form_attrs = { section_path: route_for(context.slug), filters: filters }
34-
render TinyAdmin::Views::Components::FiltersForm.new(**filters_form_attrs)
33+
filters_form = TinyAdmin::Views::Components::FiltersForm.new
34+
filters_form.update_attributes(section_path: route_for(context.slug), filters: filters)
35+
render filters_form
3536
}
3637
end
3738
}
@@ -65,9 +66,9 @@ def table_body
6566
field = fields[key]
6667
td(class: "field-value-#{field.name} field-value-type-#{field.type}") {
6768
if field.options && field.options[:link_to]
68-
messages = (field.options[:call] || '').split(',').map(&:strip)
69-
label = messages.any? ? messages.inject(record) { |result, msg| result&.send(msg) } : value
70-
a(href: route_for(field.options[:link_to], reference: value)) { label }
69+
a(href: route_for(field.options[:link_to], reference: value)) {
70+
field.apply_call_option(record) || value
71+
}
7172
else
7273
value
7374
end

0 commit comments

Comments
 (0)