Merge pull request #23583 from brchristian/penultimate

Array.second_to_last and Array.third_to_last access methods
This commit is contained in:
David Heinemeier Hansson 2016-02-10 20:45:12 +01:00
commit 074ff5ce14
10 changed files with 86 additions and 2 deletions

@ -1,3 +1,7 @@
* Add ActiveRecord `#second_to_last` and `#third_to_last` methods.
*Brian Christian*
* Added `numeric` helper into migrations. * Added `numeric` helper into migrations.
Example: Example:

@ -136,6 +136,14 @@ def forty_two(*args)
first_nth_or_last(:forty_two, *args) first_nth_or_last(:forty_two, *args)
end end
def third_to_last(*args)
first_nth_or_last(:third_to_last, *args)
end
def second_to_last(*args)
first_nth_or_last(:second_to_last, *args)
end
def last(*args) def last(*args)
first_nth_or_last(:last, *args) first_nth_or_last(:last, *args)
end end

@ -197,6 +197,16 @@ def forty_two(*args)
@association.forty_two(*args) @association.forty_two(*args)
end end
# Same as #first except returns only the third-to-last record.
def third_to_last(*args)
@association.third_to_last(*args)
end
# Same as #first except returns only the second-to-last record.
def second_to_last(*args)
@association.second_to_last(*args)
end
# Returns the last record, or the last +n+ records, from the collection. # Returns the last record, or the last +n+ records, from the collection.
# If the collection is empty, the first form returns +nil+, and the second # If the collection is empty, the first form returns +nil+, and the second
# form returns an empty array. # form returns an empty array.

@ -1,7 +1,7 @@
module ActiveRecord module ActiveRecord
module Querying module Querying
delegate :find, :take, :take!, :first, :first!, :last, :last!, :exists?, :any?, :many?, to: :all delegate :find, :take, :take!, :first, :first!, :last, :last!, :exists?, :any?, :many?, to: :all
delegate :second, :second!, :third, :third!, :fourth, :fourth!, :fifth, :fifth!, :forty_two, :forty_two!, to: :all delegate :second, :second!, :third, :third!, :fourth, :fourth!, :fifth, :fifth!, :forty_two, :forty_two!, :third_to_last, :third_to_last!, :second_to_last, :second_to_last!, to: :all
delegate :first_or_create, :first_or_create!, :first_or_initialize, to: :all delegate :first_or_create, :first_or_create!, :first_or_initialize, to: :all
delegate :find_or_create_by, :find_or_create_by!, :find_or_initialize_by, to: :all delegate :find_or_create_by, :find_or_create_by!, :find_or_initialize_by, to: :all
delegate :find_by, :find_by!, to: :all delegate :find_by, :find_by!, to: :all

@ -242,6 +242,38 @@ def forty_two!
find_nth! 41 find_nth! 41
end end
# Find the third-to-last record.
# If no order is defined it will order by primary key.
#
# Person.third_to_last # returns the third-to-last object fetched by SELECT * FROM people
# Person.offset(3).third_to_last # returns the third-to-last object from OFFSET 3
# Person.where(["user_name = :u", { u: user_name }]).third_to_last
def third_to_last
find_nth -3
end
# Same as #third_to_last but raises ActiveRecord::RecordNotFound if no record
# is found.
def third_to_last!
find_nth! -3
end
# Find the second-to-last record.
# If no order is defined it will order by primary key.
#
# Person.second_to_last # returns the second-to-last object fetched by SELECT * FROM people
# Person.offset(3).second_to_last # returns the second-to-last object from OFFSET 3
# Person.where(["user_name = :u", { u: user_name }]).second_to_last
def second_to_last
find_nth -2
end
# Same as #second_to_last but raises ActiveRecord::RecordNotFound if no record
# is found.
def second_to_last!
find_nth! -2
end
# Returns true if a record exists in the table that matches the +id+ or # Returns true if a record exists in the table that matches the +id+ or
# conditions given, or false otherwise. The argument can take six forms: # conditions given, or false otherwise. The argument can take six forms:
# #

@ -407,6 +407,16 @@ def test_no_sql_should_be_fired_if_association_already_loaded
bulbs.forty_two({}) bulbs.forty_two({})
end end
assert_no_queries do
bulbs.third_to_last()
bulbs.third_to_last({})
end
assert_no_queries do
bulbs.second_to_last()
bulbs.second_to_last({})
end
assert_no_queries do assert_no_queries do
bulbs.last() bulbs.last()
bulbs.last({}) bulbs.last({})

@ -1,3 +1,7 @@
* Add `Array#second_to_last` and `Array#third_to_last` methods.
*Brian Christian*
* Fix regression in `Hash#dig` for HashWithIndifferentAccess. * Fix regression in `Hash#dig` for HashWithIndifferentAccess.
*Jon Moss* *Jon Moss*

@ -73,4 +73,18 @@ def fifth
def forty_two def forty_two
self[41] self[41]
end end
# Equal to <tt>self[-3]</tt>.
#
# %w( a b c d e ).third_to_last # => "c"
def third_to_last
self[-3]
end
# Equal to <tt>self[-2]</tt>.
#
# %w( a b c d e ).second_to_last # => "d"
def second_to_last
self[-2]
end
end end

@ -26,6 +26,8 @@ def test_specific_accessor
assert_equal array[3], array.fourth assert_equal array[3], array.fourth
assert_equal array[4], array.fifth assert_equal array[4], array.fifth
assert_equal array[41], array.forty_two assert_equal array[41], array.forty_two
assert_equal array[-3], array.third_to_last
assert_equal array[-2], array.second_to_last
end end
def test_without def test_without

@ -2240,7 +2240,7 @@ Similarly, `from` returns the tail from the element at the passed index to the e
[].from(0) # => [] [].from(0) # => []
``` ```
The methods `second`, `third`, `fourth`, and `fifth` return the corresponding element (`first` is built-in). Thanks to social wisdom and positive constructiveness all around, `forty_two` is also available. The methods `second`, `third`, `fourth`, and `fifth` return the corresponding element, as do `second_to_last` and `third_to_last` (`first` and `last` are built-in). Thanks to social wisdom and positive constructiveness all around, `forty_two` is also available.
```ruby ```ruby
%w(a b c d).third # => "c" %w(a b c d).third # => "c"