Skip to content

Commit

Permalink
Add support for callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
wycats committed Feb 28, 2009
1 parent ee80dad commit c16c7a8
Show file tree
Hide file tree
Showing 6 changed files with 1,018 additions and 1 deletion.
6 changes: 5 additions & 1 deletion actionpack/lib/action_controller/abstract/base.rb
Expand Up @@ -15,10 +15,14 @@ def initialize

def process(action_name)
@_action_name = action_name
send(action_name)
process_action
self.response_obj[:body] = self.response_body
self
end

def process_action
send(action_name)
end

end
end
38 changes: 38 additions & 0 deletions actionpack/lib/action_controller/abstract/callbacks.rb
@@ -1,5 +1,43 @@
module AbstractController
module Callbacks
def self.included(klass)
klass.class_eval do
include ActiveSupport::NewCallbacks
define_callbacks :process_action
extend ClassMethods
end
end

def process_action
_run_process_action_callbacks(action_name) do
super
end
end

module ClassMethods
def _normalize_callback_options(options)
if only = options[:only]
only = Array(only).map {|o| "action_name == :#{o}"}.join(" && ")
options[:per_key] = {:if => only}
end
if except = options[:except]
except = Array(except).map {|e| "action_name == :#{e}"}.join(" && ")
options[:per_key] = {:unless => except}
end
end

[:before, :after, :around].each do |filter|
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
def #{filter}_filter(*names, &blk)
options = names.last.is_a?(Hash) ? names.pop : {}
_normalize_callback_options(options)
names.push(blk) if block_given?
names.each do |name|
process_action_callback(:#{filter}, name, options)
end
end
RUBY_EVAL
end
end
end
end
1 change: 1 addition & 0 deletions activesupport/lib/active_support.rb
Expand Up @@ -32,6 +32,7 @@ def self.load_all!
autoload :BufferedLogger, 'active_support/buffered_logger'
autoload :Cache, 'active_support/cache'
autoload :Callbacks, 'active_support/callbacks'
autoload :NewCallbacks, 'active_support/new_callbacks'
autoload :ConcurrentHash, 'active_support/concurrent_hash'
autoload :Deprecation, 'active_support/deprecation'
autoload :Duration, 'active_support/duration'
Expand Down

0 comments on commit c16c7a8

Please sign in to comment.