提交 a99e0045 编写于 作者: R Ryuta Kamizono

Remove duplicated protected params definitions

Use "support/stubs/strong_parameters" instead.
上级 257564d6
......@@ -2,6 +2,7 @@
require "cases/helper"
require "support/schema_dumping_helper"
require "support/stubs/strong_parameters"
class PostgresqlHstoreTest < ActiveRecord::PostgreSQLTestCase
include SchemaDumpingHelper
......@@ -11,12 +12,6 @@ class Hstore < ActiveRecord::Base
store_accessor :settings, :language, :timezone
end
class FakeParameters
def to_unsafe_h
{ "hi" => "hi" }
end
end
def setup
@connection = ActiveRecord::Base.connection
......@@ -344,7 +339,7 @@ def test_schema_dump_with_shorthand
end
def test_supports_to_unsafe_h_values
assert_equal("\"hi\"=>\"hi\"", @type.serialize(FakeParameters.new))
assert_equal "\"hi\"=>\"hi\"", @type.serialize(ProtectedParams.new("hi" => "hi"))
end
private
......
......@@ -19,6 +19,7 @@
require "models/post"
require "models/comment"
require "models/rating"
require "support/stubs/strong_parameters"
class CalculationsTest < ActiveRecord::TestCase
fixtures :companies, :accounts, :topics, :speedometers, :minivans, :books, :posts, :comments
......@@ -897,26 +898,7 @@ def test_sum_uses_enumerable_version_when_block_is_given
end
def test_having_with_strong_parameters
protected_params = Class.new do
attr_reader :permitted
alias :permitted? :permitted
def initialize(parameters)
@parameters = parameters
@permitted = false
end
def to_h
@parameters
end
def permit!
@permitted = true
self
end
end
params = protected_params.new(credit_limit: "50")
params = ProtectedParams.new(credit_limit: "50")
assert_raises(ActiveModel::ForbiddenAttributesError) do
Account.group(:id).having(params)
......
......@@ -226,14 +226,14 @@ def test_exists_with_string
end
def test_exists_with_strong_parameters
assert_equal false, Subscriber.exists?(Parameters.new(nick: "foo").permit!)
assert_equal false, Subscriber.exists?(ProtectedParams.new(nick: "foo").permit!)
Subscriber.create!(nick: "foo")
assert_equal true, Subscriber.exists?(Parameters.new(nick: "foo").permit!)
assert_equal true, Subscriber.exists?(ProtectedParams.new(nick: "foo").permit!)
assert_raises(ActiveModel::ForbiddenAttributesError) do
Subscriber.exists?(Parameters.new(nick: "foo"))
Subscriber.exists?(ProtectedParams.new(nick: "foo"))
end
end
......
# frozen_string_literal: true
require "cases/helper"
require "active_support/core_ext/hash/indifferent_access"
require "models/company"
require "models/person"
require "models/ship"
require "models/ship_part"
require "models/treasure"
class ProtectedParams
attr_accessor :permitted
alias :permitted? :permitted
delegate :keys, :key?, :has_key?, :empty?, to: :@parameters
def initialize(attributes)
@parameters = attributes.with_indifferent_access
@permitted = false
end
def permit!
@permitted = true
self
end
def [](key)
@parameters[key]
end
def to_h
@parameters
end
def stringify_keys
dup
end
def dup
super.tap do |duplicate|
duplicate.instance_variable_set :@permitted, @permitted
end
end
end
require "support/stubs/strong_parameters"
class ForbiddenAttributesProtectionTest < ActiveRecord::TestCase
def test_forbidden_attributes_cannot_be_used_for_mass_assignment
......
......@@ -14,6 +14,7 @@
require "models/topic"
require "models/treasure"
require "models/vertex"
require "support/stubs/strong_parameters"
module ActiveRecord
class WhereTest < ActiveRecord::TestCase
......@@ -339,27 +340,8 @@ def test_where_on_association_with_select_relation
end
def test_where_with_strong_parameters
protected_params = Class.new do
attr_reader :permitted
alias :permitted? :permitted
def initialize(parameters)
@parameters = parameters
@permitted = false
end
def to_h
@parameters
end
def permit!
@permitted = true
self
end
end
author = authors(:david)
params = protected_params.new(name: author.name)
params = ProtectedParams.new(name: author.name)
assert_raises(ActiveModel::ForbiddenAttributesError) { Author.where(params) }
assert_equal author, Author.where(params.permit!).first
end
......
# frozen_string_literal: true
class Parameters
require "active_support/core_ext/hash/indifferent_access"
class ProtectedParams
delegate :keys, :key?, :has_key?, :empty?, to: :@parameters
def initialize(parameters = {})
@parameters = parameters.with_indifferent_access
@permitted = false
......@@ -15,7 +19,22 @@ def permit!
self
end
def [](key)
@parameters[key]
end
def to_h
@parameters.to_h
end
alias to_unsafe_h to_h
def stringify_keys
dup
end
def dup
super.tap do |duplicate|
duplicate.instance_variable_set :@permitted, @permitted
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册