From 0405d5a7e95776f9adf5b8ff064300898d89b43a Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sat, 12 Apr 2014 09:42:02 -0700 Subject: remove branching logic from calls to find_nth --- .../lib/active_record/relation/finder_methods.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'activerecord/lib/active_record/relation/finder_methods.rb') diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index c2b9dc08fe..6274ca15ca 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -131,7 +131,7 @@ module ActiveRecord if limit find_nth_with_limit(offset_value, limit) else - find_nth(:first, offset_value) + find_nth(:first, offset_index) end end @@ -181,7 +181,7 @@ module ActiveRecord # Person.offset(3).second # returns the second object from OFFSET 3 (which is OFFSET 4) # Person.where(["user_name = :u", { u: user_name }]).second def second - find_nth(:second, offset_value ? offset_value + 1 : 1) + find_nth(:second, offset_index + 1) end # Same as +second+ but raises ActiveRecord::RecordNotFound if no record @@ -197,7 +197,7 @@ module ActiveRecord # Person.offset(3).third # returns the third object from OFFSET 3 (which is OFFSET 5) # Person.where(["user_name = :u", { u: user_name }]).third def third - find_nth(:third, offset_value ? offset_value + 2 : 2) + find_nth(:third, offset_index + 2) end # Same as +third+ but raises ActiveRecord::RecordNotFound if no record @@ -213,7 +213,7 @@ module ActiveRecord # Person.offset(3).fourth # returns the fourth object from OFFSET 3 (which is OFFSET 6) # Person.where(["user_name = :u", { u: user_name }]).fourth def fourth - find_nth(:fourth, offset_value ? offset_value + 3 : 3) + find_nth(:fourth, offset_index + 3) end # Same as +fourth+ but raises ActiveRecord::RecordNotFound if no record @@ -229,7 +229,7 @@ module ActiveRecord # Person.offset(3).fifth # returns the fifth object from OFFSET 3 (which is OFFSET 7) # Person.where(["user_name = :u", { u: user_name }]).fifth def fifth - find_nth(:fifth, offset_value ? offset_value + 4 : 4) + find_nth(:fifth, offset_index + 4) end # Same as +fifth+ but raises ActiveRecord::RecordNotFound if no record @@ -245,7 +245,7 @@ module ActiveRecord # Person.offset(3).forty_two # returns the fifth object from OFFSET 3 (which is OFFSET 44) # Person.where(["user_name = :u", { u: user_name }]).forty_two def forty_two - find_nth(:forty_two, offset_value ? offset_value + 41 : 41) + find_nth(:forty_two, offset_index + 41) end # Same as +forty_two+ but raises ActiveRecord::RecordNotFound if no record @@ -334,6 +334,10 @@ module ActiveRecord private + def offset_index + offset_value || 0 + end + def find_with_associations join_dependency = construct_join_dependency -- cgit v1.2.3 From 8c2b79d0c8f3533f8045baf647317c4ff33b0f26 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sat, 12 Apr 2014 09:45:39 -0700 Subject: only add the offset and index when we need to --- .../lib/active_record/relation/finder_methods.rb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'activerecord/lib/active_record/relation/finder_methods.rb') diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 6274ca15ca..8daea2c13e 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -131,7 +131,7 @@ module ActiveRecord if limit find_nth_with_limit(offset_value, limit) else - find_nth(:first, offset_index) + find_nth(0, offset_index) end end @@ -181,7 +181,7 @@ module ActiveRecord # Person.offset(3).second # returns the second object from OFFSET 3 (which is OFFSET 4) # Person.where(["user_name = :u", { u: user_name }]).second def second - find_nth(:second, offset_index + 1) + find_nth(1, offset_index) end # Same as +second+ but raises ActiveRecord::RecordNotFound if no record @@ -197,7 +197,7 @@ module ActiveRecord # Person.offset(3).third # returns the third object from OFFSET 3 (which is OFFSET 5) # Person.where(["user_name = :u", { u: user_name }]).third def third - find_nth(:third, offset_index + 2) + find_nth(2, offset_index) end # Same as +third+ but raises ActiveRecord::RecordNotFound if no record @@ -213,7 +213,7 @@ module ActiveRecord # Person.offset(3).fourth # returns the fourth object from OFFSET 3 (which is OFFSET 6) # Person.where(["user_name = :u", { u: user_name }]).fourth def fourth - find_nth(:fourth, offset_index + 3) + find_nth(3, offset_index) end # Same as +fourth+ but raises ActiveRecord::RecordNotFound if no record @@ -229,7 +229,7 @@ module ActiveRecord # Person.offset(3).fifth # returns the fifth object from OFFSET 3 (which is OFFSET 7) # Person.where(["user_name = :u", { u: user_name }]).fifth def fifth - find_nth(:fifth, offset_index + 4) + find_nth(4, offset_index) end # Same as +fifth+ but raises ActiveRecord::RecordNotFound if no record @@ -245,7 +245,7 @@ module ActiveRecord # Person.offset(3).forty_two # returns the fifth object from OFFSET 3 (which is OFFSET 44) # Person.where(["user_name = :u", { u: user_name }]).forty_two def forty_two - find_nth(:forty_two, offset_index + 41) + find_nth(41, offset_index) end # Same as +forty_two+ but raises ActiveRecord::RecordNotFound if no record @@ -472,10 +472,11 @@ module ActiveRecord end end - def find_nth(ordinal, offset) + def find_nth(index, offset) if loaded? - @records.send(ordinal) + @records[index] else + offset += index @offsets[offset] ||= find_nth_with_limit(offset, 1).first end end -- cgit v1.2.3 From 711a882f106385e248135c84ea9a6a827f0bd1e1 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sat, 12 Apr 2014 10:14:12 -0700 Subject: don't bother with an offset if the offset is zero we're guaranteed to pass a numeric value for offset, so if it's zero, just don't add an offset to the query --- activerecord/lib/active_record/relation/finder_methods.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'activerecord/lib/active_record/relation/finder_methods.rb') diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 8daea2c13e..7af4b29ebc 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -129,7 +129,7 @@ module ActiveRecord # def first(limit = nil) if limit - find_nth_with_limit(offset_value, limit) + find_nth_with_limit(offset_index, limit) else find_nth(0, offset_index) end @@ -482,11 +482,14 @@ module ActiveRecord end def find_nth_with_limit(offset, limit) - if order_values.empty? && primary_key - order(arel_table[primary_key].asc).limit(limit).offset(offset).to_a - else - limit(limit).offset(offset).to_a - end + relation = if order_values.empty? && primary_key + order(arel_table[primary_key].asc) + else + self + end + + relation = relation.offset(offset) unless offset.zero? + relation.limit(limit).to_a end def find_last -- cgit v1.2.3