Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make AR::Relation include Enumerable #8794

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -14,7 +14,7 @@ class Relation

VALUE_METHODS = MULTI_VALUE_METHODS + SINGLE_VALUE_METHODS

include FinderMethods, Calculations, SpawnMethods, QueryMethods, Batches, Explain, Delegation
include FinderMethods, Calculations, SpawnMethods, QueryMethods, Batches, Explain, Delegation, ::Enumerable

attr_reader :table, :klass, :loaded
attr_accessor :default_scoped
@@ -198,10 +198,6 @@ def to_a
@records
end

def as_json(options = nil) #:nodoc:
to_a.as_json(options)
end

# Returns size of the records.
def size
loaded? ? @records.length : count
@@ -218,7 +214,7 @@ def empty?
# Returns true if there are any records.
def any?
if block_given?
to_a.any? { |*block_args| yield(*block_args) }
super
else
!empty?
end
@@ -227,7 +223,7 @@ def any?
# Returns true if there is more than one record.
def many?
if block_given?
to_a.many? { |*block_args| yield(*block_args) }
super
else
limit_value ? to_a.many? : size > 1
end
@@ -11,7 +11,7 @@ module Delegation # :nodoc:
# may vary depending on the klass of a relation, so we create a subclass of Relation
# for each different klass, and the delegations are compiled into that subclass only.

delegate :to_xml, :to_yaml, :length, :collect, :map, :each, :all?, :include?, :to_ary, :to => :to_a
delegate :to_xml, :to_yaml, :length, :each, :to_ary, :to => :to_a
delegate :table_name, :quoted_table_name, :primary_key, :quoted_primary_key,
:connection, :columns_hash, :auto_explain_threshold_in_seconds, :to => :klass

@@ -196,7 +196,7 @@ def references!(*args) # :nodoc:
# # => ActiveModel::MissingAttributeError: missing attribute: other_field
def select(*fields)
if block_given?
to_a.select { |*block_args| yield(*block_args) }
super
else
raise ArgumentError, 'Call this with at least one field' if fields.empty?
spawn.select!(*fields)
@@ -1451,7 +1451,7 @@ def test_calling_many_should_count_instead_of_loading_association

def test_calling_many_on_loaded_association_should_not_use_query
firm = companies(:first_firm)
firm.clients.collect # force load
firm.clients.to_a # force load
assert_no_queries { assert firm.clients.many? }
end

@@ -22,8 +22,8 @@ def test_found_items_are_cached
all_posts = Topic.base

assert_queries(1) do
all_posts.collect
all_posts.collect
all_posts.to_a
all_posts.to_a
end
end

@@ -192,7 +192,7 @@ def test_any_should_call_proxy_found_if_using_a_block

def test_any_should_not_fire_query_if_scope_loaded
topics = Topic.base
topics.collect # force load
topics.to_a # force load
assert_no_queries { assert topics.any? }
end

@@ -221,7 +221,7 @@ def test_many_should_call_proxy_found_if_using_a_block

def test_many_should_not_fire_query_if_scope_loaded
topics = Topic.base
topics.collect # force load
topics.to_a # force load
assert_no_queries { assert topics.many? }
end

@@ -446,4 +446,8 @@ def test_eager_default_scope_relations_are_deprecated
end
assert_equal [posts(:welcome).title], klass.all.map(&:title)
end

def test_json_export
assert_equal '[{"comment":{"id":1,"post_id":1,"parent_id":null}}]', Comment.where(:id => 1).to_json(:only => [:id, :post_id, :parent_id])
end
end
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.