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

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

This commit is contained in:
David Heinemeier Hansson 2013-11-02 17:35:34 -07:00
parent 4d7080f80c
commit f950b2699f
3 changed files with 52 additions and 0 deletions

View file

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

View file

@ -550,6 +550,18 @@ module ActiveRecord
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.
#

View file

@ -76,5 +76,35 @@ module ActiveRecord
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