Skip to content

Commit

Permalink
feature: add scopes to associations (#637)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianthedev committed Feb 6, 2022
1 parent 68a4cec commit 05aa3a1
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/controllers/avo/relations_controller.rb
Expand Up @@ -18,6 +18,11 @@ def index
@parent_model = @parent_resource.class.find_scope.find(params[:id])
@parent_resource.hydrate(model: @parent_model)
@query = @authorization.apply_policy @parent_model.public_send(params[:related_name])
@association_field = @parent_resource.get_field params[:related_name]

if @association_field.present? && @association_field.scope.present?
@query = @query.instance_exec(&@association_field.scope)
end

super
end
Expand Down
6 changes: 6 additions & 0 deletions lib/avo/base_resource.rb
Expand Up @@ -154,6 +154,12 @@ def get_fields(panel: nil, reflection: nil)
fields
end

def get_field(id)
get_field_definitions.find do |f|
f.id == id.to_sym
end
end

def get_grid_fields
return if self.class.grid_loader.blank?

Expand Down
2 changes: 2 additions & 0 deletions lib/avo/fields/has_base_field.rb
Expand Up @@ -2,11 +2,13 @@ module Avo
module Fields
class HasBaseField < BaseField
attr_accessor :display
attr_accessor :scope

def initialize(id, **args, &block)
super(id, **args, &block)

@display = args[:display].present? ? args[:display] : :show
@scope = args[:scope].present? ? args[:scope] : nil
end

def resource
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/app/avo/resources/user_resource.rb
Expand Up @@ -46,7 +46,7 @@ class UserResource < Avo::BaseResource
field :posts, as: :has_many
field :people, as: :has_many, translation_key: 'avo.field_translations.people'
field :spouses, as: :has_many # STI has_many resource
field :comments, as: :has_many
field :comments, as: :has_many, scope: -> { starts_with :a }
field :projects, as: :has_and_belongs_to_many
field :teams, as: :has_and_belongs_to_many

Expand Down
2 changes: 2 additions & 0 deletions spec/dummy/app/models/comment.rb
Expand Up @@ -4,6 +4,8 @@ class Comment < ApplicationRecord
belongs_to :commentable, polymorphic: true, optional: true
belongs_to :user, optional: true

scope :starts_with, -> (prefix) { where('LOWER(body) LIKE ?', "#{prefix}%") }

def tiny_name
ActionView::Base.full_sanitizer.sanitize(body.to_s).truncate 30
end
Expand Down
13 changes: 13 additions & 0 deletions spec/features/avo/has_many_field_spec.rb
Expand Up @@ -107,4 +107,17 @@
end
end
end

describe "scope" do
let!(:regular_comment) { create :comment, user: user, body: 'Hey comment' }
let!(:a_comment) { create :comment, user: user, body: 'A comment that starts with the letter A'}

subject do
visit "/admin/resources/users/#{user.id}/comments?turbo_frame=has_many_field_show_comments"
page
end

it { is_expected.to have_text "A comment that starts with the letter A" }
it { is_expected.not_to have_text "Hey comment" }
end
end

0 comments on commit 05aa3a1

Please sign in to comment.