Skip to content

Commit dac4c27

Browse files
marlena-bPaul-Bob
andauthored
enhancement: allow dynamic content in description and name (#3693)
* Add m1 platform to Gemfile.lock * Pass @query to the ExecutionContext when generating description in has many field * Allow for passing lambda as a name * Remove name_attributes * Remove redundant return --------- Co-authored-by: Paul Bob <[email protected]>
1 parent 40747e8 commit dac4c27

File tree

6 files changed

+27
-10
lines changed

6 files changed

+27
-10
lines changed

Gemfile.lock

+1
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,7 @@ GEM
700700

701701
PLATFORMS
702702
arm64-darwin-23
703+
arm64-darwin-24
703704
x86_64-linux
704705

705706
DEPENDENCIES

app/components/avo/views/resource_index_component.rb

+1-5
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,7 @@ def singular_resource_name
119119

120120
def description
121121
# If this is a has many association, the user can pass a description to be shown just for this association.
122-
if @reflection.present?
123-
return field.description if field.present? && field.description
124-
125-
return
126-
end
122+
return field&.description(query: @query) if @reflection.present?
127123

128124
@resource.description
129125
end

lib/avo/concerns/has_description.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ module HasDescription
88
attr_accessor :description
99
end
1010

11-
def description
12-
Avo::ExecutionContext.new(target: @description || self.class.description, **description_attributes).handle
11+
def description(additional_attributes = {})
12+
Avo::ExecutionContext.new(
13+
target: @description || self.class.description,
14+
**description_attributes,
15+
**additional_attributes
16+
).handle
1317
end
1418

1519
private

lib/avo/fields/base_field.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ def translated_plural_name(default:)
116116
# Secondly we'll try to find a translation key
117117
# We'll fallback to humanizing the id
118118
def name
119-
return @name if custom_name?
120-
121-
if translation_key
119+
if custom_name?
120+
Avo::ExecutionContext.new(target: @name).handle
121+
elsif translation_key
122122
translated_name default: default_name
123123
else
124124
default_name

spec/dummy/app/avo/resources/user.rb

+2
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ def second_tabs_group
258258
translation_key: "avo.field_translations.people"
259259
field :posts,
260260
as: :has_many,
261+
name: -> { "Posts" },
262+
description: -> { "This user has #{query.count} posts." },
261263
show_on: :edit,
262264
attach_scope: -> { query.where.not(user_id: parent.id).or(query.where(user_id: nil)) }
263265
field :comments,

spec/features/avo/has_many_field_spec.rb

+14
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,18 @@
156156
expect { find("tr[data-resource-id='#{team.id}'] [data-control='detach']").click }.to raise_error("Callback Called")
157157
end
158158
end
159+
160+
describe "dynamic description" do
161+
let(:url) { "/admin/resources/users/#{user.slug}/posts?turbo_frame=has_many_field_posts&view_type=table" }
162+
let!(:post_1) { create :post, user: user }
163+
let!(:post_2) { create :post, user: user }
164+
165+
it { is_expected.to have_text "This user has 2 posts" }
166+
end
167+
168+
describe "dynamic name" do
169+
let(:url) { "/admin/resources/users/#{user.slug}/posts?turbo_frame=has_many_field_posts&view_type=table" }
170+
171+
it { is_expected.to have_text "Posts" }
172+
end
159173
end

0 commit comments

Comments
 (0)