From c74045cf0771ab51dfeca94b30c447cab6193e60 Mon Sep 17 00:00:00 2001 From: Brian Christian Date: Tue, 9 Feb 2016 16:02:08 -0800 Subject: allow Array.penultimate and Array.antepenultiate access methods --- .../associations/collection_association.rb | 8 ++++++ .../active_record/associations/collection_proxy.rb | 10 +++++++ activerecord/lib/active_record/querying.rb | 2 +- .../lib/active_record/relation/finder_methods.rb | 32 ++++++++++++++++++++++ .../associations/has_many_associations_test.rb | 10 +++++++ 5 files changed, 61 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 9f2c7292ea..6927f1fca3 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -136,6 +136,14 @@ module ActiveRecord first_nth_or_last(:forty_two, *args) end + def antepenultimate(*args) + first_nth_or_last(:antepenultimate, *args) + end + + def penultimate(*args) + first_nth_or_last(:penultimate, *args) + end + def last(*args) first_nth_or_last(:last, *args) end diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index fe693cfbb6..5f3d4c6735 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -197,6 +197,16 @@ module ActiveRecord @association.forty_two(*args) end + # Same as #first except returns only the third-to-last record. + def antepenultimate(*args) + @association.antepenultimate(*args) + end + + # Same as #first except returns only the second-to-last record. + def penultimate(*args) + @association.penultimate(*args) + end + # 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 # form returns an empty array. diff --git a/activerecord/lib/active_record/querying.rb b/activerecord/lib/active_record/querying.rb index 1f429cfd94..0a82832f56 100644 --- a/activerecord/lib/active_record/querying.rb +++ b/activerecord/lib/active_record/querying.rb @@ -1,7 +1,7 @@ module ActiveRecord module Querying 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!, :antepenultimate, :antepenultimate!, :penultimate, :penultimate!, 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_by, :find_by!, to: :all diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index d48bcea28a..90e05dc340 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -242,6 +242,38 @@ module ActiveRecord find_nth! 41 end + # Find the third-to-last record. + # If no order is defined it will order by primary key. + # + # Person.antepenultimate # returns the third-to-last object fetched by SELECT * FROM people + # Person.offset(3).antepenultimate # returns the third-to-last object from OFFSET 3 + # Person.where(["user_name = :u", { u: user_name }]).antepenultimate + def antepenultimate + find_nth -3 + end + + # Same as #antepenultimate but raises ActiveRecord::RecordNotFound if no record + # is found. + def antepenultimate! + find_nth! -3 + end + + # Find the second-to-last record. + # If no order is defined it will order by primary key. + # + # Person.penultimate # returns the second-to-last object fetched by SELECT * FROM people + # Person.offset(3).penultimate # returns the second-to-last object from OFFSET 3 + # Person.where(["user_name = :u", { u: user_name }]).penultimate + def penultimate + find_nth -2 + end + + # Same as #penultimate but raises ActiveRecord::RecordNotFound if no record + # is found. + def penultimate! + find_nth! -2 + end + # 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: # diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index ecaa521283..4e9f7c330a 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -407,6 +407,16 @@ class HasManyAssociationsTest < ActiveRecord::TestCase bulbs.forty_two({}) end + assert_no_queries do + bulbs.antepenultimate() + bulbs.antepenultimate({}) + end + + assert_no_queries do + bulbs.penultimate() + bulbs.penultimate({}) + end + assert_no_queries do bulbs.last() bulbs.last({}) -- cgit v1.2.3 From e8aeda2ab39b5d416a72edfa74e0fb17721eb6f9 Mon Sep 17 00:00:00 2001 From: Brian Christian Date: Wed, 10 Feb 2016 10:10:38 -0800 Subject: rename to 'second_to_last' and 'third_to_last' --- .../associations/collection_association.rb | 8 ++++---- .../active_record/associations/collection_proxy.rb | 8 ++++---- activerecord/lib/active_record/querying.rb | 2 +- .../lib/active_record/relation/finder_methods.rb | 24 +++++++++++----------- .../associations/has_many_associations_test.rb | 8 ++++---- 5 files changed, 25 insertions(+), 25 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 6927f1fca3..2dca6b612e 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -136,12 +136,12 @@ module ActiveRecord first_nth_or_last(:forty_two, *args) end - def antepenultimate(*args) - first_nth_or_last(:antepenultimate, *args) + def third_to_last(*args) + first_nth_or_last(:third_to_last, *args) end - def penultimate(*args) - first_nth_or_last(:penultimate, *args) + def second_to_last(*args) + first_nth_or_last(:second_to_last, *args) end def last(*args) diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index 5f3d4c6735..2a9627a474 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -198,13 +198,13 @@ module ActiveRecord end # Same as #first except returns only the third-to-last record. - def antepenultimate(*args) - @association.antepenultimate(*args) + def third_to_last(*args) + @association.third_to_last(*args) end # Same as #first except returns only the second-to-last record. - def penultimate(*args) - @association.penultimate(*args) + def second_to_last(*args) + @association.second_to_last(*args) end # Returns the last record, or the last +n+ records, from the collection. diff --git a/activerecord/lib/active_record/querying.rb b/activerecord/lib/active_record/querying.rb index 0a82832f56..5259797223 100644 --- a/activerecord/lib/active_record/querying.rb +++ b/activerecord/lib/active_record/querying.rb @@ -1,7 +1,7 @@ module ActiveRecord module Querying 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!, :antepenultimate, :antepenultimate!, :penultimate, :penultimate!, 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 :find_or_create_by, :find_or_create_by!, :find_or_initialize_by, to: :all delegate :find_by, :find_by!, to: :all diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 90e05dc340..90a6a466fd 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -245,32 +245,32 @@ module ActiveRecord # Find the third-to-last record. # If no order is defined it will order by primary key. # - # Person.antepenultimate # returns the third-to-last object fetched by SELECT * FROM people - # Person.offset(3).antepenultimate # returns the third-to-last object from OFFSET 3 - # Person.where(["user_name = :u", { u: user_name }]).antepenultimate - def antepenultimate + # 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 #antepenultimate but raises ActiveRecord::RecordNotFound if no record + # Same as #third_to_last but raises ActiveRecord::RecordNotFound if no record # is found. - def antepenultimate! + 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.penultimate # returns the second-to-last object fetched by SELECT * FROM people - # Person.offset(3).penultimate # returns the second-to-last object from OFFSET 3 - # Person.where(["user_name = :u", { u: user_name }]).penultimate - def penultimate + # 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 #penultimate but raises ActiveRecord::RecordNotFound if no record + # Same as #second_to_last but raises ActiveRecord::RecordNotFound if no record # is found. - def penultimate! + def second_to_last! find_nth! -2 end diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 4e9f7c330a..e975f4fbdd 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -408,13 +408,13 @@ class HasManyAssociationsTest < ActiveRecord::TestCase end assert_no_queries do - bulbs.antepenultimate() - bulbs.antepenultimate({}) + bulbs.third_to_last() + bulbs.third_to_last({}) end assert_no_queries do - bulbs.penultimate() - bulbs.penultimate({}) + bulbs.second_to_last() + bulbs.second_to_last({}) end assert_no_queries do -- cgit v1.2.3 From eaa1efe7d0b4e0f5682453b1ddfa186a292ddc86 Mon Sep 17 00:00:00 2001 From: Brian Christian Date: Wed, 10 Feb 2016 10:31:44 -0800 Subject: include activerecord and activesupport CHANGELOG entries --- activerecord/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index a4cf6023e1..be96f9fa2a 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,7 @@ +* Add ActiveRecord `#second_to_last` and `#third_to_last` methods. + + *Brian Christian* + * Added `numeric` helper into migrations. Example: -- cgit v1.2.3