Skip to content

Commit

Permalink
Fix the CollectionProxy#new method
Browse files Browse the repository at this point in the history
It was using the default Active Record implementation.

Fixes #21
  • Loading branch information
Rafael Mendonça França committed Mar 10, 2014
1 parent 6b59a5b commit 8988900
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/active_record/mass_assignment_security/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class CollectionProxy
def build(attributes = {}, options = {}, &block)
@association.build(attributes, options, &block)
end
alias_method :new, :build

def create(attributes = {}, options = {}, &block)
@association.create(attributes, options, &block)
Expand Down
34 changes: 34 additions & 0 deletions test/attribute_sanitization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,40 @@ def test_has_many_build_with_strict_sanitizer
end
end

# new

def test_has_many_build_with_attr_protected_attributes
best_friend = @person.best_friends.new(attributes_hash)
assert_default_attributes(best_friend)
end

def test_has_many_build_with_attr_accessible_attributes
best_friend = @person.best_friends.new(attributes_hash)
assert_default_attributes(best_friend)
end

def test_has_many_build_with_admin_role_with_attr_protected_attributes
best_friend = @person.best_friends.new(attributes_hash, :as => :admin)
assert_admin_attributes(best_friend)
end

def test_has_many_build_with_admin_role_with_attr_accessible_attributes
best_friend = @person.best_friends.new(attributes_hash, :as => :admin)
assert_admin_attributes(best_friend)
end

def test_has_many_build_without_protection
best_friend = @person.best_friends.new(attributes_hash, :without_protection => true)
assert_all_attributes(best_friend)
end

def test_has_many_build_with_strict_sanitizer
with_strict_sanitizer do
best_friend = @person.best_friends.new(attributes_hash.except(:id, :comments))
assert_equal @person.id, best_friend.best_friend_id
end
end

# create

def test_has_many_create_with_attr_protected_attributes
Expand Down

0 comments on commit 8988900

Please sign in to comment.