From 8f4220e596e0c633e40e6e8bfc1427a0796be3d7 Mon Sep 17 00:00:00 2001 From: Adrian Marin Date: Fri, 27 Sep 2024 13:53:39 +0300 Subject: [PATCH] refactor: add `resource` and `parent_resource` on associations `scope` block (#3268) * refactor: pass parent to has-many scope * test --------- Co-authored-by: Paul Bob <69730720+Paul-Bob@users.noreply.github.com> Co-authored-by: Paul Bob --- app/controllers/avo/associations_controller.rb | 8 +++++++- spec/dummy/app/avo/resources/user.rb | 6 +++++- spec/features/avo/has_many_field_spec.rb | 1 + 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/controllers/avo/associations_controller.rb b/app/controllers/avo/associations_controller.rb index d16c9d0cb7..5b18007818 100644 --- a/app/controllers/avo/associations_controller.rb +++ b/app/controllers/avo/associations_controller.rb @@ -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 diff --git a/spec/dummy/app/avo/resources/user.rb b/spec/dummy/app/avo/resources/user.rb index a72b6b06ac..d2528f5147 100644 --- a/spec/dummy/app/avo/resources/user.rb +++ b/spec/dummy/app/avo/resources/user.rb @@ -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 diff --git a/spec/features/avo/has_many_field_spec.rb b/spec/features/avo/has_many_field_spec.rb index 26bb57df23..5677a5fd58 100644 --- a/spec/features/avo/has_many_field_spec.rb +++ b/spec/features/avo/has_many_field_spec.rb @@ -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