Skip to content

Commit

Permalink
Add block setting of attributes to singular associations
Browse files Browse the repository at this point in the history
  • Loading branch information
pixeltrix committed May 17, 2011
1 parent be199a1 commit 3773aa4
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 11 deletions.
10 changes: 10 additions & 0 deletions activerecord/CHANGELOG
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 21 additions & 0 deletions activerecord/test/cases/associations/has_one_associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3773aa4

Please sign in to comment.