Skip to content

Commit 241d500

Browse files
authored
Merge pull request #18 from blocknotes/feat/call-option
feat: more resources actions options
2 parents 7790b35 + e6769ef commit 241d500

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

lib/tiny_admin/support.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
module TinyAdmin
44
class Support
55
class << self
6+
def call(value, options: [])
7+
value && options&.any? ? options.inject(value) { |result, message| result&.send(message) } : value
8+
end
9+
610
def downcase(value, options: [])
711
value&.downcase
812
end

lib/tiny_admin/views/actions/index.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ def table_header
4747
thead {
4848
tr {
4949
fields.each_value do |field|
50-
td(class: "field-header-#{field.name} field-header-type-#{field.type}") { field.title }
50+
td(class: "field-header-#{field.name} field-header-type-#{field.type}") {
51+
field.options[:header] || field.title
52+
}
5153
end
5254
td { whitespace }
5355
}
@@ -63,8 +65,9 @@ def table_body
6365
field = fields[key]
6466
td(class: "field-value-#{field.name} field-value-type-#{field.type}") {
6567
if field.options && field.options[:link_to]
66-
reference = record.send(field.options[:field])
67-
a(href: route_for(field.options[:link_to], reference: reference)) { value }
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 }
6871
else
6972
value
7073
end

lib/tiny_admin/views/actions/show.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ def template
2222
field = fields[key]
2323
div(class: "field-#{field.name} row lh-lg") {
2424
if field
25-
div(class: 'field-header col-2') { field.title }
25+
div(class: 'field-header col-2') { field.options[:header] || field.title }
2626
end
2727
div(class: 'field-value col-10') {
2828
if field.options && field.options[:link_to]
29-
reference = record.send(field.options[:field])
30-
a(href: route_for(field.options[:link_to], reference: reference)) { value }
29+
messages = (field.options[:call] || '').split(',').map(&:strip)
30+
label = messages.any? ? messages.inject(record) { |result, msg| result&.send(msg) } : value
31+
a(href: route_for(field.options[:link_to], reference: value)) { label }
3132
else
3233
value
3334
end

spec/dummy/config/tiny_admin.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ sections:
3535
pagination: 5
3636
attributes:
3737
- id
38-
- title
38+
- title: call, downcase, capitalize
3939
- field: author_id
4040
link_to: authors
4141
- category: upcase
@@ -66,6 +66,7 @@ sections:
6666
- description
6767
- field: author_id
6868
link_to: authors
69+
call: author, name
6970
- category
7071
- published
7172
- position: format, %f

0 commit comments

Comments
 (0)