diff options
author | Akira Matsuda <ronnie@dio.jp> | 2012-11-05 13:56:30 +0900 |
---|---|---|
committer | Akira Matsuda <ronnie@dio.jp> | 2012-11-30 01:18:18 +0900 |
commit | de75af7acc5c05c708443de40e78965925165217 (patch) | |
tree | 2eb07baa89d145f0d0d66ee51d5fb0e5d62646e6 /activerecord/lib | |
parent | 80b8df5f3d09205d8f239e9aefd5ed0e0ddfd4a5 (diff) | |
download | rails-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/lib')
-rw-r--r-- | activerecord/lib/active_record/relation/query_methods.rb | 103 |
1 files changed, 96 insertions, 7 deletions
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 |