From a8c1fa4afd4abe6a5c975a164235600d1b8d8b4e Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Tue, 10 May 2011 23:35:01 +0100 Subject: [PATCH] Add test to specify that attributes from an association's conditions should be assigned without mass-assignment protection when a record is built on the association. --- .../has_many_associations_test.rb | 27 ++++++++++++++++--- activerecord/test/models/bulb.rb | 2 ++ activerecord/test/models/car.rb | 2 ++ activerecord/test/schema/schema.rb | 1 + 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 5a414c49f1..b149f5912f 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -79,8 +79,7 @@ def test_create_from_association_with_nil_values_should_work assert_equal 'defaulty', bulb.name end - def test_create_from_association_set_owner_attributes_by_passing_protection - Bulb.attr_protected :car_id + def test_association_keys_bypass_attribute_protection car = Car.create(:name => 'honda') bulb = car.bulbs.new @@ -100,8 +99,28 @@ def test_create_from_association_set_owner_attributes_by_passing_protection bulb = car.bulbs.create :car_id => car.id + 1 assert_equal car.id, bulb.car_id - ensure - Bulb.attr_protected :id + end + + def test_association_conditions_bypass_attribute_protection + car = Car.create(:name => 'honda') + + bulb = car.frickinawesome_bulbs.new + assert_equal true, bulb.frickinawesome? + + bulb = car.frickinawesome_bulbs.new(:frickinawesome => false) + assert_equal true, bulb.frickinawesome? + + bulb = car.frickinawesome_bulbs.build + assert_equal true, bulb.frickinawesome? + + bulb = car.frickinawesome_bulbs.build(:frickinawesome => false) + assert_equal true, bulb.frickinawesome? + + bulb = car.frickinawesome_bulbs.create + assert_equal true, bulb.frickinawesome? + + bulb = car.frickinawesome_bulbs.create(:frickinawesome => false) + assert_equal true, bulb.frickinawesome? end # When creating objects on the association, we must not do it within a scope (even though it diff --git a/activerecord/test/models/bulb.rb b/activerecord/test/models/bulb.rb index c68d008c26..643dcefed3 100644 --- a/activerecord/test/models/bulb.rb +++ b/activerecord/test/models/bulb.rb @@ -2,6 +2,8 @@ class Bulb < ActiveRecord::Base default_scope where(:name => 'defaulty') belongs_to :car + attr_protected :car_id, :frickinawesome + attr_reader :scope_after_initialize after_initialize :record_scope_after_initialize diff --git a/activerecord/test/models/car.rb b/activerecord/test/models/car.rb index b036f0f5c9..de1864345a 100644 --- a/activerecord/test/models/car.rb +++ b/activerecord/test/models/car.rb @@ -2,6 +2,8 @@ class Car < ActiveRecord::Base has_many :bulbs has_many :foo_bulbs, :class_name => "Bulb", :conditions => { :name => 'foo' } + has_many :frickinawesome_bulbs, :class_name => "Bulb", :conditions => { :frickinawesome => true } + has_many :tyres has_many :engines has_many :wheels, :as => :wheelable diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index c20304c0e2..c8a98f121d 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -89,6 +89,7 @@ def create_table(*args, &block) create_table :bulbs, :force => true do |t| t.integer :car_id t.string :name + t.boolean :frickinawesome end create_table "CamelCase", :force => true do |t| -- GitLab