Skip to content
Permalink
Browse files

Added ActiveRecord::QueryMethods#rewhere which will overwrite an exis…

…ting, named where condition.
  • Loading branch information
dhh committed Nov 3, 2013
1 parent 4d7080f commit f950b2699f97749ef706c6939a84dfc85f0b05f2
@@ -2,6 +2,16 @@

*Jon Leighton*

* Added ActiveRecord::QueryMethods#rewhere which will overwrite an existing, named where condition.

Examples:

Post.where(trashed: true).where(trashed: false) #=> WHERE `trashed` = 1 AND `trashed` = 0
Post.where(trashed: true).rewhere(trashed: false) #=> WHERE `trashed` = 0
Post.where(active: true).where(trashed: true).rewhere(trashed: false) #=> WHERE `active` = 1 AND `trashed` = 0

*DHH*

* Extend ActiveRecord::Base#cache_key to take an optional list of timestamp attributes of which the highest will be used.

Example:
@@ -550,6 +550,18 @@ def where!(opts = :chain, *rest) # :nodoc:
end
end

# Allows you to change a previously set where condition for a given attribute, instead of appending to that condition.
#
# Post.where(trashed: true).where(trashed: false) #=> WHERE `trashed` = 1 AND `trashed` = 0
# Post.where(trashed: true).rewhere(trashed: false) #=> WHERE `trashed` = 0
# Post.where(active: true).where(trashed: true).rewhere(trashed: false) #=> WHERE `active` = 1 AND `trashed` = 0
#
# This is short-hand for unscope(where: conditions.keys).where(conditions). Note that unlike reorder, we're only unscoping
# the named conditions -- not the entire where statement.
def rewhere(conditions)
unscope(where: conditions.keys).where(conditions)
end

# Allows to specify a HAVING clause. Note that you can't use HAVING
# without also specifying a GROUP clause.
#
@@ -76,5 +76,35 @@ def test_chaining_multiple
expected = Arel::Nodes::NotEqual.new(Post.arel_table[@name], 'ruby on rails')
assert_equal(expected, relation.where_values[1])
end

def test_rewhere_with_one_condition
relation = Post.where(title: 'hello').where(title: 'world').rewhere(title: 'alone')

expected = Arel::Nodes::Equality.new(Post.arel_table[@name], 'alone')
assert_equal 1, relation.where_values.size
assert_equal expected, relation.where_values.first
end

def test_rewhere_with_multiple_overwriting_conditions
relation = Post.where(title: 'hello').where(body: 'world').rewhere(title: 'alone', body: 'again')

title_expected = Arel::Nodes::Equality.new(Post.arel_table['title'], 'alone')
body_expected = Arel::Nodes::Equality.new(Post.arel_table['body'], 'again')

assert_equal 2, relation.where_values.size
assert_equal title_expected, relation.where_values.first
assert_equal body_expected, relation.where_values.second
end

def test_rewhere_with_one_overwriting_condition_and_one_unrelated
relation = Post.where(title: 'hello').where(body: 'world').rewhere(title: 'alone')

title_expected = Arel::Nodes::Equality.new(Post.arel_table['title'], 'alone')
body_expected = Arel::Nodes::Equality.new(Post.arel_table['body'], 'world')

assert_equal 2, relation.where_values.size
assert_equal body_expected, relation.where_values.first
assert_equal title_expected, relation.where_values.second
end
end
end

0 comments on commit f950b26

Please sign in to comment.
You can’t perform that action at this time.