From d5450a6659a33ef08a170afcaae084023bcd275b Mon Sep 17 00:00:00 2001 From: Dmitry Polushkin Date: Sun, 9 Jun 2013 19:36:39 +0200 Subject: Refactored ActiveRecord `first` method to get rid of duplication. --- .../lib/active_record/relation/finder_methods.rb | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'activerecord/lib/active_record/relation') diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 7ddaea1bb0..cf71df8fba 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -81,11 +81,7 @@ module ActiveRecord # Person.first(3) # returns the first three objects fetched by SELECT * FROM people LIMIT 3 def first(limit = nil) if limit - if order_values.empty? && primary_key - order(arel_table[primary_key].asc).limit(limit).to_a - else - limit(limit).to_a - end + find_first_records(order_values, limit) else find_first end @@ -307,12 +303,15 @@ module ActiveRecord if loaded? @records.first else - @first ||= - if with_default_scope.order_values.empty? && primary_key - order(arel_table[primary_key].asc).limit(1).to_a.first - else - limit(1).to_a.first - end + @first ||= find_first_records(with_default_scope.order_values, 1).first + end + end + + def find_first_records(order_values, limit) + if order_values.empty? && primary_key + order(arel_table[primary_key].asc).limit(limit).to_a + else + limit(limit).to_a end end -- cgit v1.2.3 From e720b50fb1ff841970b2f1198996144c35b48461 Mon Sep 17 00:00:00 2001 From: Dmitry Polushkin Date: Mon, 10 Jun 2013 14:38:32 +0200 Subject: rename method `find_first_records` to `find_first_with_limit` --- activerecord/lib/active_record/relation/finder_methods.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record/relation') diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index cf71df8fba..f240d0aaa9 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -81,7 +81,7 @@ module ActiveRecord # Person.first(3) # returns the first three objects fetched by SELECT * FROM people LIMIT 3 def first(limit = nil) if limit - find_first_records(order_values, limit) + find_first_with_limit(order_values, limit) else find_first end @@ -303,11 +303,11 @@ module ActiveRecord if loaded? @records.first else - @first ||= find_first_records(with_default_scope.order_values, 1).first + @first ||= find_first_with_limit(with_default_scope.order_values, 1).first end end - def find_first_records(order_values, limit) + def find_first_with_limit(order_values, limit) if order_values.empty? && primary_key order(arel_table[primary_key].asc).limit(limit).to_a else -- cgit v1.2.3