Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Convert Draper to using a Railtie for loading

  • Loading branch information...
commit 051dec8360eac93eb4dc1b4a2d6b50c7a7332cbe 1 parent de2d62c
Kevin Wang and Stephen Caudill authored Hashrocket Workstation committed
View
2  lib/draper.rb
@@ -7,5 +7,5 @@
require 'draper/view_context'
require 'draper/decorated_enumerable_proxy'
require 'draper/rspec_integration' if defined?(RSpec) and RSpec.respond_to?(:configure)
+require 'draper/railtie'
-Draper::System.setup
View
19 lib/draper/railtie.rb
@@ -0,0 +1,19 @@
+require 'rails/railtie'
+
+module Draper
+ class Railtie < Rails::Railtie
+
+ initializer "draper.extend_action_controller_base" do |app|
+ ActiveSupport.on_load(:action_controller) do
+ Draper::System.setup(:action_controller)
+ end
+ end
+
+ initializer "draper.extend_action_mailer_base" do |app|
+ ActiveSupport.on_load(:action_mailer) do
+ Draper::System.setup(:action_mailer)
+ end
+ end
+
+ end
+end
View
11 lib/draper/system.rb
@@ -1,9 +1,12 @@
module Draper
class System
- def self.setup
- ActionController::Base.send(:include, Draper::ViewContextFilter) if defined?(ActionController::Base)
- ActionMailer::Base.send(:include, Draper::ViewContextFilter) if defined?(ActionMailer::Base)
- ActionController::Base.send(:helper, Draper::HelperSupport) if defined?(ActionController::Base)
+ 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)
+ end
end
end
end
View
7 spec/support/samples/application_controller.rb
@@ -9,12 +9,11 @@ def self.before_filters
def self.before_filter(name)
@@before_filters << name
end
- def self.helper(mod)
- extend mod
- end
end
end
+Draper::System.setup(:action_controller)
+
class ApplicationController < ActionController::Base
extend ActionView::Helpers
extend ActionView::Helpers::TagHelper
@@ -42,5 +41,3 @@ def self.capture_triggered
@@capture ||= false
end
end
-
-Draper::System.setup
Please sign in to comment.
Something went wrong with that request. Please try again.