Skip to content

Commit

Permalink
enhancement: allow dynamic content in description and name (#3693)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
marlena-b and Paul-Bob authored Feb 26, 2025
1 parent 40747e8 commit dac4c27
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,7 @@ GEM

PLATFORMS
arm64-darwin-23
arm64-darwin-24
x86_64-linux

DEPENDENCIES
Expand Down
6 changes: 1 addition & 5 deletions app/components/avo/views/resource_index_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,7 @@ def singular_resource_name

def description
# If this is a has many association, the user can pass a description to be shown just for this association.
if @reflection.present?
return field.description if field.present? && field.description

return
end
return field&.description(query: @query) if @reflection.present?

@resource.description
end
Expand Down
8 changes: 6 additions & 2 deletions lib/avo/concerns/has_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ module HasDescription
attr_accessor :description
end

def description
Avo::ExecutionContext.new(target: @description || self.class.description, **description_attributes).handle
def description(additional_attributes = {})
Avo::ExecutionContext.new(
target: @description || self.class.description,
**description_attributes,
**additional_attributes
).handle
end

private
Expand Down
6 changes: 3 additions & 3 deletions lib/avo/fields/base_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ def translated_plural_name(default:)
# Secondly we'll try to find a translation key
# We'll fallback to humanizing the id
def name
return @name if custom_name?

if translation_key
if custom_name?
Avo::ExecutionContext.new(target: @name).handle
elsif translation_key
translated_name default: default_name
else
default_name
Expand Down
2 changes: 2 additions & 0 deletions spec/dummy/app/avo/resources/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ def second_tabs_group
translation_key: "avo.field_translations.people"
field :posts,
as: :has_many,
name: -> { "Posts" },
description: -> { "This user has #{query.count} posts." },
show_on: :edit,
attach_scope: -> { query.where.not(user_id: parent.id).or(query.where(user_id: nil)) }
field :comments,
Expand Down
14 changes: 14 additions & 0 deletions spec/features/avo/has_many_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,18 @@
expect { find("tr[data-resource-id='#{team.id}'] [data-control='detach']").click }.to raise_error("Callback Called")
end
end

describe "dynamic description" do
let(:url) { "/admin/resources/users/#{user.slug}/posts?turbo_frame=has_many_field_posts&view_type=table" }
let!(:post_1) { create :post, user: user }
let!(:post_2) { create :post, user: user }

it { is_expected.to have_text "This user has 2 posts" }
end

describe "dynamic name" do
let(:url) { "/admin/resources/users/#{user.slug}/posts?turbo_frame=has_many_field_posts&view_type=table" }

it { is_expected.to have_text "Posts" }
end
end

0 comments on commit dac4c27

Please sign in to comment.