Skip to content

Commit 9a42b97

Browse files
authored
Merge pull request #20 from blocknotes/feat/index-entries-links
feat: customise index entries links
2 parents 909a981 + f44d106 commit 9a42b97

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

lib/tiny_admin/actions/index.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Index < BasicAction
66
attr_reader :current_page,
77
:fields_options,
88
:filters_list,
9+
:links,
910
:pagination,
1011
:pages,
1112
:params,
@@ -25,6 +26,7 @@ def call(app:, context:, options:)
2526
actions: context.actions,
2627
fields: fields,
2728
filters: filters,
29+
links: links,
2830
prepare_record: ->(record) { repository.index_record_attrs(record, fields: fields_options) },
2931
records: records,
3032
title: repository.index_title
@@ -41,6 +43,7 @@ def evaluate_options(options)
4143
@filters_list = options[:filters]
4244
@pagination = options[:pagination] || 10
4345
@sort = options[:sort]
46+
@links = options[:links]
4447

4548
@current_page = (params['p'] || 1).to_i
4649
@query_string = params_to_s(params.except('p'))

lib/tiny_admin/utils.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ def route_for(section, reference: nil, action: nil, query: nil)
3737
route[0] == '/' ? route : route.prepend('/')
3838
end
3939

40+
def to_label(string)
41+
return '' unless string
42+
43+
string.respond_to?(:humanize) ? string.humanize : string.tr('_', ' ').capitalize
44+
end
45+
4046
def context
4147
TinyAdmin::Context.instance
4248
end

lib/tiny_admin/views/actions/index.rb

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module TinyAdmin
44
module Views
55
module Actions
66
class Index < DefaultLayout
7-
attr_accessor :actions, :fields, :filters, :pagination_component, :prepare_record, :records
7+
attr_accessor :actions, :fields, :filters, :links, :pagination_component, :prepare_record, :records
88

99
def template
1010
super do
@@ -74,8 +74,25 @@ def table_body
7474
end
7575
}
7676
end
77-
td(class: 'actions') {
78-
a(href: route_for(context.slug, reference: record.id)) { 'show' }
77+
78+
td(class: 'actions p-1') {
79+
div(class: 'btn-group btn-group-sm') {
80+
link_class = 'btn btn-outline-secondary'
81+
if links
82+
links.each do |link|
83+
whitespace
84+
if link == 'show'
85+
a(href: route_for(context.slug, reference: record.id), class: link_class) { 'show' }
86+
else
87+
a(href: route_for(context.slug, reference: record.id, action: link), class: link_class) {
88+
to_label(link)
89+
}
90+
end
91+
end
92+
else
93+
a(href: route_for(context.slug, reference: record.id), class: link_class) { 'show' }
94+
end
95+
}
7996
}
8097
}
8198
end
@@ -87,8 +104,9 @@ def actions_buttons
87104
(actions || {}).each do |action, action_class|
88105
li(class: 'nav-item mx-1') {
89106
href = route_for(context.slug, action: action)
90-
title = action_class.respond_to?(:title) ? action_class.title : action
91-
a(href: href, class: 'nav-link btn btn-outline-secondary') { title }
107+
a(href: href, class: 'nav-link btn btn-outline-secondary') {
108+
action_class.respond_to?(:title) ? action_class.title : action
109+
}
92110
}
93111
end
94112
}

spec/dummy/config/tiny_admin.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ sections:
2727
- sample_col: SampleCollectionAction
2828
member_actions:
2929
- sample_mem: SampleMemberAction
30+
index:
31+
attributes:
32+
- id
33+
- name
34+
- email
35+
links:
36+
- show
37+
- sample_mem
3038
- slug: posts
3139
name: Posts
3240
type: resource

0 commit comments

Comments
 (0)