aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/predicate_builder.rb
diff options
context:
space:
mode:
authorMelanie Gilman <melanie@thoughtbot.com>2014-12-01 16:02:21 -0500
committerMelanie Gilman <melanie@thoughtbot.com>2014-12-02 15:49:16 -0500
commitfcc3cbc71f49f56ac5e4f4925c8c1114c08e54c5 (patch)
treee16ef8f2aa451cb9a6b546f1933527491bdfab7f /activerecord/lib/active_record/relation/predicate_builder.rb
parent3317d6958cf5627ac8db8ce4086639efa5adad43 (diff)
downloadrails-fcc3cbc71f49f56ac5e4f4925c8c1114c08e54c5.tar.gz
rails-fcc3cbc71f49f56ac5e4f4925c8c1114c08e54c5.tar.bz2
rails-fcc3cbc71f49f56ac5e4f4925c8c1114c08e54c5.zip
Refactor `build_from_hash` to convert dot notation to hash first
This ensures that we're handling all forms of nested tables the same way. We're aware that the `convert_dot_notation_to_hash` method will cause a performance hit, and we intend to come back to it once we've refactored some of the surrounding code. [Melissa Xie & Melanie Gilman]
Diffstat (limited to 'activerecord/lib/active_record/relation/predicate_builder.rb')
-rw-r--r--activerecord/lib/active_record/relation/predicate_builder.rb63
1 files changed, 34 insertions, 29 deletions
diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb
index a6da60b21a..eb21d01465 100644
--- a/activerecord/lib/active_record/relation/predicate_builder.rb
+++ b/activerecord/lib/active_record/relation/predicate_builder.rb
@@ -21,35 +21,8 @@ module ActiveRecord
end
def build_from_hash(attributes)
- queries = []
- builder = self
-
- attributes.each do |column, value|
- if value.is_a?(Hash)
- if value.empty?
- queries << '1=0'
- else
- arel_table = Arel::Table.new(column)
- association = klass._reflect_on_association(column)
- builder = self.class.new(association && association.klass, arel_table)
-
- value.each do |k, v|
- queries.concat builder.expand(k, v)
- end
- end
- else
- column = column.to_s
-
- if column.include?('.')
- table_name, column = column.split('.', 2)
- arel_table = Arel::Table.new(table_name)
- builder = self.class.new(klass, arel_table)
- end
- queries.concat builder.expand(column, value)
- end
- end
-
- queries
+ attributes = convert_dot_notation_to_hash(attributes.stringify_keys)
+ expand_from_hash(attributes)
end
def expand(column, value)
@@ -130,5 +103,37 @@ module ActiveRecord
protected
attr_reader :klass, :table
+
+ def expand_from_hash(attributes)
+ return ["1=0"] if attributes.empty?
+
+ attributes.flat_map do |key, value|
+ if value.is_a?(Hash)
+ arel_table = Arel::Table.new(key)
+ association = klass._reflect_on_association(key)
+ builder = self.class.new(association && association.klass, arel_table)
+
+ builder.expand_from_hash(value)
+ else
+ expand(key, value)
+ end
+ end
+ end
+
+ private
+
+ def convert_dot_notation_to_hash(attributes)
+ dot_notation = attributes.keys.select { |s| s.include?(".") }
+
+ dot_notation.each do |key|
+ table_name, column_name = key.split(".")
+ value = attributes.delete(key)
+ attributes[table_name] ||= {}
+
+ attributes[table_name] = attributes[table_name].merge(column_name => value)
+ end
+
+ attributes
+ end
end
end