Skip to content

Commit 9b7006a

Browse files
authored
Merge pull request #9 from blocknotes/feat/improve-layout-structure
feat: improve layout structure
2 parents 7cc1362 + dd422f4 commit 9b7006a

File tree

12 files changed

+74
-87
lines changed

12 files changed

+74
-87
lines changed

lib/tiny_admin/actions/index.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ def call(app:, context:, options:, actions:)
1414
title = repository.index_title
1515
pages = (total_count / pagination) + 1
1616

17-
prepare_page(Views::Actions::Index, title: title, context: context, query_string: query_string) do |page|
17+
prepare_page(Views::Actions::Index, context: context) do |page|
1818
page.setup_pagination(current_page: current_page, pages: pages > 1 ? pages : false)
1919
page.setup_records(records: records, fields: fields, prepare_record: prepare_record)
20-
page.actions = actions
21-
page.filters = filters
20+
page.update_attributes(actions: actions, filters: filters, query_string: query_string, title: title)
2221
end
2322
end
2423

@@ -37,7 +36,8 @@ def evaluate_options(options)
3736
end
3837

3938
def prepare_filters(fields, filters_list)
40-
filters = (filters_list || []).map { _1.is_a?(Hash) ? _1 : { field: _1 } }.index_by { _1[:field] }
39+
filters = (filters_list || []).map { _1.is_a?(Hash) ? _1 : { field: _1 } }
40+
filters = filters.each_with_object({}) { |filter, result| result[filter[:field]] = filter }
4141
values = (params['q'] || {})
4242
fields.each_with_object({}) do |field, result|
4343
result[field] = { value: values[field.name], filter: filters[field.name] } if filters.key?(field.name)

lib/tiny_admin/actions/show.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ def call(app:, context:, options:, actions:)
1111
prepare_record = ->(record_data) { repository.show_record_attrs(record_data, fields: fields_options) }
1212
fields = repository.fields(options: fields_options)
1313

14-
prepare_page(Views::Actions::Show, title: repository.show_title(record), context: context) do |page|
14+
prepare_page(Views::Actions::Show, context: context) do |page|
1515
page.setup_record(record: record, fields: fields, prepare_record: prepare_record)
16-
page.actions = actions
16+
page.update_attributes(actions: actions, title: repository.show_title(record))
1717
end
1818
rescue Plugins::BaseRepository::RecordNotFound => _e
1919
prepare_page(options[:record_not_found_page] || Views::Pages::RecordNotFound)

lib/tiny_admin/authentication.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ class Authentication < BasicApp
2525

2626
def render_login(notices: nil, warnings: nil, errors: nil)
2727
page = prepare_page(settings.authentication[:login], options: %i[no_menu compact_layout])
28-
page.setup_flash_messages(
28+
page.messages = {
2929
notices: notices || flash['notices'],
3030
warnings: warnings || flash['warnings'],
3131
errors: errors || flash['errors']
32-
)
32+
}
3333
render(inline: page.call)
3434
end
3535
end

lib/tiny_admin/basic_app.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,5 @@ def authentication_plugin
2020
plugin authentication_plugin, TinyAdmin::Settings.instance.authentication
2121

2222
not_found { prepare_page(settings.page_not_found).call }
23-
24-
def attach_flash_messages(page)
25-
return unless page.respond_to?(:setup_flash_messages)
26-
27-
page.setup_flash_messages(notices: flash['notices'], warnings: flash['warnings'], errors: flash['errors'])
28-
end
2923
end
3024
end

lib/tiny_admin/router.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ class Router < BasicApp
4141
private
4242

4343
def render_page(page)
44-
attach_flash_messages(page)
44+
if page.respond_to?(:messages=)
45+
page.messages = { notices: flash['notices'], warnings: flash['warnings'], errors: flash['errors'] }
46+
end
4547
render(inline: page.call)
4648
end
4749

lib/tiny_admin/settings.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ def load_settings
4040

4141
@root ||= {}
4242
@root[:title] ||= 'TinyAdmin'
43-
@root[:path] ||= root_path
4443
@root[:page] ||= Views::Pages::Root
4544

4645
if @authentication[:plugin] == Plugins::SimpleAuth

lib/tiny_admin/utils.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,10 @@ def params_to_s(params)
1313
list.join('&')
1414
end
1515

16-
def prepare_page(page_class, title: nil, context: nil, query_string: '', options: [])
16+
def prepare_page(page_class, context: nil, options: nil)
1717
page_class.new.tap do |page|
18-
page.setup_page(title: title, query_string: query_string, settings: settings)
19-
page.setup_options(
20-
context: context,
21-
compact_layout: options.include?(:compact_layout),
22-
no_menu: options.include?(:no_menu)
23-
)
18+
page.context = context
19+
page.options = options
2420
yield(page) if block_given?
2521
end
2622
end

lib/tiny_admin/views/actions/index.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Views
55
module Actions
66
class Index < DefaultLayout
77
attr_reader :current_page, :fields, :pages, :prepare_record, :records
8-
attr_accessor :actions, :filters
8+
attr_accessor :actions, :filters, :query_string
99

1010
def setup_pagination(current_page:, pages:)
1111
@current_page = current_page
@@ -14,7 +14,7 @@ def setup_pagination(current_page:, pages:)
1414

1515
def setup_records(records:, fields:, prepare_record:)
1616
@records = records
17-
@fields = fields.index_by(&:name)
17+
@fields = fields.each_with_object({}) { |field, result| result[field.name] = field }
1818
@prepare_record = prepare_record
1919
end
2020

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
module TinyAdmin
4+
module Views
5+
class BasicLayout < Phlex::HTML
6+
include Utils
7+
8+
def components
9+
settings.components
10+
end
11+
12+
def update_attributes(attributes)
13+
attributes.each do |key, value|
14+
send("#{key}=", value)
15+
end
16+
end
17+
end
18+
end
19+
end

lib/tiny_admin/views/components/flash.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ module Components
66
class Flash < Phlex::HTML
77
attr_reader :errors, :notices, :warnings
88

9-
def initialize(notices: [], warnings: [], errors: [])
10-
@notices = notices
11-
@warnings = warnings
12-
@errors = errors
9+
def initialize(messages:)
10+
return unless messages
11+
12+
@notices = messages[:notices]
13+
@warnings = messages[:warnings]
14+
@errors = messages[:errors]
1315
end
1416

1517
def template

0 commit comments

Comments
 (0)