1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add block setting of attributes to singular associations

This commit is contained in:
Andrew White 2011-05-17 22:15:47 +01:00
parent be199a1d53
commit 3773aa4fbb
5 changed files with 64 additions and 11 deletions

View file

@ -1,5 +1,15 @@
*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

View file

@ -29,16 +29,16 @@ module ActiveRecord::Associations::Builder
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

View file

@ -17,17 +17,18 @@ module ActiveRecord
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

View file

@ -626,4 +626,25 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
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

View file

@ -426,4 +426,25 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
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