Skip to content

Commit c2bf6db

Browse files
committed
Fix breaking with a habtm association with scope given
Fixes #2067
1 parent d1f1154 commit c2bf6db

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

lib/rails_admin/adapters/active_record/association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def inverse_of
9292
end
9393

9494
def read_only?
95-
(klass.all.instance_eval(&scope).readonly_value if scope.is_a? Proc) ||
95+
(klass.all.instance_exec(&scope).readonly_value if scope.is_a?(Proc) && scope.arity == 0) ||
9696
association.nested? ||
9797
false
9898
end

spec/rails_admin/adapters/active_record/association_spec.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class ARPost < Tableless
2020
end
2121

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

@@ -173,6 +174,22 @@ class FieldTestWithSymbolForeignKey < FieldTest
173174
expect(subject.read_only?).to be_falsey
174175
expect(subject.nested_options).to be_nil
175176
end
177+
178+
context 'with a scope given' do
179+
subject { @category.associations.detect { |a| a.name == :a_r_posts } }
180+
181+
it 'does not break' do
182+
expect(subject.read_only?).to be_truthy
183+
end
184+
end
185+
186+
context 'with a scope that receives an argument given' do
187+
subject { @category.associations.detect { |a| a.name == :scoped_posts } }
188+
189+
it 'ignores the scope' do
190+
expect(subject.read_only?).to be_falsey
191+
end
192+
end
176193
end
177194

178195
describe 'polymorphic belongs_to association' do

0 commit comments

Comments
 (0)