Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #24 from avit/association_relation_scoping
Browse files Browse the repository at this point in the history
Preserve scoping when building associations with conditions
  • Loading branch information
westonganger authored Mar 24, 2021
2 parents e8c9854 + b38b8a9 commit 86a56c6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
42 changes: 27 additions & 15 deletions lib/active_record/mass_assignment_security/association_relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,41 @@ class AssociationRelation
undef :create!

def build(attributes = nil, options = {}, &block)
if ActiveRecord::VERSION::STRING.to_f < 5.2
scoping { @association.build(attributes, options, &block) }
else
@association.build(attributes, options, &block)
end
block = protected_attributes_scope_block('new', block)
scoping { @association.build(attributes, options, &block) }
end
alias new build

def create(attributes = nil, options = {}, &block)
if ActiveRecord::VERSION::STRING.to_f < 5.2
scoping { @association.create(attributes, options, &block) }
else
@association.create(attributes, options, &block)
end
block = protected_attributes_scope_block('create', block)
scoping { @association.create(attributes, options, &block) }
end

def create!(attributes = nil, options = {}, &block)
if ActiveRecord::VERSION::STRING.to_f < 5.2
scoping { @association.create!(attributes, options, &block) }
else
@association.create!(attributes, options, &block)
end
block = protected_attributes_scope_block('create!', block)
scoping { @association.create!(attributes, options, &block) }
end

private

if ActiveRecord.gem_version < Gem::Version.new('6.0')

def protected_attributes_scope_block(_label, block)
block
end

elsif ActiveRecord.gem_version < Gem::Version.new('6.1')

def protected_attributes_scope_block(label, block)
_deprecated_scope_block(label, &block)
end

else

def protected_attributes_scope_block(_label, block)
current_scope_restoring_block(&block)
end

end
end
end
18 changes: 18 additions & 0 deletions test/mass_assignment_security/association_relation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,22 @@ class AssociationRelationTest < ActiveSupport::TestCase
group = Group.create
assert_nothing_raised { group.memberships.where("1=1").first_or_create }
end

test "build passes scoped attributes" do
group = Group.create
membership = group.memberships.where(pirate_id: 1).build
assert_equal(1, membership.pirate_id)
end

test "create passes scoped attributes" do
group = Group.create
membership = group.memberships.where(pirate_id: 1).create
assert_equal(1, membership.pirate_id)
end

test "create! passes scoped attributes" do
group = Group.create
membership = group.memberships.where(pirate_id: 1).create!
assert_equal(1, membership.pirate_id)
end
end

0 comments on commit 86a56c6

Please sign in to comment.