Skip to content

Commit

Permalink
Fix breaking with a habtm association with scope given
Browse files Browse the repository at this point in the history
Fixes #2067
  • Loading branch information
mshibuya committed Jun 26, 2022
1 parent d1f1154 commit c2bf6db
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/rails_admin/adapters/active_record/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def inverse_of
end

def read_only?
(klass.all.instance_eval(&scope).readonly_value if scope.is_a? Proc) ||
(klass.all.instance_exec(&scope).readonly_value if scope.is_a?(Proc) && scope.arity == 0) ||
association.nested? ||
false
end
Expand Down
19 changes: 18 additions & 1 deletion spec/rails_admin/adapters/active_record/association_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class ARPost < Tableless
end

class ARCategory < Tableless
has_and_belongs_to_many :a_r_posts
has_and_belongs_to_many :a_r_posts, -> { readonly }
has_and_belongs_to_many :scoped_posts, ->(category) { where(id: category.id) }, class_name: 'ARPost'
belongs_to :librarian, polymorphic: true
end

Expand Down Expand Up @@ -173,6 +174,22 @@ class FieldTestWithSymbolForeignKey < FieldTest
expect(subject.read_only?).to be_falsey
expect(subject.nested_options).to be_nil
end

context 'with a scope given' do
subject { @category.associations.detect { |a| a.name == :a_r_posts } }

it 'does not break' do
expect(subject.read_only?).to be_truthy
end
end

context 'with a scope that receives an argument given' do
subject { @category.associations.detect { |a| a.name == :scoped_posts } }

it 'ignores the scope' do
expect(subject.read_only?).to be_falsey
end
end
end

describe 'polymorphic belongs_to association' do
Expand Down

0 comments on commit c2bf6db

Please sign in to comment.