public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Allow multiple conditions for callbacks [#1627 state:resolved]
josh (author)
Sun Dec 28 13:07:17 -0800 2008
commit  1e45818a622405e720a4529795f8be2f11660361
tree    051a3175e52fedbcba931010852734f1e33839bc
parent  3b92b141fd0759476690a174d5e2f8f0f2d9f1b7
...
192
193
194
195
196
197
198
199
200
201
 
 
202
203
204
...
192
193
194
 
 
 
 
 
 
 
195
196
197
198
199
0
@@ -192,13 +192,8 @@ module ActiveSupport
0
         end
0
 
0
         def should_run_callback?(*args)
0
-          if options[:if]
0
-            evaluate_method(options[:if], *args)
0
-          elsif options[:unless]
0
-            !evaluate_method(options[:unless], *args)
0
-          else
0
-            true
0
-          end
0
+          [options[:if]].flatten.compact.all? { |a| evaluate_method(a, *args) } &&
0
+          ![options[:unless]].flatten.compact.any? { |a| evaluate_method(a, *args) }
0
         end
0
     end
0
 
...
53
54
55
 
56
57
58
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
61
62
...
90
91
92
93
 
 
 
 
 
 
 
 
 
 
94
95
96
...
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
...
121
122
123
 
124
125
126
127
128
129
130
131
132
133
134
135
136
0
@@ -53,10 +53,41 @@ class Person < Record
0
 end
0
 
0
 class ConditionalPerson < Record
0
+  # proc
0
   before_save Proc.new { |r| r.history << [:before_save, :proc] }, :if => Proc.new { |r| true }
0
   before_save Proc.new { |r| r.history << "b00m" }, :if => Proc.new { |r| false }
0
   before_save Proc.new { |r| r.history << [:before_save, :proc] }, :unless => Proc.new { |r| false }
0
   before_save Proc.new { |r| r.history << "b00m" }, :unless => Proc.new { |r| true }
0
+  # symbol
0
+  before_save Proc.new { |r| r.history << [:before_save, :symbol] }, :if => :yes
0
+  before_save Proc.new { |r| r.history << "b00m" }, :if => :no
0
+  before_save Proc.new { |r| r.history << [:before_save, :symbol] }, :unless => :no
0
+  before_save Proc.new { |r| r.history << "b00m" }, :unless => :yes
0
+  # string
0
+  before_save Proc.new { |r| r.history << [:before_save, :string] }, :if => 'yes'
0
+  before_save Proc.new { |r| r.history << "b00m" }, :if => 'no'
0
+  before_save Proc.new { |r| r.history << [:before_save, :string] }, :unless => 'no'
0
+  before_save Proc.new { |r| r.history << "b00m" }, :unless => 'yes'
0
+  # Array with conditions
0
+  before_save Proc.new { |r| r.history << [:before_save, :symbol_array] }, :if => [:yes, :other_yes]
0
+  before_save Proc.new { |r| r.history << "b00m" }, :if => [:yes, :no]
0
+  before_save Proc.new { |r| r.history << [:before_save, :symbol_array] }, :unless => [:no, :other_no]
0
+  before_save Proc.new { |r| r.history << "b00m" }, :unless => [:yes, :no]
0
+  # Combined if and unless
0
+  before_save Proc.new { |r| r.history << [:before_save, :combined_symbol] }, :if => :yes, :unless => :no
0
+  before_save Proc.new { |r| r.history << "b00m" }, :if => :yes, :unless => :yes
0
+  # Array with different types of conditions
0
+  before_save Proc.new { |r| r.history << [:before_save, :symbol_proc_string_array] }, :if => [:yes, Proc.new { |r| true }, 'yes']
0
+  before_save Proc.new { |r| r.history << "b00m" }, :if => [:yes, Proc.new { |r| true }, 'no']
0
+  # Array with different types of conditions comibned if and unless
0
+  before_save Proc.new { |r| r.history << [:before_save, :combined_symbol_proc_string_array] },
0
+              :if => [:yes, Proc.new { |r| true }, 'yes'], :unless => [:no, 'no']
0
+  before_save Proc.new { |r| r.history << "b00m" }, :if => [:yes, Proc.new { |r| true }, 'no'], :unless => [:no, 'no']
0
+
0
+  def yes; true; end
0
+  def other_yes; true; end
0
+  def no; false; end
0
+  def other_no; false; end
0
 
0
   def save
0
     run_callbacks(:before_save)
0
@@ -90,7 +121,16 @@ class ConditionalCallbackTest < Test::Unit::TestCase
0
     person.save
0
     assert_equal [
0
       [:before_save, :proc],
0
-      [:before_save, :proc]
0
+      [:before_save, :proc],
0
+      [:before_save, :symbol],
0
+      [:before_save, :symbol],
0
+      [:before_save, :string],
0
+      [:before_save, :string],
0
+      [:before_save, :symbol_array],
0
+      [:before_save, :symbol_array],
0
+      [:before_save, :combined_symbol],
0
+      [:before_save, :symbol_proc_string_array],
0
+      [:before_save, :combined_symbol_proc_string_array]
0
     ], person.history
0
   end
0
 end

Comments