Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix after_initialize and Base.create edge case #2236

Merged
merged 3 commits into from Jul 25, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions activerecord/test/cases/base_test.rb
Expand Up @@ -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'

Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/models/bulb.rb
Expand Up @@ -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