Skip to content

Commit 8988900

Browse files
author
Rafael Mendonça França
committed
Fix the CollectionProxy#new method
It was using the default Active Record implementation. Fixes #21
1 parent 6b59a5b commit 8988900

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lib/active_record/mass_assignment_security/associations.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class CollectionProxy
5454
def build(attributes = {}, options = {}, &block)
5555
@association.build(attributes, options, &block)
5656
end
57+
alias_method :new, :build
5758

5859
def create(attributes = {}, options = {}, &block)
5960
@association.create(attributes, options, &block)

test/attribute_sanitization_test.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,40 @@ def test_has_many_build_with_strict_sanitizer
595595
end
596596
end
597597

598+
# new
599+
600+
def test_has_many_build_with_attr_protected_attributes
601+
best_friend = @person.best_friends.new(attributes_hash)
602+
assert_default_attributes(best_friend)
603+
end
604+
605+
def test_has_many_build_with_attr_accessible_attributes
606+
best_friend = @person.best_friends.new(attributes_hash)
607+
assert_default_attributes(best_friend)
608+
end
609+
610+
def test_has_many_build_with_admin_role_with_attr_protected_attributes
611+
best_friend = @person.best_friends.new(attributes_hash, :as => :admin)
612+
assert_admin_attributes(best_friend)
613+
end
614+
615+
def test_has_many_build_with_admin_role_with_attr_accessible_attributes
616+
best_friend = @person.best_friends.new(attributes_hash, :as => :admin)
617+
assert_admin_attributes(best_friend)
618+
end
619+
620+
def test_has_many_build_without_protection
621+
best_friend = @person.best_friends.new(attributes_hash, :without_protection => true)
622+
assert_all_attributes(best_friend)
623+
end
624+
625+
def test_has_many_build_with_strict_sanitizer
626+
with_strict_sanitizer do
627+
best_friend = @person.best_friends.new(attributes_hash.except(:id, :comments))
628+
assert_equal @person.id, best_friend.best_friend_id
629+
end
630+
end
631+
598632
# create
599633

600634
def test_has_many_create_with_attr_protected_attributes

0 commit comments

Comments
 (0)