Include view and helper extensions in current context

Instead of including extensions in hardcoded ActionController::Base, use
the current context from AS on_load hook, which in most cases should be
ActionController::Base, but may change in scenarios such as
ActionController::API (using the rails-api gem).

See spastorino/rails-api#28 for more info.

Closes #212
This commit is contained in:
Carlos Antonio da Silva 2012-05-28 09:55:42 -03:00
parent 6130a98c5f
commit d8779aa11d
3 changed files with 7 additions and 9 deletions

View File

@ -36,13 +36,13 @@ module Draper
initializer "draper.extend_action_controller_base" do |app|
ActiveSupport.on_load(:action_controller) do
Draper::System.setup(:action_controller)
Draper::System.setup(self)
end
end
initializer "draper.extend_action_mailer_base" do |app|
ActiveSupport.on_load(:action_mailer) do
Draper::System.setup(:action_mailer)
Draper::System.setup(self)
end
end

View File

@ -10,11 +10,9 @@ module Draper
end
def self.setup(component)
if component == :action_controller
ActionController::Base.send(:include, Draper::ViewContextFilter)
ActionController::Base.extend(Draper::HelperSupport)
elsif component == :action_mailer
ActionMailer::Base.send(:include, Draper::ViewContextFilter)
component.class_eval do
include Draper::ViewContextFilter
extend Draper::HelperSupport unless defined?(::ActionMailer) && self.is_a?(::ActionMailer::Base)
end
end
end

View File

@ -9,11 +9,11 @@ module ActionController
def self.before_filter(name)
@@before_filters << name
end
Draper::System.setup(self)
end
end
Draper::System.setup(:action_controller)
class ApplicationController < ActionController::Base
extend ActionView::Helpers
extend ActionView::Helpers::TagHelper