aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorAkira Matsuda <ronnie@dio.jp>2012-11-05 13:56:30 +0900
committerAkira Matsuda <ronnie@dio.jp>2012-11-30 01:18:18 +0900
commitde75af7acc5c05c708443de40e78965925165217 (patch)
tree2eb07baa89d145f0d0d66ee51d5fb0e5d62646e6 /activerecord
parent80b8df5f3d09205d8f239e9aefd5ed0e0ddfd4a5 (diff)
downloadrails-de75af7acc5c05c708443de40e78965925165217.tar.gz
rails-de75af7acc5c05c708443de40e78965925165217.tar.bz2
rails-de75af7acc5c05c708443de40e78965925165217.zip
Relation.where with no args can be chained with not, like, and not_like
examples: Model.where.not field: nil #=> "SELECT * FROM models WHERE field IS NOT NULL Model.where.like name: 'Jeremy%' #=> "SELECT * FROM models WHERE name LIKE 'Jeremy%' this feature was originally suggested by Jeremy Kemper https://github.com/rails/rails/pull/5950#issuecomment-5591330 Closes #5950
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md9
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb103
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb2
-rw-r--r--activerecord/test/cases/relation/where_chain_test.rb78
4 files changed, 184 insertions, 8 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 66359fb80f..a8c5ffeaed 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,5 +1,14 @@
## Rails 4.0.0 (unreleased) ##
+* Allow `Relation#where` with no arguments to be chained with new query methods
+ `not`, `like`, and `not_like`.
+
+ Example:
+
+ Developer.where.not(name: 'Aaron').where.like(name: 'Takoyaki%')
+
+ *Akira Matsuda*
+
* Fix dirty attribute checks for TimeZoneConversion with nil and blank
datetime attributes. Setting a nil datetime to a blank string should not
result in a change being flagged. Fix #8310
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index b3712b4ad6..5e79c5489f 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -4,6 +4,68 @@ module ActiveRecord
module QueryMethods
extend ActiveSupport::Concern
+ # WhereChain objects act as placeholder for queries in which #where does not have any parameter.
+ # In this case, #where must be chained with either #not, #like, or #not_like to return a new relation.
+ class WhereChain
+ def initialize(scope)
+ @scope = scope
+ end
+
+ # Returns a new relation expressing WHERE + NOT condition
+ # according to the conditions in the arguments.
+ #
+ # User.where.not(name: "Jon")
+ # # SELECT * FROM users WHERE name <> 'Jon'
+ #
+ # User.where.not(name: nil)
+ # # SELECT * FROM users WHERE name IS NOT NULL
+ #
+ # User.where.not(name: %(Ko1 Nobu))
+ # # SELECT * FROM users WHERE name NOT IN ('Ko1', 'Nobu')
+ def not(opts, *rest)
+ where_value = @scope.send(:build_where, opts, rest).map do |rel|
+ case rel
+ when Arel::Nodes::Equality
+ Arel::Nodes::NotEqual.new(rel.left, rel.right)
+ when Arel::Nodes::In
+ Arel::Nodes::NotIn.new(rel.left, rel.right)
+ when String
+ Arel::Nodes::Not.new(Arel::Nodes::SqlLiteral.new(rel))
+ else
+ Arel::Nodes::Not.new(rel)
+ end
+ end
+ @scope.where_values += where_value
+ @scope
+ end
+
+ # Returns a new relation expressing WHERE + LIKE condition
+ # according to the conditions in the arguments.
+ #
+ # Book.where.like(title: "Rails%")
+ # # SELECT * FROM books WHERE title LIKE 'Rails%'
+ def like(opts, *rest)
+ where_value = @scope.send(:build_where, opts, rest).map do |rel|
+ Arel::Nodes::Matches.new(rel.left, rel.right)
+ end
+ @scope.where_values += where_value
+ @scope
+ end
+
+ # Returns a new relation expressing WHERE + NOT LIKE condition
+ # according to the conditions in the arguments.
+ #
+ # Conference.where.not_like(name: "%Kaigi")
+ # # SELECT * FROM conferences WHERE name NOT LIKE '%Kaigi'
+ def not_like(opts, *rest)
+ where_value = @scope.send(:build_where, opts, rest).map do |rel|
+ Arel::Nodes::DoesNotMatch.new(rel.left, rel.right)
+ end
+ @scope.where_values += where_value
+ @scope
+ end
+ end
+
Relation::MULTI_VALUE_METHODS.each do |name|
class_eval <<-CODE, __FILE__, __LINE__ + 1
def #{name}_values # def select_values
@@ -379,20 +441,47 @@ module ActiveRecord
# User.joins(:posts).where({ "posts.published" => true })
# User.joins(:posts).where({ posts: { published: true } })
#
+ # === no argument or nil
+ #
+ # If no argument or nil is passed, #where returns a new instance of WhereChain which, when
+ # chained with either #not, #like, or #not_like, returns a new relation.
+ #
+ # User.where.not(name: "Jon")
+ # # SELECT * FROM users WHERE name <> 'Jon'
+ #
+ # Book.where.like(title: "Rails%")
+ # # SELECT * FROM books WHERE title LIKE 'Rails%'
+ #
+ # Conference.where.not_like(name: "%Kaigi")
+ # # SELECT * FROM conferences WHERE name NOT LIKE '%Kaigi'
+ #
+ # See WhereChain for more details on #not, #like, and #not_like.
+ #
# === empty condition
#
- # If the condition returns true for blank?, then where is a no-op and returns the current relation.
- def where(opts, *rest)
- opts.blank? ? self : spawn.where!(opts, *rest)
+ # If the condition is any other blank-ish object than nil, then where is a # no-op and returns
+ # the current relation.
+ def where(opts = nil, *rest)
+ if opts.nil?
+ WhereChain.new(spawn)
+ elsif opts.blank?
+ self
+ else
+ spawn.where!(opts, *rest)
+ end
end
# #where! is identical to #where, except that instead of returning a new relation, it adds
# the condition to the existing relation.
- def where!(opts, *rest)
- references!(PredicateBuilder.references(opts)) if Hash === opts
+ def where!(opts = nil, *rest)
+ if opts.nil?
+ WhereChain.new(self)
+ else
+ references!(PredicateBuilder.references(opts)) if Hash === opts
- self.where_values += build_where(opts, rest)
- self
+ self.where_values += build_where(opts, rest)
+ self
+ end
end
# Allows to specify a HAVING clause. Note that you can't use HAVING
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index e5022d49f1..664f636176 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -271,7 +271,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
end
def test_find_with_blank_conditions
- [[], {}, nil, ""].each do |blank|
+ [[], {}, ""].each do |blank|
assert_equal 2, Firm.all.merge!(:order => "id").first.clients.where(blank).to_a.size
end
end
diff --git a/activerecord/test/cases/relation/where_chain_test.rb b/activerecord/test/cases/relation/where_chain_test.rb
new file mode 100644
index 0000000000..10b19da3fd
--- /dev/null
+++ b/activerecord/test/cases/relation/where_chain_test.rb
@@ -0,0 +1,78 @@
+require 'cases/helper'
+require 'models/post'
+require 'models/comment'
+
+module ActiveRecord
+ class WhereChainTest < ActiveRecord::TestCase
+ fixtures :posts
+
+ def test_not_eq
+ expected = Arel::Nodes::NotEqual.new(Post.arel_table[:title], 'hello')
+ relation = Post.where.not(title: 'hello')
+ assert_equal([expected], relation.where_values)
+ end
+
+ def test_not_null
+ expected = Arel::Nodes::NotEqual.new(Post.arel_table[:title], nil)
+ relation = Post.where.not(title: nil)
+ assert_equal([expected], relation.where_values)
+ end
+
+ def test_not_in
+ expected = Arel::Nodes::NotEqual.new(Post.arel_table[:title], %w[hello goodbye])
+ relation = Post.where.not(title: %w[hello goodbye])
+ assert_equal([expected], relation.where_values)
+ end
+
+ def test_association_not_eq
+ expected = Arel::Nodes::NotEqual.new(Comment.arel_table[:title], 'hello')
+ relation = Post.joins(:comments).where.not(comments: {title: 'hello'})
+ assert_equal(expected.to_sql, relation.where_values.first.to_sql)
+ end
+
+ def test_not_eq_with_preceding_where
+ relation = Post.where(title: 'hello').where.not(title: 'world')
+
+ expected = Arel::Nodes::Equality.new(Post.arel_table[:title], 'hello')
+ assert_equal(expected, relation.where_values.first)
+
+ expected = Arel::Nodes::NotEqual.new(Post.arel_table[:title], 'world')
+ assert_equal(expected, relation.where_values.last)
+ end
+
+ def test_not_eq_with_succeeding_where
+ relation = Post.where.not(title: 'hello').where(title: 'world')
+
+ expected = Arel::Nodes::NotEqual.new(Post.arel_table[:title], 'hello')
+ assert_equal(expected, relation.where_values.first)
+
+ expected = Arel::Nodes::Equality.new(Post.arel_table[:title], 'world')
+ assert_equal(expected, relation.where_values.last)
+ end
+
+ def test_like
+ expected = Arel::Nodes::Matches.new(Post.arel_table[:title], 'a%')
+ relation = Post.where.like(title: 'a%')
+ assert_equal([expected], relation.where_values)
+ end
+
+ def test_not_like
+ expected = Arel::Nodes::DoesNotMatch.new(Post.arel_table[:title], 'a%')
+ relation = Post.where.not_like(title: 'a%')
+ assert_equal([expected], relation.where_values)
+ end
+
+ def test_chaining_multiple
+ relation = Post.where.like(title: 'ruby on %').where.not(title: 'ruby on rails').where.not_like(title: '% ales')
+
+ expected = Arel::Nodes::Matches.new(Post.arel_table[:title], 'ruby on %')
+ assert_equal(expected, relation.where_values[0])
+
+ expected = Arel::Nodes::NotEqual.new(Post.arel_table[:title], 'ruby on rails')
+ assert_equal(expected, relation.where_values[1])
+
+ expected = Arel::Nodes::DoesNotMatch.new(Post.arel_table[:title], '% ales')
+ assert_equal(expected, relation.where_values[2])
+ end
+ end
+end