public
Description: Build common named scopes automatically, and provide a simple way to merge them with a single #search call.
Clone URL: git://github.com/technoweenie/can_search.git
Click here to lend your support to: can_search and make a donation at www.pledgie.com !
fixing add existing scopes to exclude when no key is given, but include if 
value is nil
towski (author)
Wed Sep 17 18:21:06 -0700 2008
commit  49890673aef1adbfb8f920314d785072439d6402
tree    621359b2f77c665148e30518475227fb943e5512
parent  aee7254d17b4286786113b9f07a122ef51414bbb
...
59
60
61
 
62
63
64
...
59
60
61
62
63
64
65
0
@@ -59,6 +59,7 @@ module CanSearch
0
     
0
     # strip out any scoped keys from options and return a chained finder.
0
     def scope_for(finder, options = {})
0
+ return finder unless options.keys.include? @named_scope
0
       value = options.delete(@named_scope)
0
       finder.send(@named_scope, value)
0
     end
...
71
72
73
 
74
75
 
76
77
78
79
80
81
 
 
 
 
 
 
 
 
 
 
 
 
82
83
84
...
71
72
73
74
75
76
77
78
79
80
81
 
 
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
0
@@ -71,14 +71,26 @@ module CanSearch
0
       include CanSearchSpecHelper
0
       before do
0
         @scope = Record.named_scope :example, lambda { |name| {:conditions => {:name => name} } }
0
+ Record.named_scope :peanut_butter, lambda { |id| {:conditions => {:parent_id => id} } }
0
         Record.can_search do
0
           add_existing_scope :example
0
+ add_existing_scope :peanut_butter
0
         end
0
       end
0
       
0
       it "uses custom scopes" do
0
- record = Record.create! :name => "this"
0
- Record.search(:example => "this").should == [ record ]
0
+ record = Record.create! :name => "this", :parent_id => 3
0
+ Record.search(:example => "this", :peanut_butter => record.parent_id).should == [ record ]
0
+ end
0
+
0
+ it "excludes unused scopes" do
0
+ record = Record.create! :name => "that", :parent_id => 3
0
+ Record.search(:example => "that").should == [ record ]
0
+ end
0
+
0
+ it "includes scopes if they set to nil" do
0
+ record = Record.create! :name => nil, :parent_id => 3
0
+ Record.search(:example => nil, :peanut_butter => nil).should == [ ]
0
       end
0
     end
0
   end

Comments

    No one has commented yet.