-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Actually add the files for the new except scope.
- Loading branch information
1 parent
dca3b5d
commit ac1e849
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |