diff options
author | Sean Griffin <sean@thoughtbot.com> | 2015-01-25 17:44:09 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2015-01-25 17:50:24 -0700 |
commit | 4b71ab089ce57cf063005f014e1ff1bb675c46c7 (patch) | |
tree | 87ec95421e0009be9d319373bff28ca8187c99df /activerecord/lib/active_record | |
parent | 17b1b5d77342db8fe3aa064d848d46052cb4695c (diff) | |
download | rails-4b71ab089ce57cf063005f014e1ff1bb675c46c7.tar.gz rails-4b71ab089ce57cf063005f014e1ff1bb675c46c7.tar.bz2 rails-4b71ab089ce57cf063005f014e1ff1bb675c46c7.zip |
Move `where_values_hash` over to `WhereClause`
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 17 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation/where_clause.rb | 22 |
2 files changed, 23 insertions, 16 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 54cb6861b8..5aee74ae61 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -571,22 +571,7 @@ module ActiveRecord # User.where(name: 'Oscar').where_values_hash # # => {name: "Oscar"} def where_values_hash(relation_table_name = table_name) - equalities = where_values.grep(Arel::Nodes::Equality).find_all { |node| - node.left.relation.name == relation_table_name - } - - binds = Hash[bind_values.find_all(&:first).map { |column, v| [column.name, v] }] - - Hash[equalities.map { |where| - name = where.left.name - [name, binds.fetch(name.to_s) { - case where.right - when Array then where.right.map(&:val) - when Arel::Nodes::Casted, Arel::Nodes::Quoted - where.right.val - end - }] - }] + where_clause.to_h(relation_table_name) end def scope_for_create diff --git a/activerecord/lib/active_record/relation/where_clause.rb b/activerecord/lib/active_record/relation/where_clause.rb index ca9c7d19a7..90fb85cbf1 100644 --- a/activerecord/lib/active_record/relation/where_clause.rb +++ b/activerecord/lib/active_record/relation/where_clause.rb @@ -31,6 +31,28 @@ module ActiveRecord ) end + def to_h(table_name = nil) + equalities = predicates.grep(Arel::Nodes::Equality) + if table_name + equalities = equalities.select do |node| + node.left.relation.name == table_name + end + end + + binds = self.binds.select(&:first).to_h.transform_keys(&:name) + + equalities.map { |node| + name = node.left.name + [name, binds.fetch(name.to_s) { + case node.right + when Array then node.right.map(&:val) + when Arel::Nodes::Casted, Arel::Nodes::Quoted + node.right.val + end + }] + }.to_h + end + def ==(other) other.is_a?(WhereClause) && predicates == other.predicates && |