Refactor to remove some duplication

This commit is contained in:
Jon Leighton 2012-09-12 23:51:30 +01:00
parent eb4a623d74
commit e588c98035
2 changed files with 26 additions and 39 deletions

View File

@ -358,6 +358,10 @@ module ActiveRecord
end
end
def polymorphic?
options.key? :polymorphic
end
private
def derive_class_name
class_name = name.to_s.camelize

View File

@ -11,15 +11,7 @@ module ActiveRecord
association = engine.reflect_on_association(column.to_sym)
value.each do |k, v|
if association && rk = find_reflection_key(k, association.klass, v)
if rk[:foreign_type]
queries << build(table[rk[:foreign_type]], v.class.base_class)
end
k = rk[:foreign_key]
end
queries << build(table[k.to_sym], v)
queries.concat expand(association && association.klass, table, k, v)
end
else
column = column.to_s
@ -29,21 +21,33 @@ module ActiveRecord
table = Arel::Table.new(table_name, engine)
end
if rk = find_reflection_key(column, engine, value)
if rk[:foreign_type]
queries << build(table[rk[:foreign_type]], value.class.base_class)
end
column = rk[:foreign_key]
end
queries << build(table[column.to_sym], value)
queries.concat expand(engine, table, column, value)
end
end
queries
end
def self.expand(klass, table, column, value)
queries = []
# Find the foreign key when using queries such as:
# Post.where(:author => author)
#
# For polymorphic relationships, find the foreign key and type:
# PriceEstimate.where(:estimate_of => treasure)
if klass && value.class < Model::Tag && reflection = klass.reflect_on_association(column.to_sym)
if reflection.polymorphic?
queries << build(table[reflection.foreign_type], value.class.base_class)
end
column = reflection.foreign_key
end
queries << build(table[column.to_sym], value)
queries
end
def self.references(attributes)
attributes.map do |key, value|
if value.is_a?(Hash)
@ -55,27 +59,6 @@ module ActiveRecord
end.compact
end
# Find the foreign key when using queries such as:
# Post.where(:author => author)
#
# For polymorphic relationships, find the foreign key and type:
# PriceEstimate.where(:estimate_of => treasure)
def self.find_reflection_key(parent_column, model, value)
# value must be an ActiveRecord object
return nil unless value.class < Model::Tag
if reflection = model.reflections[parent_column.to_sym]
if reflection.options[:polymorphic]
{
:foreign_key => reflection.foreign_key,
:foreign_type => reflection.foreign_type
}
else
{ :foreign_key => reflection.foreign_key }
end
end
end
private
def self.build(attribute, value)
case value