Skip to content

Commit

Permalink
Actually add the files for the new except scope.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmorrison committed Aug 20, 2008
1 parent dca3b5d commit ac1e849
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/utility_scopes/except.rb
Original file line number Original file line Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions spec/except_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -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

0 comments on commit ac1e849

Please sign in to comment.