2015-01-23 21:51:59 +00:00
|
|
|
require "cases/helper"
|
2015-11-23 22:19:39 +00:00
|
|
|
require "active_support/core_ext/hash/indifferent_access"
|
2015-01-23 21:51:59 +00:00
|
|
|
require "active_support/hash_with_indifferent_access"
|
2015-01-23 11:57:34 +00:00
|
|
|
|
|
|
|
class AttributeAssignmentTest < ActiveModel::TestCase
|
|
|
|
class Model
|
|
|
|
include ActiveModel::AttributeAssignment
|
|
|
|
|
|
|
|
attr_accessor :name, :description
|
|
|
|
|
|
|
|
def initialize(attributes = {})
|
|
|
|
assign_attributes(attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
def broken_attribute=(value)
|
2015-01-23 21:51:59 +00:00
|
|
|
raise ErrorFromAttributeWriter
|
2015-01-23 11:57:34 +00:00
|
|
|
end
|
|
|
|
|
2016-12-19 10:10:49 +00:00
|
|
|
# TODO Change this to private once we've dropped Ruby 2.2 support.
|
|
|
|
# Workaround for Ruby 2.2 "private attribute?" warning.
|
2015-01-23 21:51:59 +00:00
|
|
|
protected
|
|
|
|
|
2016-08-06 17:55:02 +00:00
|
|
|
attr_writer :metadata
|
2015-01-23 21:51:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class ErrorFromAttributeWriter < StandardError
|
2015-01-23 11:57:34 +00:00
|
|
|
end
|
|
|
|
|
2015-07-23 13:49:03 +00:00
|
|
|
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
|
|
|
|
|
2015-01-23 11:57:34 +00:00
|
|
|
def permit!
|
|
|
|
@permitted = true
|
2015-07-23 13:49:03 +00:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def [](key)
|
|
|
|
@parameters[key]
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_h
|
|
|
|
@parameters
|
2015-01-23 11:57:34 +00:00
|
|
|
end
|
|
|
|
|
2015-07-23 13:49:03 +00:00
|
|
|
def stringify_keys
|
|
|
|
dup
|
2015-01-23 11:57:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def dup
|
|
|
|
super.tap do |duplicate|
|
|
|
|
duplicate.instance_variable_set :@permitted, permitted?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "simple assignment" do
|
|
|
|
model = Model.new
|
|
|
|
|
2015-01-23 21:51:59 +00:00
|
|
|
model.assign_attributes(name: "hello", description: "world")
|
|
|
|
assert_equal "hello", model.name
|
|
|
|
assert_equal "world", model.description
|
2015-01-23 11:57:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "assign non-existing attribute" do
|
|
|
|
model = Model.new
|
2015-02-25 14:14:43 +00:00
|
|
|
error = assert_raises(ActiveModel::UnknownAttributeError) do
|
2015-01-23 11:57:34 +00:00
|
|
|
model.assign_attributes(hz: 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal model, error.record
|
|
|
|
assert_equal "hz", error.attribute
|
|
|
|
end
|
|
|
|
|
|
|
|
test "assign private attribute" do
|
|
|
|
model = Model.new
|
2015-02-25 14:14:43 +00:00
|
|
|
assert_raises(ActiveModel::UnknownAttributeError) do
|
2015-01-23 11:57:34 +00:00
|
|
|
model.assign_attributes(metadata: { a: 1 })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-23 21:51:59 +00:00
|
|
|
test "does not swallow errors raised in an attribute writer" do
|
|
|
|
assert_raises(ErrorFromAttributeWriter) do
|
2015-01-23 11:57:34 +00:00
|
|
|
Model.new(broken_attribute: 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-22 14:34:23 +00:00
|
|
|
test "an ArgumentError is raised if a non-hash-like object is passed" do
|
2015-01-23 21:51:59 +00:00
|
|
|
assert_raises(ArgumentError) do
|
2015-01-23 11:57:34 +00:00
|
|
|
Model.new(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-23 21:51:59 +00:00
|
|
|
test "forbidden attributes cannot be used for mass assignment" do
|
|
|
|
params = ProtectedParams.new(name: "Guille", description: "m")
|
|
|
|
|
2015-01-23 11:57:34 +00:00
|
|
|
assert_raises(ActiveModel::ForbiddenAttributesError) do
|
|
|
|
Model.new(params)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-23 21:51:59 +00:00
|
|
|
test "permitted attributes can be used for mass assignment" do
|
|
|
|
params = ProtectedParams.new(name: "Guille", description: "desc")
|
2015-01-23 11:57:34 +00:00
|
|
|
params.permit!
|
|
|
|
model = Model.new(params)
|
|
|
|
|
2015-01-23 21:51:59 +00:00
|
|
|
assert_equal "Guille", model.name
|
|
|
|
assert_equal "desc", model.description
|
2015-01-23 11:57:34 +00:00
|
|
|
end
|
|
|
|
|
2015-01-23 21:51:59 +00:00
|
|
|
test "regular hash should still be used for mass assignment" do
|
|
|
|
model = Model.new(name: "Guille", description: "m")
|
2015-01-23 11:57:34 +00:00
|
|
|
|
2015-01-23 21:51:59 +00:00
|
|
|
assert_equal "Guille", model.name
|
|
|
|
assert_equal "m", model.description
|
2015-01-23 11:57:34 +00:00
|
|
|
end
|
|
|
|
|
2015-01-23 21:51:59 +00:00
|
|
|
test "assigning no attributes should not raise, even if the hash is un-permitted" do
|
2015-01-23 11:57:34 +00:00
|
|
|
model = Model.new
|
|
|
|
assert_nil model.assign_attributes(ProtectedParams.new({}))
|
|
|
|
end
|
|
|
|
end
|