Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Actually add the files for the new except scope.

  • Loading branch information...
commit ac1e8492606f51fcf86611306ea1a9cbbeec713b 1 parent dca3b5d
@danielmorrison danielmorrison authored
Showing with 46 additions and 0 deletions.
  1. +21 −0 lib/utility_scopes/except.rb
  2. +25 −0 spec/except_spec.rb
View
21 lib/utility_scopes/except.rb
@@ -0,0 +1,21 @@
+module UtilityScopes
+ module Except
+
+ def self.included(within)
+
+ within.class_eval do
+
+ # Allow easy rejection of items.
+ # Can take an a single id or ActiveRecord object, or an array of them
+ # Example:
+ # before Article.all.reject{|a| a == @bad_article }
+ # after Article.except(@bad_article)
+ named_scope :except, lambda { |item_or_list|
+ # nil or empty array causes issues here with mysql
+ item_or_list.blank? ? {} : {:conditions => ["#{quoted_table_name}.#{primary_key} NOT IN (?)", item_or_list]}
+ }
+
+ end
+ end
+ end
+end
View
25 spec/except_spec.rb
@@ -0,0 +1,25 @@
+require File.join(File.dirname(__FILE__), *%w[abstract_spec])
+
+describe "except scope" do
+
+ before do
+ uses_fixture(:article)
+ # Stub these so it doesn't try to hit the (non-existant) database
+ ActiveRecord::Base.stub!(:quoted_table_name).and_return('articles')
+ ActiveRecord::Base.stub!(:primary_key).and_return('id')
+ end
+
+ it "should not change the scope with nil" do
+ Article.except(nil).proxy_options.should == {}
+ end
+
+ it "should return all items except the one specified" do
+ item = 123
+ Article.except(item).proxy_options.should == {:conditions => ['articles.id NOT IN (?)', item]}
+ end
+
+ it "should return all items except ones in the list specified" do
+ list = [123, 456, 7]
+ Article.except(list).proxy_options.should == {:conditions => ['articles.id NOT IN (?)', list]}
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.