Skip to content

Commit

Permalink
refactor: add resource and parent_resource on associations `scope…
Browse files Browse the repository at this point in the history
…` block (#3268)

* refactor: pass parent to has-many scope

* test

---------

Co-authored-by: Paul Bob <[email protected]>
Co-authored-by: Paul Bob <[email protected]>
  • Loading branch information
3 people authored Sep 27, 2024
1 parent 595812c commit 8f4220e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
8 changes: 7 additions & 1 deletion app/controllers/avo/associations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ def index
@association_field = find_association_field(resource: @parent_resource, association: params[:related_name])

if @association_field.present? && @association_field.scope.present?
@query = Avo::ExecutionContext.new(target: @association_field.scope, query: @query, parent: @parent_record).handle
@query = Avo::ExecutionContext.new(
target: @association_field.scope,
query: @query,
parent: @parent_record,
resource: @resource,
parent_resource: @parent_resource
).handle
end

super
Expand Down
6 changes: 5 additions & 1 deletion spec/dummy/app/avo/resources/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,11 @@ def second_tabs_group
field :comments,
as: :has_many,
# show_on: :edit,
scope: -> { query.starts_with parent.first_name[0].downcase },
scope: -> {
TestBuddy.hi("parent_resource:#{parent_resource.present?},resource:#{resource.present?}")

query.starts_with parent.first_name[0].downcase
},
description: "The comments listed in the attach modal all start with the name of the parent user."
field :comment, as: :has_one, name: "Main comment"
end
Expand Down
1 change: 1 addition & 0 deletions spec/features/avo/has_many_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
let!(:a_comment) { create :comment, user: user, body: "A comment that starts with the letter A" }

subject do
expect(TestBuddy).to receive(:hi).with("parent_resource:true,resource:true").at_least :once
visit "/admin/resources/users/#{user.id}/comments?turbo_frame=has_many_field_show_comments"
page
end
Expand Down

0 comments on commit 8f4220e

Please sign in to comment.