aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/where_clause.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2015-01-25 17:44:09 -0700
committerSean Griffin <sean@thoughtbot.com>2015-01-25 17:50:24 -0700
commit4b71ab089ce57cf063005f014e1ff1bb675c46c7 (patch)
tree87ec95421e0009be9d319373bff28ca8187c99df /activerecord/lib/active_record/relation/where_clause.rb
parent17b1b5d77342db8fe3aa064d848d46052cb4695c (diff)
downloadrails-4b71ab089ce57cf063005f014e1ff1bb675c46c7.tar.gz
rails-4b71ab089ce57cf063005f014e1ff1bb675c46c7.tar.bz2
rails-4b71ab089ce57cf063005f014e1ff1bb675c46c7.zip
Move `where_values_hash` over to `WhereClause`
Diffstat (limited to 'activerecord/lib/active_record/relation/where_clause.rb')
-rw-r--r--activerecord/lib/active_record/relation/where_clause.rb22
1 files changed, 22 insertions, 0 deletions
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 &&