mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
✂️ and 💅 for #10776
Minor style changes across the board. Changed an alias to an explicit method declaration, since the alias will not be documented otherwise.
This commit is contained in:
parent
2606fb3397
commit
a225d4bec5
4 changed files with 37 additions and 49 deletions
|
@ -34,6 +34,7 @@ module ActiveModel
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def _assign_attributes(attributes)
|
def _assign_attributes(attributes)
|
||||||
attributes.each do |k, v|
|
attributes.each do |k, v|
|
||||||
_assign_attribute(k, v)
|
_assign_attribute(k, v)
|
||||||
|
@ -41,10 +42,8 @@ module ActiveModel
|
||||||
end
|
end
|
||||||
|
|
||||||
def _assign_attribute(k, v)
|
def _assign_attribute(k, v)
|
||||||
public_send("#{k}=", v)
|
|
||||||
rescue NoMethodError
|
|
||||||
if respond_to?("#{k}=")
|
if respond_to?("#{k}=")
|
||||||
raise
|
public_send("#{k}=", v)
|
||||||
else
|
else
|
||||||
raise UnknownAttributeError.new(self, k)
|
raise UnknownAttributeError.new(self, k)
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
require 'cases/helper'
|
require "cases/helper"
|
||||||
require 'active_support/hash_with_indifferent_access'
|
require "active_support/hash_with_indifferent_access"
|
||||||
|
|
||||||
class AttributeAssignmentTest < ActiveModel::TestCase
|
class AttributeAssignmentTest < ActiveModel::TestCase
|
||||||
|
|
||||||
class Model
|
class Model
|
||||||
include ActiveModel::AttributeAssignment
|
include ActiveModel::AttributeAssignment
|
||||||
|
|
||||||
|
@ -13,13 +12,15 @@ class AttributeAssignmentTest < ActiveModel::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def broken_attribute=(value)
|
def broken_attribute=(value)
|
||||||
non_existing_method(value)
|
raise ErrorFromAttributeWriter
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
protected
|
||||||
def metadata=(data)
|
|
||||||
@metadata = data
|
attr_writer :metadata
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class ErrorFromAttributeWriter < StandardError
|
||||||
end
|
end
|
||||||
|
|
||||||
class ProtectedParams < ActiveSupport::HashWithIndifferentAccess
|
class ProtectedParams < ActiveSupport::HashWithIndifferentAccess
|
||||||
|
@ -41,14 +42,14 @@ class AttributeAssignmentTest < ActiveModel::TestCase
|
||||||
test "simple assignment" do
|
test "simple assignment" do
|
||||||
model = Model.new
|
model = Model.new
|
||||||
|
|
||||||
model.assign_attributes(name: 'hello', description: 'world')
|
model.assign_attributes(name: "hello", description: "world")
|
||||||
assert_equal 'hello', model.name
|
assert_equal "hello", model.name
|
||||||
assert_equal 'world', model.description
|
assert_equal "world", model.description
|
||||||
end
|
end
|
||||||
|
|
||||||
test "assign non-existing attribute" do
|
test "assign non-existing attribute" do
|
||||||
model = Model.new
|
model = Model.new
|
||||||
error = assert_raises ActiveModel::AttributeAssignment::UnknownAttributeError do
|
error = assert_raises(ActiveModel::AttributeAssignment::UnknownAttributeError) do
|
||||||
model.assign_attributes(hz: 1)
|
model.assign_attributes(hz: 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -58,49 +59,49 @@ class AttributeAssignmentTest < ActiveModel::TestCase
|
||||||
|
|
||||||
test "assign private attribute" do
|
test "assign private attribute" do
|
||||||
model = Model.new
|
model = Model.new
|
||||||
assert_raises ActiveModel::AttributeAssignment::UnknownAttributeError do
|
assert_raises(ActiveModel::AttributeAssignment::UnknownAttributeError) do
|
||||||
model.assign_attributes(metadata: { a: 1 })
|
model.assign_attributes(metadata: { a: 1 })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "raises NoMethodError if raised in attribute writer" do
|
test "does not swallow errors raised in an attribute writer" do
|
||||||
assert_raises NoMethodError do
|
assert_raises(ErrorFromAttributeWriter) do
|
||||||
Model.new(broken_attribute: 1)
|
Model.new(broken_attribute: 1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "raises ArgumentError if non-hash object passed" do
|
test "an ArgumentError is raised if a non-hash-like obejct is passed" do
|
||||||
assert_raises ArgumentError do
|
assert_raises(ArgumentError) do
|
||||||
Model.new(1)
|
Model.new(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'forbidden attributes cannot be used for mass assignment' do
|
test "forbidden attributes cannot be used for mass assignment" do
|
||||||
params = ProtectedParams.new(name: 'Guille', description: 'm')
|
params = ProtectedParams.new(name: "Guille", description: "m")
|
||||||
|
|
||||||
assert_raises(ActiveModel::ForbiddenAttributesError) do
|
assert_raises(ActiveModel::ForbiddenAttributesError) do
|
||||||
Model.new(params)
|
Model.new(params)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'permitted attributes can be used for mass assignment' do
|
test "permitted attributes can be used for mass assignment" do
|
||||||
params = ProtectedParams.new(name: 'Guille', description: 'desc')
|
params = ProtectedParams.new(name: "Guille", description: "desc")
|
||||||
params.permit!
|
params.permit!
|
||||||
model = Model.new(params)
|
model = Model.new(params)
|
||||||
|
|
||||||
assert_equal 'Guille', model.name
|
assert_equal "Guille", model.name
|
||||||
assert_equal 'desc', model.description
|
assert_equal "desc", model.description
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'regular hash should still be used for mass assignment' do
|
test "regular hash should still be used for mass assignment" do
|
||||||
model = Model.new(name: 'Guille', description: 'm')
|
model = Model.new(name: "Guille", description: "m")
|
||||||
|
|
||||||
assert_equal 'Guille', model.name
|
assert_equal "Guille", model.name
|
||||||
assert_equal 'm', model.description
|
assert_equal "m", model.description
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'blank attributes should not raise' do
|
test "assigning no attributes should not raise, even if the hash is un-permitted" do
|
||||||
model = Model.new
|
model = Model.new
|
||||||
assert_nil model.assign_attributes(ProtectedParams.new({}))
|
assert_nil model.assign_attributes(ProtectedParams.new({}))
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -22,22 +22,10 @@ module ActiveRecord
|
||||||
assign_multiparameter_attributes(multi_parameter_attributes) unless multi_parameter_attributes.empty?
|
assign_multiparameter_attributes(multi_parameter_attributes) unless multi_parameter_attributes.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
# Allows you to set all the attributes by passing in a hash of attributes with
|
# Alias for `assign_attributes`. See +ActiveModel::AttributeAssignment+
|
||||||
# keys matching the attribute names (which again matches the column names).
|
def attributes=(attributes)
|
||||||
#
|
assign_attributes(attributes)
|
||||||
# If the passed hash responds to <tt>permitted?</tt> method and the return value
|
end
|
||||||
# of this method is +false+ an <tt>ActiveModel::ForbiddenAttributesError</tt>
|
|
||||||
# exception is raised.
|
|
||||||
#
|
|
||||||
# cat = Cat.new(name: "Gorby", status: "yawning")
|
|
||||||
# cat.attributes # => { "name" => "Gorby", "status" => "yawning", "created_at" => nil, "updated_at" => nil}
|
|
||||||
# cat.assign_attributes(status: "sleeping")
|
|
||||||
# cat.attributes # => { "name" => "Gorby", "status" => "sleeping", "created_at" => nil, "updated_at" => nil }
|
|
||||||
#
|
|
||||||
# New attributes will be persisted in the database when the object is saved.
|
|
||||||
#
|
|
||||||
# Aliased to <tt>assign_attributes</tt>.
|
|
||||||
alias attributes= assign_attributes
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue