Permalink
Browse files

Remove method if exists, avoid calling Array#first so many times.

  • Loading branch information...
1 parent da8f9ca commit 4d6e178f888c8287eb59e14bd9ecb77b4d71455b @miloops miloops committed Sep 28, 2010
Showing with 5 additions and 3 deletions.
  1. +5 −3 actionpack/lib/action_dispatch/middleware/callbacks.rb
@@ -19,9 +19,11 @@ class Callbacks
# replace the existing callback. Passing an identifier is a suggested
# practice if the code adding a preparation block may be reloaded.
def self.to_prepare(*args, &block)
- if args.first.is_a?(Symbol) && block_given?
- define_method :"__#{args.first}", &block
- set_callback(:prepare, :"__#{args.first}")
+ first_arg = args.first
+ if first_arg.is_a?(Symbol) && block_given?
+ remove_method :"__#{first_arg}" if method_defined?(:"__#{first_arg}")
+ define_method :"__#{first_arg}", &block
+ set_callback(:prepare, :"__#{first_arg}")
else
set_callback(:prepare, *args, &block)
end

0 comments on commit 4d6e178

Please sign in to comment.