Skip to content
This repository
Browse code

Allow multiple conditions for callbacks [#1627 state:resolved]

  • Loading branch information...
commit 1e45818a622405e720a4529795f8be2f11660361 1 parent 3b92b14
Joshua Peek authored December 28, 2008
9  activesupport/lib/active_support/callbacks.rb
@@ -192,13 +192,8 @@ def evaluate_method(method, *args, &block)
192 192
         end
193 193
 
194 194
         def should_run_callback?(*args)
195  
-          if options[:if]
196  
-            evaluate_method(options[:if], *args)
197  
-          elsif options[:unless]
198  
-            !evaluate_method(options[:unless], *args)
199  
-          else
200  
-            true
201  
-          end
  195
+          [options[:if]].flatten.compact.all? { |a| evaluate_method(a, *args) } &&
  196
+          ![options[:unless]].flatten.compact.any? { |a| evaluate_method(a, *args) }
202 197
         end
203 198
     end
204 199
 
42  activesupport/test/callbacks_test.rb
@@ -53,10 +53,41 @@ def save
53 53
 end
54 54
 
55 55
 class ConditionalPerson < Record
  56
+  # proc
56 57
   before_save Proc.new { |r| r.history << [:before_save, :proc] }, :if => Proc.new { |r| true }
57 58
   before_save Proc.new { |r| r.history << "b00m" }, :if => Proc.new { |r| false }
58 59
   before_save Proc.new { |r| r.history << [:before_save, :proc] }, :unless => Proc.new { |r| false }
59 60
   before_save Proc.new { |r| r.history << "b00m" }, :unless => Proc.new { |r| true }
  61
+  # symbol
  62
+  before_save Proc.new { |r| r.history << [:before_save, :symbol] }, :if => :yes
  63
+  before_save Proc.new { |r| r.history << "b00m" }, :if => :no
  64
+  before_save Proc.new { |r| r.history << [:before_save, :symbol] }, :unless => :no
  65
+  before_save Proc.new { |r| r.history << "b00m" }, :unless => :yes
  66
+  # string
  67
+  before_save Proc.new { |r| r.history << [:before_save, :string] }, :if => 'yes'
  68
+  before_save Proc.new { |r| r.history << "b00m" }, :if => 'no'
  69
+  before_save Proc.new { |r| r.history << [:before_save, :string] }, :unless => 'no'
  70
+  before_save Proc.new { |r| r.history << "b00m" }, :unless => 'yes'
  71
+  # Array with conditions
  72
+  before_save Proc.new { |r| r.history << [:before_save, :symbol_array] }, :if => [:yes, :other_yes]
  73
+  before_save Proc.new { |r| r.history << "b00m" }, :if => [:yes, :no]
  74
+  before_save Proc.new { |r| r.history << [:before_save, :symbol_array] }, :unless => [:no, :other_no]
  75
+  before_save Proc.new { |r| r.history << "b00m" }, :unless => [:yes, :no]
  76
+  # Combined if and unless
  77
+  before_save Proc.new { |r| r.history << [:before_save, :combined_symbol] }, :if => :yes, :unless => :no
  78
+  before_save Proc.new { |r| r.history << "b00m" }, :if => :yes, :unless => :yes
  79
+  # Array with different types of conditions
  80
+  before_save Proc.new { |r| r.history << [:before_save, :symbol_proc_string_array] }, :if => [:yes, Proc.new { |r| true }, 'yes']
  81
+  before_save Proc.new { |r| r.history << "b00m" }, :if => [:yes, Proc.new { |r| true }, 'no']
  82
+  # Array with different types of conditions comibned if and unless
  83
+  before_save Proc.new { |r| r.history << [:before_save, :combined_symbol_proc_string_array] },
  84
+              :if => [:yes, Proc.new { |r| true }, 'yes'], :unless => [:no, 'no']
  85
+  before_save Proc.new { |r| r.history << "b00m" }, :if => [:yes, Proc.new { |r| true }, 'no'], :unless => [:no, 'no']
  86
+
  87
+  def yes; true; end
  88
+  def other_yes; true; end
  89
+  def no; false; end
  90
+  def other_no; false; end
60 91
 
61 92
   def save
62 93
     run_callbacks(:before_save)
@@ -90,7 +121,16 @@ def test_save_conditional_person
90 121
     person.save
91 122
     assert_equal [
92 123
       [:before_save, :proc],
93  
-      [:before_save, :proc]
  124
+      [:before_save, :proc],
  125
+      [:before_save, :symbol],
  126
+      [:before_save, :symbol],
  127
+      [:before_save, :string],
  128
+      [:before_save, :string],
  129
+      [:before_save, :symbol_array],
  130
+      [:before_save, :symbol_array],
  131
+      [:before_save, :combined_symbol],
  132
+      [:before_save, :symbol_proc_string_array],
  133
+      [:before_save, :combined_symbol_proc_string_array]
94 134
     ], person.history
95 135
   end
96 136
 end

0 notes on commit 1e45818

Please sign in to comment.
Something went wrong with that request. Please try again.