Permalink
Browse files

where_not :foo => bar

  • Loading branch information...
1 parent d6657ff commit 8afeceb2129c8394a4cac5f78daaba9aaafd25d5 @amatsuda amatsuda committed Nov 24, 2011
Showing with 73 additions and 0 deletions.
  1. +21 −0 lib/everywhere/method.rb
  2. +52 −0 spec/method_spec.rb
View
@@ -0,0 +1,21 @@
+require 'everywhere/util'
+
+module ActiveRecord
+ class Base
+ class << self
+ delegate :where_not, :to => :scoped
+ end
+ end
+
+ module QueryMethods
+ include Everywhere::Util
+
+ def where_not(opts, *rest)
+ return self if opts.blank?
+
+ relation = clone
+ relation.where_values += build_where(opts, rest).map {|r| negate r}
+ relation
+ end
+ end
+end
View
@@ -0,0 +1,52 @@
+require 'spec_helper'
+require 'everywhere/method'
+
+describe 'normal query' do
+ before do
+ @where = Post.where(:name => 'hello').where_values
+ end
+ subject { @where }
+ it { @where.should have(1).item }
+ subject { @where.first }
+ its(:to_sql) { should == %q["posts"."name" = 'hello'] }
+end
+
+describe 'not eq' do
+ before do
+ @where = Post.where_not(:name => 'hello').where_values
+ end
+ subject { @where }
+ it { @where.should have(1).item }
+ subject { @where.first }
+ its(:to_sql) { should == %q["posts"."name" != 'hello'] }
+end
+
+describe 'not null' do
+ before do
+ @where = Post.where_not(:created_at => nil).where_values
+ end
+ subject { @where }
+ it { @where.should have(1).item }
+ subject { @where.first }
+ its(:to_sql) { should == %q["posts"."created_at" IS NOT NULL] }
+end
+
+describe 'not in' do
+ before do
+ @where = Post.where_not(:name => %w[hello goodbye]).where_values
+ end
+ subject { @where }
+ it { @where.should have(1).item }
+ subject { @where.first }
+ its(:to_sql) { should == %q["posts"."name" NOT IN ('hello', 'goodbye')] }
+end
+
+describe 'association' do
+ before do
+ @where = Post.joins(:comments).where_not(:comments => {:body => 'foo'}).where_values
+ end
+ subject { @where }
+ it { @where.should have(1).item }
+ subject { @where.first }
+ its(:to_sql) { should == %q["comments"."body" != 'foo'] }
+end

0 comments on commit 8afeceb

Please sign in to comment.