-
Notifications
You must be signed in to change notification settings - Fork 21.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate ActiveModel::ForbiddenAttributesProtection from StrongParam…
…eters gem
- Loading branch information
1 parent
8850054
commit a8f6d5c
Showing
4 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
activemodel/lib/active_model/forbidden_attributes_protection.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module ActiveModel | ||
class ForbiddenAttributes < StandardError | ||
end | ||
|
||
module ForbiddenAttributesProtection | ||
def sanitize_for_mass_assignment(new_attributes, options = {}) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
carlosantoniodasilva
Member
|
||
if !new_attributes.respond_to?(:permitted?) || (new_attributes.respond_to?(:permitted?) && new_attributes.permitted?) | ||
super | ||
else | ||
raise ActiveModel::ForbiddenAttributes | ||
end | ||
end | ||
end | ||
end |
32 changes: 32 additions & 0 deletions
32
activemodel/test/cases/forbidden_attributes_protection_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require 'cases/helper' | ||
require 'models/mass_assignment_specific' | ||
|
||
class ActiveModelMassUpdateProtectionTest < ActiveSupport::TestCase | ||
test "forbidden attributes cannot be used for mass updating" do | ||
params = { "a" => "b" } | ||
class << params | ||
define_method(:permitted?) { false } | ||
end | ||
assert_raises(ActiveModel::ForbiddenAttributes) do | ||
SpecialPerson.new.sanitize_for_mass_assignment(params) | ||
end | ||
end | ||
|
||
test "permitted attributes can be used for mass updating" do | ||
params = { "a" => "b" } | ||
class << params | ||
define_method(:permitted?) { true } | ||
end | ||
assert_nothing_raised do | ||
assert_equal({ "a" => "b" }, | ||
SpecialPerson.new.sanitize_for_mass_assignment(params)) | ||
end | ||
end | ||
|
||
test "regular attributes should still be allowed" do | ||
assert_nothing_raised do | ||
assert_equal({ a: "b" }, | ||
SpecialPerson.new.sanitize_for_mass_assignment(a: "b")) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
why do we have an unused
options
hash? I hate options hashes, especially unused ones. :-P