diff options
author | Jon Leighton <j@jonathanleighton.com> | 2012-09-12 23:51:30 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2012-09-12 23:51:30 +0100 |
commit | e588c98035cbb0ab55d9509cf64916b97383ab6e (patch) | |
tree | 19c340e6810beec632293d4de6ee1dd4cef855a0 /activerecord | |
parent | eb4a623d74fe501ac73dc53d77cfca0d9d4ee01b (diff) | |
download | rails-e588c98035cbb0ab55d9509cf64916b97383ab6e.tar.gz rails-e588c98035cbb0ab55d9509cf64916b97383ab6e.tar.bz2 rails-e588c98035cbb0ab55d9509cf64916b97383ab6e.zip |
Refactor to remove some duplication
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/reflection.rb | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation/predicate_builder.rb | 57 |
2 files changed, 24 insertions, 37 deletions
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index cf949a893f..d9c65fa170 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -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 diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index 49fbd1686b..bc483c1825 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -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,18 +21,30 @@ 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 + queries.concat expand(engine, table, column, value) + end + end - column = rk[:foreign_key] - end + queries + end - queries << build(table[column.to_sym], value) + 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 @@ -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 |