Skip to content

Commit

Permalink
Added ActiveRecord::QueryMethods#rewhere which will overwrite an exis…
Browse files Browse the repository at this point in the history
…ting, named where condition.
  • Loading branch information
dhh committed Nov 3, 2013
1 parent 4d7080f commit f950b26
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
10 changes: 10 additions & 0 deletions activerecord/CHANGELOG.md
Expand Up @@ -2,6 +2,16 @@


*Jon Leighton* *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. * Extend ActiveRecord::Base#cache_key to take an optional list of timestamp attributes of which the highest will be used.


Example: Example:
Expand Down
12 changes: 12 additions & 0 deletions activerecord/lib/active_record/relation/query_methods.rb
Expand Up @@ -550,6 +550,18 @@ def where!(opts = :chain, *rest) # :nodoc:
end end
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 # Allows to specify a HAVING clause. Note that you can't use HAVING
# without also specifying a GROUP clause. # without also specifying a GROUP clause.
# #
Expand Down
30 changes: 30 additions & 0 deletions activerecord/test/cases/relation/where_chain_test.rb
Expand Up @@ -76,5 +76,35 @@ def test_chaining_multiple
expected = Arel::Nodes::NotEqual.new(Post.arel_table[@name], 'ruby on rails') expected = Arel::Nodes::NotEqual.new(Post.arel_table[@name], 'ruby on rails')
assert_equal(expected, relation.where_values[1]) assert_equal(expected, relation.where_values[1])
end 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
end end

0 comments on commit f950b26

Please sign in to comment.