未验证 提交 470e6bda 编写于 作者: R Rafael Mendonça França

Merge pull request #34966 from bogdanvlviv/ensure-ar-relation-exists-allows-permitted-params

Ensure that AR::Relation#exists? allows only permitted params
...@@ -56,7 +56,7 @@ ...@@ -56,7 +56,7 @@
*Laerti Papa* *Laerti Papa*
* Allow `ActionController::Params` as argument of `ActiveRecord::Base#exists?`. * Allow permitted instance of `ActionController::Parameters` as argument of `ActiveRecord::Relation#exists?`.
*Gannon McGibbon* *Gannon McGibbon*
......
...@@ -226,11 +226,15 @@ def test_exists_with_string ...@@ -226,11 +226,15 @@ def test_exists_with_string
end end
def test_exists_with_strong_parameters def test_exists_with_strong_parameters
assert_equal false, Subscriber.exists?(Parameters.new(nick: "foo")) assert_equal false, Subscriber.exists?(Parameters.new(nick: "foo").permit!)
Subscriber.create!(nick: "foo") Subscriber.create!(nick: "foo")
assert_equal true, Subscriber.exists?(Parameters.new(nick: "foo")) assert_equal true, Subscriber.exists?(Parameters.new(nick: "foo").permit!)
assert_raises(ActiveModel::ForbiddenAttributesError) do
Subscriber.exists?(Parameters.new(nick: "foo"))
end
end end
def test_exists_passing_active_record_object_is_not_permitted def test_exists_passing_active_record_object_is_not_permitted
......
...@@ -3,10 +3,16 @@ ...@@ -3,10 +3,16 @@
class Parameters class Parameters
def initialize(parameters = {}) def initialize(parameters = {})
@parameters = parameters.with_indifferent_access @parameters = parameters.with_indifferent_access
@permitted = false
end end
def permitted? def permitted?
true @permitted
end
def permit!
@permitted = true
self
end end
def to_h def to_h
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册