Skip to content

Commit 6dcd314

Browse files
authored
Merge pull request #419 from seanpdoyle/strong-parameters
Support Strong Parameters
2 parents 42b6fa3 + 44c934b commit 6dcd314

File tree

6 files changed

+53
-1
lines changed

6 files changed

+53
-1
lines changed

lib/active_resource/base.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,7 @@ def all(*args)
10661066
end
10671067

10681068
def where(clauses = {})
1069+
clauses = sanitize_forbidden_attributes(clauses)
10691070
raise ArgumentError, "expected a clauses Hash, got #{clauses.inspect}" unless clauses.is_a? Hash
10701071
find(:all, params: clauses)
10711072
end
@@ -1498,6 +1499,7 @@ def load(attributes, remove_root = false, persisted = false)
14981499
raise ArgumentError, "expected attributes to be able to convert to Hash, got #{attributes.inspect}"
14991500
end
15001501

1502+
attributes = sanitize_forbidden_attributes(attributes)
15011503
attributes = attributes.to_hash
15021504
@prefix_options, attributes = split_options(attributes)
15031505

@@ -1745,11 +1747,13 @@ def method_missing(method_symbol, *arguments) # :nodoc:
17451747
end
17461748

17471749
class Base
1750+
extend ActiveModel::ForbiddenAttributesProtection
17481751
extend ActiveModel::Naming
17491752
extend ActiveResource::Associations
17501753

17511754
include Callbacks, CustomMethods, Validations
17521755
include ActiveModel::Conversion
1756+
include ActiveModel::ForbiddenAttributesProtection
17531757
include ActiveModel::Serializers::JSON
17541758
include ActiveModel::Serializers::Xml
17551759
include ActiveResource::Reflection, ActiveResource::Rescuable

test/abstract_unit.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
require "active_support"
1212
require "active_support/test_case"
1313
require "setter_trap"
14+
require "strong_parameters"
1415
require "active_support/logger"
1516
require "base64"
1617

test/cases/base/load_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,16 @@ def test_load_object_with_implicit_conversion_to_hash
127127
assert_equal @matz.stringify_keys, @person.load(FakeParameters.new(@matz)).attributes
128128
end
129129

130+
def test_load_object_with_unpermitted_strong_parameters
131+
params = StrongParameters.new(@matz)
132+
assert_raises(ActiveModel::ForbiddenAttributesError) { @person.load(params) }
133+
end
134+
135+
def test_load_object_with_permitted_strong_parameters
136+
params = StrongParameters.new(@matz).tap(&:permit!)
137+
assert_equal @matz.stringify_keys, @person.load(params).attributes
138+
end
139+
130140
def test_after_load_attributes_are_accessible
131141
assert_equal Hash.new, @person.attributes
132142
assert_equal @matz.stringify_keys, @person.load(@matz).attributes

test/cases/base_test.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,11 @@ def test_collection_url_with_parameters
817817
assert Person.collection_url(name: "Test", student: true).include?("name=Test")
818818
assert Person.collection_url(name: "Test", student: true).include?("student=true")
819819

820-
assert_equal "http://37s.sunrise.i:3000/people.json?name%5B%5D=bob&name%5B%5D=your+uncle%2Bme&name%5B%5D=&name%5B%5D=false", Person.collection_url(name: [ "bob", "your uncle+me", nil, false ])
820+
if ActiveSupport::VERSION::MAJOR < 8 || ActiveSupport::VERSION::MINOR < 1
821+
assert_equal "http://37s.sunrise.i:3000/people.json?name%5B%5D=bob&name%5B%5D=your+uncle%2Bme&name%5B%5D=&name%5B%5D=false", Person.collection_url(name: [ "bob", "your uncle+me", nil, false ])
822+
else
823+
assert_equal "http://37s.sunrise.i:3000/people.json?name%5B%5D=bob&name%5B%5D=your+uncle%2Bme&name%5B%5D&name%5B%5D=false", Person.collection_url(name: [ "bob", "your uncle+me", nil, false ])
824+
end
821825
assert_equal "http://37s.sunrise.i:3000/people.json?struct%5Ba%5D%5B%5D=2&struct%5Ba%5D%5B%5D=1&struct%5Bb%5D=fred", Person.collection_url(struct: { :a => [ 2, 1 ], "b" => "fred" })
822826
end
823827

test/cases/finder_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ def test_where_with_clauses
6363
assert_kind_of StreetAddress, addresses.first
6464
end
6565

66+
def test_where_clause_with_unpermitted_params
67+
params = StrongParameters.new(person_id: "1")
68+
assert_raises(ActiveModel::ForbiddenAttributesError) { StreetAddress.where(params) }
69+
end
70+
71+
def test_where_clause_with_permitted_params
72+
params = StrongParameters.new(person_id: "1").tap(&:permit!)
73+
addresses = StreetAddress.where(params)
74+
assert_equal 1, addresses.size
75+
assert_kind_of StreetAddress, addresses.first
76+
end
77+
6678
def test_where_with_clause_in
6779
ActiveResource::HttpMock.respond_to { |m| m.get "/people.json?id%5B%5D=2", {}, @people_david }
6880
people = Person.where(id: [ 2 ])

test/strong_parameters.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
class StrongParameters
4+
def initialize(parameters = {})
5+
@parameters = parameters
6+
@permitted = false
7+
end
8+
9+
def permitted?
10+
@permitted
11+
end
12+
13+
def permit!
14+
@permitted = true
15+
end
16+
17+
def to_hash
18+
@parameters.to_hash
19+
end
20+
alias to_h to_hash
21+
end

0 commit comments

Comments
 (0)