提交 12afdba8 编写于 作者: G Gannon McGibbon

Fix unscoped grouped where

上级 3c7dbe4b
* Allow attributes to be fetched from Arel node groupings.
*Jeff Emminger*, *Gannon McGibbon*
* Calling methods like `establish_connection` with a `Hash` which is invalid (eg: no `adapter`) will now raise an error the same way as connections defined in `config/database.yml`.
*John Crepezzi*
......
......@@ -46,10 +46,20 @@ def self.arel_node?(value) # :nodoc:
value.is_a?(Arel::Node) || value.is_a?(Arel::Attribute) || value.is_a?(Arel::Nodes::SqlLiteral)
end
def self.fetch_attribute(value) # :nodoc:
def self.fetch_attribute(value, &block) # :nodoc:
case value
when Arel::Nodes::Between, Arel::Nodes::In, Arel::Nodes::NotIn, Arel::Nodes::Equality, Arel::Nodes::NotEqual, Arel::Nodes::LessThan, Arel::Nodes::LessThanOrEqual, Arel::Nodes::GreaterThan, Arel::Nodes::GreaterThanOrEqual
yield value.left.is_a?(Arel::Attributes::Attribute) ? value.left : value.right
when Arel::Nodes::Between, Arel::Nodes::In, Arel::Nodes::NotIn, Arel::Nodes::Equality,
Arel::Nodes::NotEqual, Arel::Nodes::LessThan, Arel::Nodes::LessThanOrEqual,
Arel::Nodes::GreaterThan, Arel::Nodes::GreaterThanOrEqual
if value.left.is_a?(Arel::Attributes::Attribute)
yield value.left
else
yield value.right
end
when Arel::Nodes::Or
fetch_attribute(value.left, &block) && fetch_attribute(value.right, &block)
when Arel::Nodes::Grouping
fetch_attribute(value.expr, &block)
end
end
......
......@@ -2050,6 +2050,15 @@ def test_unscope_specific_where_value
assert_equal 1, posts.unscope(where: :body).count
end
def test_unscope_grouped_where
posts = Post.where(
title: ["Welcome to the weblog", "So I was thinking", nil]
)
assert_equal 2, posts.count
assert_equal Post.count, posts.unscope(where: :title).count
end
def test_locked_should_not_build_arel
posts = Post.locked
assert_predicate posts, :locked?
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册