Skip to content

Commit

Permalink
Convert Draper to using a Railtie for loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Wang and Stephen Caudill authored and hashrocketeer committed Feb 24, 2012
1 parent de2d62c commit 051dec8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/draper.rb
Expand Up @@ -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
19 changes: 19 additions & 0 deletions 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
11 changes: 7 additions & 4 deletions 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
7 changes: 2 additions & 5 deletions spec/support/samples/application_controller.rb
Expand Up @@ -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
Expand Down Expand Up @@ -42,5 +41,3 @@ def self.capture_triggered
@@capture ||= false
end
end

Draper::System.setup

0 comments on commit 051dec8

Please sign in to comment.