diff --git a/lib/draper/railtie.rb b/lib/draper/railtie.rb index 5334994..2981f6a 100644 --- a/lib/draper/railtie.rb +++ b/lib/draper/railtie.rb @@ -3,6 +3,28 @@ require 'rails/railtie' module Draper class Railtie < Rails::Railtie + ## + # Decorators are loaded in + # => at app boot in non-development environments + # => after each request in the development environment + # + # This is necessary because we might never explicitly reference + # Decorator constants. + # + config.to_prepare do + ::Draper::System.load_app_local_decorators + end + + ## + # The `app/decorators` path is eager loaded + # + # This is the standard "Rails Way" to add paths from which constants + # can be loaded. + # + config.before_initialize do |app| + app.config.paths.add 'app/decorators', :eager_load => true + end + initializer "draper.extend_action_controller_base" do |app| ActiveSupport.on_load(:action_controller) do Draper::System.setup(:action_controller) diff --git a/lib/draper/system.rb b/lib/draper/system.rb index cea1643..ad375e3 100644 --- a/lib/draper/system.rb +++ b/lib/draper/system.rb @@ -1,5 +1,14 @@ module Draper class System + def self.app_local_decorator_glob + 'app/decorators/**/*_decorator.rb' + end + + def self.load_app_local_decorators + decorator_files = Dir[ "#{ Rails.root }/#{ app_local_decorator_glob }" ] + decorator_files.each { |d| require_dependency d } + end + def self.setup(component) if component == :action_controller ActionController::Base.send(:include, Draper::ViewContextFilter)