From 2606fb339797a99c50e531105fc92071cef3db01 Mon Sep 17 00:00:00 2001 From: Bogdan Gusiev Date: Fri, 23 Jan 2015 13:57:34 +0200 Subject: Extracted `ActiveRecord::AttributeAssignment` to `ActiveModel::AttributesAssignment` Allows to use it for any object as an includable module. --- .../test/cases/attribute_assignment_test.rb | 106 +++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 activemodel/test/cases/attribute_assignment_test.rb (limited to 'activemodel/test') diff --git a/activemodel/test/cases/attribute_assignment_test.rb b/activemodel/test/cases/attribute_assignment_test.rb new file mode 100644 index 0000000000..5c1be6a456 --- /dev/null +++ b/activemodel/test/cases/attribute_assignment_test.rb @@ -0,0 +1,106 @@ +require 'cases/helper' +require 'active_support/hash_with_indifferent_access' + +class AttributeAssignmentTest < ActiveModel::TestCase + + class Model + include ActiveModel::AttributeAssignment + + attr_accessor :name, :description + + def initialize(attributes = {}) + assign_attributes(attributes) + end + + def broken_attribute=(value) + non_existing_method(value) + end + + private + def metadata=(data) + @metadata = data + end + end + + class ProtectedParams < ActiveSupport::HashWithIndifferentAccess + def permit! + @permitted = true + end + + def permitted? + @permitted ||= false + end + + def dup + super.tap do |duplicate| + duplicate.instance_variable_set :@permitted, permitted? + end + end + end + + test "simple assignment" do + model = Model.new + + model.assign_attributes(name: 'hello', description: 'world') + assert_equal 'hello', model.name + assert_equal 'world', model.description + end + + test "assign non-existing attribute" do + model = Model.new + error = assert_raises ActiveModel::AttributeAssignment::UnknownAttributeError do + 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 + assert_raises ActiveModel::AttributeAssignment::UnknownAttributeError do + model.assign_attributes(metadata: { a: 1 }) + end + end + + test "raises NoMethodError if raised in attribute writer" do + assert_raises NoMethodError do + Model.new(broken_attribute: 1) + end + end + + test "raises ArgumentError if non-hash object passed" do + assert_raises ArgumentError do + Model.new(1) + end + end + + test 'forbidden attributes cannot be used for mass assignment' do + params = ProtectedParams.new(name: 'Guille', description: 'm') + assert_raises(ActiveModel::ForbiddenAttributesError) do + Model.new(params) + end + end + + test 'permitted attributes can be used for mass assignment' do + params = ProtectedParams.new(name: 'Guille', description: 'desc') + params.permit! + model = Model.new(params) + + assert_equal 'Guille', model.name + assert_equal 'desc', model.description + end + + test 'regular hash should still be used for mass assignment' do + model = Model.new(name: 'Guille', description: 'm') + + assert_equal 'Guille', model.name + assert_equal 'm', model.description + end + + test 'blank attributes should not raise' do + model = Model.new + assert_nil model.assign_attributes(ProtectedParams.new({})) + end + +end -- cgit v1.2.3 From a225d4bec51778d99ccba5f0d6700dd00d2474f4 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Fri, 23 Jan 2015 14:51:59 -0700 Subject: =?UTF-8?q?=E2=9C=82=EF=B8=8F=20and=20=F0=9F=92=85=20for=20#10776?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Minor style changes across the board. Changed an alias to an explicit method declaration, since the alias will not be documented otherwise. --- .../test/cases/attribute_assignment_test.rb | 59 +++++++++++----------- 1 file changed, 30 insertions(+), 29 deletions(-) (limited to 'activemodel/test') diff --git a/activemodel/test/cases/attribute_assignment_test.rb b/activemodel/test/cases/attribute_assignment_test.rb index 5c1be6a456..402caf21f7 100644 --- a/activemodel/test/cases/attribute_assignment_test.rb +++ b/activemodel/test/cases/attribute_assignment_test.rb @@ -1,8 +1,7 @@ -require 'cases/helper' -require 'active_support/hash_with_indifferent_access' +require "cases/helper" +require "active_support/hash_with_indifferent_access" class AttributeAssignmentTest < ActiveModel::TestCase - class Model include ActiveModel::AttributeAssignment @@ -13,13 +12,15 @@ class AttributeAssignmentTest < ActiveModel::TestCase end def broken_attribute=(value) - non_existing_method(value) + raise ErrorFromAttributeWriter end - private - def metadata=(data) - @metadata = data - end + protected + + attr_writer :metadata + end + + class ErrorFromAttributeWriter < StandardError end class ProtectedParams < ActiveSupport::HashWithIndifferentAccess @@ -41,14 +42,14 @@ class AttributeAssignmentTest < ActiveModel::TestCase test "simple assignment" do model = Model.new - model.assign_attributes(name: 'hello', description: 'world') - assert_equal 'hello', model.name - assert_equal 'world', model.description + model.assign_attributes(name: "hello", description: "world") + assert_equal "hello", model.name + assert_equal "world", model.description end test "assign non-existing attribute" do model = Model.new - error = assert_raises ActiveModel::AttributeAssignment::UnknownAttributeError do + error = assert_raises(ActiveModel::AttributeAssignment::UnknownAttributeError) do model.assign_attributes(hz: 1) end @@ -58,49 +59,49 @@ class AttributeAssignmentTest < ActiveModel::TestCase test "assign private attribute" do model = Model.new - assert_raises ActiveModel::AttributeAssignment::UnknownAttributeError do + assert_raises(ActiveModel::AttributeAssignment::UnknownAttributeError) do model.assign_attributes(metadata: { a: 1 }) end end - test "raises NoMethodError if raised in attribute writer" do - assert_raises NoMethodError do + test "does not swallow errors raised in an attribute writer" do + assert_raises(ErrorFromAttributeWriter) do Model.new(broken_attribute: 1) end end - test "raises ArgumentError if non-hash object passed" do - assert_raises ArgumentError do + test "an ArgumentError is raised if a non-hash-like obejct is passed" do + assert_raises(ArgumentError) do Model.new(1) end end - test 'forbidden attributes cannot be used for mass assignment' do - params = ProtectedParams.new(name: 'Guille', description: 'm') + test "forbidden attributes cannot be used for mass assignment" do + params = ProtectedParams.new(name: "Guille", description: "m") + assert_raises(ActiveModel::ForbiddenAttributesError) do Model.new(params) end end - test 'permitted attributes can be used for mass assignment' do - params = ProtectedParams.new(name: 'Guille', description: 'desc') + test "permitted attributes can be used for mass assignment" do + params = ProtectedParams.new(name: "Guille", description: "desc") params.permit! model = Model.new(params) - assert_equal 'Guille', model.name - assert_equal 'desc', model.description + assert_equal "Guille", model.name + assert_equal "desc", model.description end - test 'regular hash should still be used for mass assignment' do - model = Model.new(name: 'Guille', description: 'm') + test "regular hash should still be used for mass assignment" do + model = Model.new(name: "Guille", description: "m") - assert_equal 'Guille', model.name - assert_equal 'm', model.description + assert_equal "Guille", model.name + assert_equal "m", model.description 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 assert_nil model.assign_attributes(ProtectedParams.new({})) end - end -- cgit v1.2.3