public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Search Repo:
Tidy up ActiveSupport::Callbacks::CallbackChain instance API.
josh (author)
Thu Apr 17 21:30:01 -0700 2008
commit  cf04e621270bb2e5e9e7971d2c59e73d6797482d
tree    af14a01cb712d6ade17f39da3a8103d7bf3bfd8c
parent  82b4faf81218bbd8916ab559590db236c7f80e46
...
22
23
24
25
 
26
27
28
...
22
23
24
 
25
26
27
28
0
@@ -22,7 +22,7 @@
0
       def to_prepare(identifier = nil, &block)
0
         @prepare_dispatch_callbacks ||= ActiveSupport::Callbacks::CallbackChain.new
0
         callback = ActiveSupport::Callbacks::Callback.new(:prepare_dispatch, block, :identifier => identifier)
0
- @prepare_dispatch_callbacks.replace_or_append_callback(callback)
0
+ @prepare_dispatch_callbacks | callback
0
       end
0
 
0
       # If the block raises, send status code as a last-ditch response.
...
265
266
267
268
 
269
270
271
...
302
303
304
305
 
306
307
308
...
326
327
328
329
 
330
331
332
...
265
266
267
 
268
269
270
271
...
302
303
304
 
305
306
307
308
...
326
327
328
 
329
330
331
332
0
@@ -265,7 +265,7 @@
0
       def skip_filter_in_chain(*filters, &test)
0
         filters, conditions = extract_options(filters)
0
         filters.each do |filter|
0
- if callback = find_callback(filter) then delete(callback) end
0
+ if callback = find(filter) then delete(callback) end
0
         end if conditions.empty?
0
         update_filter_in_chain(filters, :skip => conditions, &test)
0
       end
0
@@ -302,7 +302,7 @@
0
         def find_or_create_filter(filter, filter_type, options = {})
0
           update_filter_in_chain([filter], options)
0
 
0
- if found_filter = find_callback(filter) { |f| f.type == filter_type }
0
+ if found_filter = find(filter) { |f| f.type == filter_type }
0
             found_filter
0
           else
0
             filter_kind = case
0
@@ -326,7 +326,7 @@
0
         end
0
 
0
         def update_filter_in_chain(filters, options, &test)
0
- filters.map! { |f| block_given? ? find_callback(f, &test) : find_callback(f) }
0
+ filters.map! { |f| block_given? ? find(f, &test) : find(f) }
0
           filters.compact!
0
 
0
           map! do |filter|
...
7
8
9
10
11
 
12
13
14
...
7
8
9
 
 
10
11
12
13
0
@@ -7,8 +7,7 @@
0
       %w( validate validate_on_create validate_on_update ).each do |validation_method|
0
         base.class_eval <<-"end_eval"
0
           def self.#{validation_method}(*methods, &block)
0
- methods = CallbackChain.build(:#{validation_method}, *methods, &block)
0
- self.#{validation_method}_callback_chain.replace(#{validation_method}_callback_chain | methods)
0
+ self.#{validation_method}_callback_chain | CallbackChain.build(:#{validation_method}, *methods, &block)
0
           end
0
 
0
           def self.#{validation_method}_callback_chain
...
285
286
287
288
289
 
290
291
292
...
285
286
287
 
 
288
289
290
291
0
@@ -285,8 +285,7 @@
0
       VALIDATIONS.each do |validation_method|
0
         base.class_eval <<-"end_eval"
0
           def self.#{validation_method}(*methods, &block)
0
- methods = CallbackChain.build(:#{validation_method}, *methods, &block)
0
- self.#{validation_method}_callback_chain.replace(#{validation_method}_callback_chain | methods)
0
+ self.#{validation_method}_callback_chain | CallbackChain.build(:#{validation_method}, *methods, &block)
0
           end
0
 
0
           def self.#{validation_method}_callback_chain
...
96
97
98
99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
101
102
103
104
105
106
107
108
109
 
 
110
111
112
...
96
97
98
 
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
 
 
 
 
 
 
 
117
118
119
120
121
0
@@ -96,17 +96,26 @@
0
         end
0
       end
0
 
0
- def find_callback(callback, &block)
0
+ def |(chain)
0
+ if chain.is_a?(Callback)
0
+ if found_callback = find(chain)
0
+ index = index(found_callback)
0
+ self[index] = chain
0
+ else
0
+ self << chain
0
+ end
0
+ else
0
+ chain.each { |callback| self | callback }
0
+ end
0
+ self
0
+ end
0
+
0
+ def find(callback, &block)
0
         select { |c| c == callback && (!block_given? || yield(c)) }.first
0
       end
0
 
0
- def replace_or_append_callback(callback)
0
- if found_callback = find_callback(callback)
0
- index = index(found_callback)
0
- self[index] = callback
0
- else
0
- self << callback
0
- end
0
+ def delete(callback)
0
+ super(find(callback))
0
       end
0
 
0
       private
...
96
97
98
 
 
99
100
101
...
113
114
115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
117
...
96
97
98
99
100
101
102
103
...
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
0
@@ -96,6 +96,8 @@
0
 end
0
 
0
 class CallbackTest < Test::Unit::TestCase
0
+ include ActiveSupport::Callbacks
0
+
0
   def test_eql
0
     callback = Callback.new(:before, :save, :identifier => :lifesaver)
0
     assert callback.eql?(Callback.new(:before, :save, :identifier => :lifesaver))
0
@@ -113,6 +115,35 @@
0
     b.options[:unless] = :pigs_fly
0
     assert_equal({:unless => :pigs_fly}, b.options)
0
     assert_equal({}, a.options)
0
+ end
0
+end
0
+
0
+class CallbackChainTest < Test::Unit::TestCase
0
+ include ActiveSupport::Callbacks
0
+
0
+ def setup
0
+ @chain = CallbackChain.build(:make, :bacon, :lettuce, :tomato)
0
+ end
0
+
0
+ def test_build
0
+ assert_equal 3, @chain.size
0
+ assert_equal [:bacon, :lettuce, :tomato], @chain.map(&:method)
0
+ end
0
+
0
+ def test_find
0
+ assert_equal :bacon, @chain.find(:bacon).method
0
+ end
0
+
0
+ def test_union
0
+ assert_equal [:bacon, :lettuce, :tomato], (@chain | Callback.new(:make, :bacon)).map(&:method)
0
+ assert_equal [:bacon, :lettuce, :tomato, :turkey], (@chain | CallbackChain.build(:make, :bacon, :lettuce, :tomato, :turkey)).map(&:method)
0
+ assert_equal [:bacon, :lettuce, :tomato, :turkey, :mayo], (@chain | Callback.new(:make, :mayo)).map(&:method)
0
+ end
0
+
0
+ def test_delete
0
+ assert_equal [:bacon, :lettuce, :tomato], @chain.map(&:method)
0
+ @chain.delete(:bacon)
0
+ assert_equal [:lettuce, :tomato], @chain.map(&:method)
0
   end
0
 end

Comments

    No one has commented yet.