mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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]
This commit is contained in:
parent
7d14bd3ff5
commit
b644964b2b
5 changed files with 21 additions and 37 deletions
|
@ -1,3 +1,7 @@
|
|||
* Include the `Enumerable` module in `ActiveRecord::Relation`
|
||||
|
||||
*Sean Griffin & bogdan*
|
||||
|
||||
* Use `Enumerable#sum` in `ActiveRecord::Relation` if a block is given.
|
||||
|
||||
*Sean Griffin*
|
||||
|
|
|
@ -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,39 +276,27 @@ module ActiveRecord
|
|||
|
||||
# Returns true if there are no records.
|
||||
def none?
|
||||
if block_given?
|
||||
to_a.none? { |*block_args| yield(*block_args) }
|
||||
else
|
||||
return super if block_given?
|
||||
empty?
|
||||
end
|
||||
end
|
||||
|
||||
# Returns true if there are any records.
|
||||
def any?
|
||||
if block_given?
|
||||
to_a.any? { |*block_args| yield(*block_args) }
|
||||
else
|
||||
return super if block_given?
|
||||
!empty?
|
||||
end
|
||||
end
|
||||
|
||||
# Returns true if there is exactly one record.
|
||||
def one?
|
||||
if block_given?
|
||||
to_a.one? { |*block_args| yield(*block_args) }
|
||||
else
|
||||
return super if block_given?
|
||||
limit_value ? to_a.one? : size == 1
|
||||
end
|
||||
end
|
||||
|
||||
# Returns true if there is more than one record.
|
||||
def many?
|
||||
if block_given?
|
||||
to_a.many? { |*block_args| yield(*block_args) }
|
||||
else
|
||||
return super if block_given?
|
||||
limit_value ? to_a.many? : size > 1
|
||||
end
|
||||
end
|
||||
|
||||
# Scope all queries to the current scope.
|
||||
#
|
||||
|
|
|
@ -70,13 +70,10 @@ module ActiveRecord
|
|||
# +calculate+ for examples with options.
|
||||
#
|
||||
# Person.sum(:age) # => 4562
|
||||
def sum(*args, &block)
|
||||
if block_given?
|
||||
to_a.sum(&block)
|
||||
else
|
||||
def sum(*args)
|
||||
return super if block_given?
|
||||
calculate(:sum, *args)
|
||||
end
|
||||
end
|
||||
|
||||
# This calculates aggregate values in the given column. Methods for count, sum, average,
|
||||
# minimum, and maximum have been added as shortcuts.
|
||||
|
|
|
@ -62,12 +62,9 @@ module ActiveRecord
|
|||
# Person.where(name: 'Spartacus', rating: 4).pluck(:field1, :field2)
|
||||
# # returns an Array of the required fields.
|
||||
def find(*args)
|
||||
if block_given?
|
||||
to_a.find(*args) { |*block_args| yield(*block_args) }
|
||||
else
|
||||
return super if block_given?
|
||||
find_with_ids(*args)
|
||||
end
|
||||
end
|
||||
|
||||
# Finds the first record matching the specified conditions. There
|
||||
# is no implied ordering so if order matters, you should specify it
|
||||
|
|
|
@ -242,13 +242,10 @@ module ActiveRecord
|
|||
# Model.select(:field).first.other_field
|
||||
# # => ActiveModel::MissingAttributeError: missing attribute: other_field
|
||||
def select(*fields)
|
||||
if block_given?
|
||||
to_a.select { |*block_args| yield(*block_args) }
|
||||
else
|
||||
return super if block_given?
|
||||
raise ArgumentError, 'Call this with at least one field' if fields.empty?
|
||||
spawn._select!(*fields)
|
||||
end
|
||||
end
|
||||
|
||||
def _select!(*fields) # :nodoc:
|
||||
fields.flatten!
|
||||
|
|
Loading…
Reference in a new issue