提交 64affc93 编写于 作者: S Santiago Pastorino

Merge pull request #2236 from cldwalker/after_initialize

Fix after_initialize and Base.create edge case
......@@ -504,8 +504,7 @@ def create(attributes = nil, options = {}, &block)
if attributes.is_a?(Array)
attributes.collect { |attr| create(attr, options, &block) }
else
object = new(attributes, options)
yield(object) if block_given?
object = new(attributes, options, &block)
object.save
object
end
......
......@@ -21,6 +21,7 @@
require 'models/person'
require 'models/edge'
require 'models/joke'
require 'models/bulb'
require 'rexml/document'
require 'active_support/core_ext/exception'
......@@ -260,6 +261,18 @@ def test_initialize_with_invalid_attribute
end
end
def test_create_after_initialize_without_block
cb = CustomBulb.create(:name => 'Dude')
assert_equal('Dude', cb.name)
assert_equal(true, cb.frickinawesome)
end
def test_create_after_initialize_with_block
cb = CustomBulb.create {|c| c.name = 'Dude' }
assert_equal('Dude', cb.name)
assert_equal(true, cb.frickinawesome)
end
def test_load
topics = Topic.find(:all, :order => 'id')
assert_equal(4, topics.size)
......
......@@ -33,4 +33,9 @@ def self.new(attributes = {}, options = {}, &block)
end
class CustomBulb < Bulb
after_initialize :set_awesomeness
def set_awesomeness
self.frickinawesome = true if name == 'Dude'
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册