1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Rename first_or_new to first_or_initialize.

For consistency with find_or_initialize_by. Also remove first_or_build
alias.
This commit is contained in:
Jon Leighton 2011-09-13 19:09:01 +01:00
parent d3baa92831
commit 11870117c6
5 changed files with 15 additions and 28 deletions

View file

@ -4,9 +4,11 @@ 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", :hot => true)
User.where(:first_name => "Scarlett").first_or_create!(:last_name => "Johansson")
[Andrés Mejía]

View file

@ -442,7 +442,7 @@ module ActiveRecord #:nodoc:
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

View file

@ -132,10 +132,9 @@ module ActiveRecord
# 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) ||

View file

@ -294,16 +294,8 @@ class BasicsTest < ActiveRecord::TestCase
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?

View file

@ -956,8 +956,8 @@ class RelationTest < ActiveRecord::TestCase
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,8 +966,8 @@ class RelationTest < ActiveRecord::TestCase
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?
@ -975,8 +975,8 @@ class RelationTest < ActiveRecord::TestCase
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 @@ class RelationTest < ActiveRecord::TestCase
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