提交 3773aa4f 编写于 作者: A Andrew White

Add block setting of attributes to singular associations

上级 be199a1d
*Rails 3.1.0 (unreleased)*
* Add block setting of attributes to singular associations:
class User < ActiveRecord::Base
has_one :account
end
user.build_account{ |a| a.credit_limit => 100.0 }
The block is called after the instance has been initialized. [Andrew White]
* Add ActiveRecord::Base.attribute_names to return a list of attribute names. This will return an empty array if the model is abstract or table does not exists. [Prem Sichanugrist]
* CSV Fixtures are deprecated and support will be removed in Rails 3.2.0
......
......@@ -29,16 +29,16 @@ def define_readers
def define_constructors
name = self.name
model.redefine_method("build_#{name}") do |*params|
association(name).build(*params)
model.redefine_method("build_#{name}") do |*params, &block|
association(name).build(*params, &block)
end
model.redefine_method("create_#{name}") do |*params|
association(name).create(*params)
model.redefine_method("create_#{name}") do |*params, &block|
association(name).create(*params, &block)
end
model.redefine_method("create_#{name}!") do |*params|
association(name).create!(*params)
model.redefine_method("create_#{name}!") do |*params, &block|
association(name).create!(*params, &block)
end
end
end
......
......@@ -17,17 +17,18 @@ def writer(record)
replace(record)
end
def create(attributes = {}, options = {})
build(attributes, options).tap { |record| record.save }
def create(attributes = {}, options = {}, &block)
build(attributes, options, &block).tap { |record| record.save }
end
def create!(attributes = {}, options = {})
build(attributes, options).tap { |record| record.save! }
def create!(attributes = {}, options = {}, &block)
build(attributes, options, &block).tap { |record| record.save! }
end
def build(attributes = {}, options = {})
def build(attributes = {}, options = {}, &block)
record = reflection.build_association(attributes, options)
record.assign_attributes(create_scope.except(*record.changed), :without_protection => true)
yield(record) if block_given?
set_new_record(record)
record
end
......
......@@ -626,4 +626,25 @@ def test_create_bang_with_conditions
assert_equal "Bob", firm.name
end
def test_build_with_block
client = Client.create(:name => 'Client Company')
firm = client.build_firm{ |f| f.name = 'Agency Company' }
assert_equal 'Agency Company', firm.name
end
def test_create_with_block
client = Client.create(:name => 'Client Company')
firm = client.create_firm{ |f| f.name = 'Agency Company' }
assert_equal 'Agency Company', firm.name
end
def test_create_bang_with_block
client = Client.create(:name => 'Client Company')
firm = client.create_firm!{ |f| f.name = 'Agency Company' }
assert_equal 'Agency Company', firm.name
end
end
......@@ -426,4 +426,25 @@ def test_new_is_called_with_attributes_and_options
bulb = car.build_bulb({ :bulb_type => :custom }, :as => :admin)
assert_equal CustomBulb, bulb.class
end
def test_build_with_block
car = Car.create(:name => 'honda')
bulb = car.build_bulb{ |b| b.color = 'Red' }
assert_equal 'RED!', bulb.color
end
def test_create_with_block
car = Car.create(:name => 'honda')
bulb = car.create_bulb{ |b| b.color = 'Red' }
assert_equal 'RED!', bulb.color
end
def test_create_bang_with_block
car = Car.create(:name => 'honda')
bulb = car.create_bulb!{ |b| b.color = 'Red' }
assert_equal 'RED!', bulb.color
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册