提交 a8c1fa4a 编写于 作者: J Jon Leighton

Add test to specify that attributes from an association's conditions should be...

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.
上级 93a85ce3
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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|
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册