Permalink
Browse files

Rename first_or_new to first_or_initialize.

For consistency with find_or_initialize_by. Also remove first_or_build
alias.
  • Loading branch information...
1 parent d3baa92 commit 11870117c6d9231b79e8125218728423e9dff207 @jonleighton jonleighton committed Sep 13, 2011
@@ -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]
@@ -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
@@ -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) ||
@@ -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?
@@ -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?
@@ -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?
@@ -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

2 comments on commit 1187011

@vijaydev
Member

@jonleighton any reason why you removed the first_or_build alias ?

@josevalim
Member

first_or_build doesn't make sense as we don't have something like find_or_build_by_email. build is used more as an association thing most of the time.

Please sign in to comment.