1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Move where_values_hash over to WhereClause

This commit is contained in:
Sean Griffin 2015-01-25 17:44:09 -07:00
parent 17b1b5d773
commit 4b71ab089c
2 changed files with 23 additions and 16 deletions

View file

@ -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

View file

@ -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 &&