提交 4b71ab08 编写于 作者: S Sean Griffin

Move `where_values_hash` over to `WhereClause`

上级 17b1b5d7
......@@ -571,22 +571,7 @@ def to_sql
# 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
......
......@@ -31,6 +31,28 @@ def except(*columns)
)
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 &&
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册