Skip to content

Commit

Permalink
Rename first_or_new to first_or_initialize.
Browse files Browse the repository at this point in the history
For consistency with find_or_initialize_by. Also remove first_or_build
alias.
  • Loading branch information
jonleighton committed Sep 13, 2011
1 parent d3baa92 commit 1187011
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 28 deletions.
8 changes: 5 additions & 3 deletions activerecord/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ Wed Sep 7 15:25:02 2011 Aaron Patterson <aaron@tenderlovemaking.com>
keys are per process id.
* lib/active_record/connection_adapters/sqlite_adapter.rb: ditto

* Add first_or_create, first_or_create!, first_or_build and first_or_new methods to Active Record. This is a better approach over the old find_or_create_by dynamic methods because it's clearer which arguments are used to find the record and which are used to create it:
* Add first_or_create, first_or_create!, first_or_initialize methods to Active Record. This is a
better approach over the old find_or_create_by dynamic methods because it's clearer which
arguments are used to find the record and which are used to create it:

User.where(:first_name => "Scarlett").first_or_create!(:last_name => "Johansson")

User.where(:first_name => "Scarlett").first_or_create!(:last_name => "Johansson", :hot => true)

[Andrés Mejía]

* Support bulk change_table in mysql2 adapter, as well as the mysql one. [Jon Leighton]
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ class Base

class << self # Class methods
delegate :find, :first, :first!, :last, :last!, :all, :exists?, :any?, :many?, :to => :scoped
delegate :first_or_create, :first_or_create!, :first_or_new, :first_or_build, :to => :scoped
delegate :first_or_create, :first_or_create!, :first_or_initialize, :to => :scoped
delegate :destroy, :destroy_all, :delete, :delete_all, :update, :update_all, :to => :scoped
delegate :find_each, :find_in_batches, :to => :scoped
delegate :select, :group, :order, :except, :reorder, :limit, :offset, :joins, :where, :preload, :eager_load, :includes, :from, :lock, :readonly, :having, :create_with, :to => :scoped
Expand Down
3 changes: 1 addition & 2 deletions activerecord/lib/active_record/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,9 @@ def first_or_create!(attributes = nil, options = {}, &block)
# Like <tt>first_or_create</tt> but calls <tt>new</tt> instead of <tt>create</tt>.
#
# Expects arguments in the same format as <tt>Base.new</tt>.
def first_or_new(attributes = nil, options = {}, &block)
def first_or_initialize(attributes = nil, options = {}, &block)
first || new(attributes, options, &block)
end
alias :first_or_build :first_or_new

def respond_to?(method, include_private = false)
arel.respond_to?(method, include_private) ||
Expand Down
12 changes: 2 additions & 10 deletions activerecord/test/cases/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,16 +294,8 @@ def test_first_or_create_bang
assert_equal parrot, the_same_parrot
end

def test_first_or_new
parrot = Bird.first_or_new(:color => 'green', :name => 'parrot')
assert_kind_of Bird, parrot
assert !parrot.persisted?
assert parrot.new_record?
assert parrot.valid?
end

def test_first_or_build
parrot = Bird.first_or_build(:color => 'green', :name => 'parrot')
def test_first_or_initialize
parrot = Bird.first_or_initialize(:color => 'green', :name => 'parrot')
assert_kind_of Bird, parrot
assert !parrot.persisted?
assert parrot.new_record?
Expand Down
18 changes: 6 additions & 12 deletions activerecord/test/cases/relations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -956,8 +956,8 @@ def test_first_or_create_with_invalid_array
assert_raises(ActiveRecord::RecordInvalid) { Bird.where(:color => 'green').first_or_create!([ {:name => 'parrot'}, {:pirate_id => 1} ]) }
end

def test_first_or_new
parrot = Bird.where(:color => 'green').first_or_new(:name => 'parrot')
def test_first_or_initialize
parrot = Bird.where(:color => 'green').first_or_initialize(:name => 'parrot')
assert_kind_of Bird, parrot
assert !parrot.persisted?
assert parrot.valid?
Expand All @@ -966,17 +966,17 @@ def test_first_or_new
assert_equal 'green', parrot.color
end

def test_first_or_new_with_no_parameters
parrot = Bird.where(:color => 'green').first_or_new
def test_first_or_initialize_with_no_parameters
parrot = Bird.where(:color => 'green').first_or_initialize
assert_kind_of Bird, parrot
assert !parrot.persisted?
assert !parrot.valid?
assert parrot.new_record?
assert_equal 'green', parrot.color
end

def test_first_or_new_with_block
parrot = Bird.where(:color => 'green').first_or_new { |bird| bird.name = 'parrot' }
def test_first_or_initialize_with_block
parrot = Bird.where(:color => 'green').first_or_initialize { |bird| bird.name = 'parrot' }
assert_kind_of Bird, parrot
assert !parrot.persisted?
assert parrot.valid?
Expand All @@ -985,12 +985,6 @@ def test_first_or_new_with_block
assert_equal 'parrot', parrot.name
end

def test_first_or_build_is_alias_for_first_or_new
birds = Bird.scoped
assert birds.respond_to?(:first_or_build)
assert_equal birds.method(:first_or_new), birds.method(:first_or_build)
end

def test_explicit_create_scope
hens = Bird.where(:name => 'hen')
assert_equal 'hen', hens.new.name
Expand Down

0 comments on commit 1187011

Please sign in to comment.