Browse files

where :not, :foo => bar

  • Loading branch information...
1 parent 5e14e04 commit d6657ff11d5dcdd70e00166b020585d5856f7c75 @amatsuda amatsuda committed Nov 24, 2011
Showing with 68 additions and 0 deletions.
  1. +16 −0 lib/everywhere/symbol.rb
  2. +52 −0 spec/symbol_spec.rb
View
16 lib/everywhere/symbol.rb
@@ -0,0 +1,16 @@
+require 'everywhere/util'
+
+module ActiveRecord
+ module QueryMethods
+ include Everywhere::Util
+
+ def build_where_with_not(opts, other = [])
+ if opts == :not
+ build_where_without_not(*other).map {|r| negate r}
+ else
+ build_where_without_not(opts, other)
+ end
+ end
+ alias_method_chain :build_where, :not
+ end
+end
View
52 spec/symbol_spec.rb
@@ -0,0 +1,52 @@
+require 'spec_helper'
+require 'everywhere/symbol'
+
+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 d6657ff

Please sign in to comment.