aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2015-06-19 15:35:35 -0600
committerSean Griffin <sean@thoughtbot.com>2015-06-19 15:35:35 -0600
commitb644964b2b555798fc4b94d384b98438db863b3f (patch)
treeaf3a39838508d3cbce032b20b58889a62c607a05 /activerecord/lib/active_record/relation.rb
parent7d14bd3ff553d6aba11a50b43773bc21ae20f31e (diff)
downloadrails-b644964b2b555798fc4b94d384b98438db863b3f.tar.gz
rails-b644964b2b555798fc4b94d384b98438db863b3f.tar.bz2
rails-b644964b2b555798fc4b94d384b98438db863b3f.zip
Include `Enumerable` in `ActiveRecord::Relation`
After discussing, we've decided it makes more sense to include it. We're already forwarding every conflicting method to `to_a`, and there's no conflation of concerns. `Enumerable` has no mutating methods, and it just allows us to simplify the code. No existing methods will have a change in behavior. Un-overridden Enumerable methods will simply delegate to `each`. [Sean Griffin & bogdan]
Diffstat (limited to 'activerecord/lib/active_record/relation.rb')
-rw-r--r--activerecord/lib/active_record/relation.rb29
1 files changed, 9 insertions, 20 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index 7d37313058..e4df122af6 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -15,6 +15,7 @@ module ActiveRecord
VALUE_METHODS = MULTI_VALUE_METHODS + SINGLE_VALUE_METHODS + CLAUSE_METHODS
+ include Enumerable
include FinderMethods, Calculations, SpawnMethods, QueryMethods, Batches, Explain, Delegation
attr_reader :table, :klass, :loaded, :predicate_builder
@@ -275,38 +276,26 @@ module ActiveRecord
# Returns true if there are no records.
def none?
- if block_given?
- to_a.none? { |*block_args| yield(*block_args) }
- else
- empty?
- end
+ return super if block_given?
+ empty?
end
# Returns true if there are any records.
def any?
- if block_given?
- to_a.any? { |*block_args| yield(*block_args) }
- else
- !empty?
- end
+ return super if block_given?
+ !empty?
end
# Returns true if there is exactly one record.
def one?
- if block_given?
- to_a.one? { |*block_args| yield(*block_args) }
- else
- limit_value ? to_a.one? : size == 1
- end
+ return super if block_given?
+ limit_value ? to_a.one? : size == 1
end
# Returns true if there is more than one record.
def many?
- if block_given?
- to_a.many? { |*block_args| yield(*block_args) }
- else
- limit_value ? to_a.many? : size > 1
- end
+ return super if block_given?
+ limit_value ? to_a.many? : size > 1
end
# Scope all queries to the current scope.