add a specific factory method rather than using new

This commit is contained in:
Aaron Patterson 2013-07-23 14:43:17 -07:00
parent b017562382
commit f38b544442
9 changed files with 11 additions and 13 deletions

View File

@ -117,7 +117,7 @@ module ActiveRecord
# Can be overridden (i.e. in ThroughAssociation) to merge in other scopes (i.e. the # Can be overridden (i.e. in ThroughAssociation) to merge in other scopes (i.e. the
# through association's scope) # through association's scope)
def target_scope def target_scope
AssociationRelation.new(klass, klass.arel_table, self).merge!(klass.all) AssociationRelation.create(klass, klass.arel_table, self).merge!(klass.all)
end end
# Loads the \target if needed and returns it. # Loads the \target if needed and returns it.

View File

@ -34,7 +34,7 @@ module ActiveRecord
reload reload
end end
@proxy ||= CollectionProxy.new(klass, self) @proxy ||= CollectionProxy.create(klass, self)
end end
# Implements the writer method, e.g. foo.items= for Foo.has_many :items # Implements the writer method, e.g. foo.items= for Foo.has_many :items

View File

@ -105,13 +105,13 @@ module ActiveRecord
if item.is_a?(Relation) if item.is_a?(Relation)
item item
else else
ActiveRecord::Relation.new(klass, table).instance_exec(self, &item) ActiveRecord::Relation.create(klass, table).instance_exec(self, &item)
end end
end end
if reflection.type if reflection.type
scope_chain_items << scope_chain_items <<
ActiveRecord::Relation.new(klass, table) ActiveRecord::Relation.create(klass, table)
.where(reflection.type => foreign_klass.base_class.name) .where(reflection.type => foreign_klass.base_class.name)
end end

View File

@ -85,7 +85,7 @@ module ActiveRecord
def initialize(records, associations, preload_scope = nil) def initialize(records, associations, preload_scope = nil)
@records = Array.wrap(records).compact.uniq @records = Array.wrap(records).compact.uniq
@associations = Array.wrap(associations) @associations = Array.wrap(associations)
@preload_scope = preload_scope || Relation.new(nil, nil) @preload_scope = preload_scope || Relation.create(nil, nil)
end end
def run def run

View File

@ -146,7 +146,7 @@ module ActiveRecord
private private
def relation #:nodoc: def relation #:nodoc:
relation = Relation.new(self, arel_table) relation = Relation.create(self, arel_table)
if finder_needs_type_condition? if finder_needs_type_condition?
relation.where(type_condition).create_with(inheritance_column.to_sym => sti_name) relation.where(type_condition).create_with(inheritance_column.to_sym => sti_name)

View File

@ -124,7 +124,7 @@ module ActiveRecord
@quoted_table_name = nil @quoted_table_name = nil
@arel_table = nil @arel_table = nil
@sequence_name = nil unless defined?(@explicit_sequence_name) && @explicit_sequence_name @sequence_name = nil unless defined?(@explicit_sequence_name) && @explicit_sequence_name
@relation = Relation.new(self, arel_table) @relation = Relation.create(self, arel_table)
end end
# Returns a quoted version of the table name, used to construct SQL statements. # Returns a quoted version of the table name, used to construct SQL statements.

View File

@ -73,10 +73,8 @@ module ActiveRecord
module ClassMethods # :nodoc: module ClassMethods # :nodoc:
@@subclasses = ThreadSafe::Cache.new(:initial_capacity => 2) @@subclasses = ThreadSafe::Cache.new(:initial_capacity => 2)
def new(klass, *args) def create(klass, *args)
relation = relation_class_for(klass).allocate relation_class_for(klass).new(klass, *args)
relation.__send__(:initialize, klass, *args)
relation
end end
# This doesn't have to be thread-safe. relation_class_for guarantees that this will only be # This doesn't have to be thread-safe. relation_class_for guarantees that this will only be

View File

@ -22,7 +22,7 @@ module ActiveRecord
# build a relation to merge in rather than directly merging # build a relation to merge in rather than directly merging
# the values. # the values.
def other def other
other = Relation.new(relation.klass, relation.table) other = Relation.create(relation.klass, relation.table)
hash.each { |k, v| hash.each { |k, v|
if k == :joins if k == :joins
if Hash === v if Hash === v

View File

@ -64,7 +64,7 @@ module ActiveRecord
private private
def relation_with(values) # :nodoc: def relation_with(values) # :nodoc:
result = Relation.new(klass, table, values) result = Relation.create(klass, table, values)
result.extend(*extending_values) if extending_values.any? result.extend(*extending_values) if extending_values.any?
result result
end end