public
Rubygem
Description: Adds support for creating state machines for attributes on any Ruby class
Homepage: http://www.pluginaweek.org
Clone URL: git://github.com/pluginaweek/state_machine.git
added support for without_#{state} scopes
sob (author)
Mon Nov 10 13:11:04 -0800 2008
commit  13a0906c1186ce9250b508296d4a860edc097c93
tree    010ea8b41d94a8512b654087bf5c4e53aad6a2dc
parent  6d213798a6088c75c2f882fcef4b67bb2275464a
...
403
404
405
406
407
 
 
 
 
408
409
410
...
403
404
405
 
 
406
407
408
409
410
411
412
0
@@ -403,8 +403,10 @@ module PluginAWeek #:nodoc:
0
         # for the attribute
0
         def add_named_scopes
0
           [attribute, attribute.pluralize].uniq.each do |name|
0
-            name = "with_#{name}"
0
-            owner_class.named_scope name.to_sym, lambda {|*values| {:conditions => {attribute => values.flatten}}} unless owner_class.respond_to?(name)
0
+            with_name = "with_#{name}"
0
+            without_name = "without_#{name}"
0
+            owner_class.named_scope with_name.to_sym, lambda {|*values| {:conditions => {attribute => values.flatten}}} unless owner_class.respond_to?(with_name)
0
+            owner_class.named_scope without_name.to_sym, lambda {|*values| {:conditions => ["#{attribute} NOT IN (?)", values.flatten]}} unless owner_class.respond_to?(without_name)
0
           end
0
         end
0
     end
...
60
61
62
 
 
 
 
 
 
 
63
64
65
...
67
68
69
 
 
 
 
 
 
 
 
70
71
72
...
145
146
147
 
 
 
 
 
 
 
 
148
149
150
...
155
156
157
 
 
 
 
 
 
 
 
158
159
160
...
60
61
62
63
64
65
66
67
68
69
70
71
72
...
74
75
76
77
78
79
80
81
82
83
84
85
86
87
...
160
161
162
163
164
165
166
167
168
169
170
171
172
173
...
178
179
180
181
182
183
184
185
186
187
188
189
190
191
0
@@ -60,6 +60,13 @@ class MachineTest < Test::Unit::TestCase
0
     assert_equal [on], Switch.with_state('on')
0
   end
0
   
0
+  def test_should_define_a_named_scope_for_excluding_the_attribute
0
+    on = create_switch(:state => 'on')
0
+    off = create_switch(:state => 'off')
0
+    
0
+    assert_equal [off], Switch.without_state('on')
0
+  end
0
+  
0
   def test_should_define_a_pluralized_named_scope_for_the_attribute
0
     on = create_switch(:state => 'on')
0
     off = create_switch(:state => 'off')
0
@@ -67,6 +74,14 @@ class MachineTest < Test::Unit::TestCase
0
     assert_equal [on, off], Switch.with_states('on', 'off')
0
   end
0
   
0
+  def test_should_define_a_pluralized_named_scope_for_excluding_the_attribute
0
+    on = create_switch(:state => 'on')
0
+    off = create_switch(:state => 'off')
0
+    unsure = create_switch(:state => 'unsure')
0
+    
0
+    assert_equal [unsure], Switch.without_states('on', 'off')
0
+  end
0
+  
0
   def test_should_raise_exception_if_invalid_option_specified
0
     assert_raise(ArgumentError) {PluginAWeek::StateMachine::Machine.new(Switch, 'state', :invalid => true)}
0
   end
0
@@ -145,6 +160,14 @@ class MachineWithConflictingNamedScopesTest < Test::Unit::TestCase
0
     def self.with_states
0
       :custom
0
     end
0
+    
0
+    def self.without_state
0
+      :custom
0
+    end
0
+    
0
+    def self.without_states
0
+      :custom
0
+    end
0
   end
0
   
0
   def setup
0
@@ -155,6 +178,14 @@ class MachineWithConflictingNamedScopesTest < Test::Unit::TestCase
0
     assert_equal :custom, Switch.with_state
0
   end
0
   
0
+  def test_should_not_define_a_named_scope_for_excluding_the_attribute
0
+    assert_equal :custom, Switch.without_state
0
+  end
0
+  
0
+  def test_should_not_define_a_pluralized_named_scope_for_excluding_the_attribute
0
+    assert_equal :custom, Switch.without_states
0
+  end
0
+  
0
   def test_should_not_define_a_pluralized_named_scope_for_the_attribute
0
     assert_equal :custom, Switch.with_states
0
   end

Comments