Skip to content

Commit ec1442e

Browse files
author
Mattia Roccoberton
committed
feat: actions improvements
1 parent 2e28090 commit ec1442e

File tree

9 files changed

+25
-22
lines changed

9 files changed

+25
-22
lines changed

lib/tiny_admin/actions/index.rb

Lines changed: 3 additions & 3 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'))

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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ module TinyAdmin
44
class Context
55
include Singleton
66

7-
attr_accessor :navbar,
7+
attr_accessor :actions,
8+
:navbar,
89
:pages,
910
:reference,
1011
:repository,

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: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ def add_resource_section(slug, section)
113113
repository: repository.is_a?(String) ? Object.const_get(repository) : repository
114114
}
115115
resource_options = section.slice(:resource, :only, :index, :show, :collection_actions, :member_actions)
116+
resource_options[:only] ||= %i[index show]
116117
context.resources[slug].merge!(resource_options)
117118
hidden = section[:options] && (section[:options].include?(:hidden) || section[:options].include?('hidden'))
118119
{ name: section[:name], path: route_for(slug) } unless hidden

lib/tiny_admin/views/actions/show.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ def actions_buttons
4646
(actions || {}).each do |action, action_class|
4747
li(class: 'nav-item mx-1') {
4848
href = route_for(context.slug, reference: context.reference, action: action)
49-
title = action_class.respond_to?(:title) ? action_class.title : action
50-
a(href: href, class: 'nav-link btn btn-outline-secondary') { title }
49+
a(href: href, class: 'nav-link btn btn-outline-secondary') {
50+
action_class.respond_to?(:title) ? action_class.title : action
51+
}
5152
}
5253
end
5354
}

spec/dummy/app/tiny_admin/sample_collection_action.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def template
77
end
88

99
class SampleCollectionAction < TinyAdmin::Actions::BasicAction
10-
def call(app:, context:, options:, actions: [])
10+
def call(app:, context:, options:)
1111
SampleCollectionPage
1212
end
1313
end

spec/dummy/app/tiny_admin/sample_member_action.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def template
77
end
88

99
class SampleMemberAction < TinyAdmin::Actions::BasicAction
10-
def call(app:, context:, options:, actions: [])
10+
def call(app:, context:, options:)
1111
SampleMemberPage
1212
end
1313
end

0 commit comments

Comments
 (0)