diff --git a/lib/princely.rb b/lib/princely.rb index 8dfdf5b..3d25d42 100644 --- a/lib/princely.rb +++ b/lib/princely.rb @@ -17,7 +17,7 @@ # require 'logger' require 'pathname' -require 'princely/rails' if defined?(Rails) +require 'princely/railtie' if defined?(Rails) module Princely autoload :StdoutLogger, 'princely/stdout_logger' diff --git a/lib/princely/rails.rb b/lib/princely/rails.rb deleted file mode 100644 index 48d9b75..0000000 --- a/lib/princely/rails.rb +++ /dev/null @@ -1,16 +0,0 @@ -require 'princely/pdf_helper' - -if Mime::Type.lookup_by_extension(:pdf) != 'application/pdf' - Mime::Type.register 'application/pdf', :pdf -end - -if defined?(Rails) - if Rails::VERSION::MAJOR >= 5 - ActionController::Base.send(:prepend, Princely::PdfHelper) - else - ActionController::Base.send(:include, Princely::PdfHelper) - end - ActionController::Base.send(:include, Princely::AssetSupport) if - (Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR > 0) || - (Rails::VERSION::MAJOR >= 4) -end diff --git a/lib/princely/railtie.rb b/lib/princely/railtie.rb new file mode 100644 index 0000000..93deae6 --- /dev/null +++ b/lib/princely/railtie.rb @@ -0,0 +1,27 @@ +require 'princely/pdf_helper' + +module Princely + class Railtie < Rails::Railtie + initializer "princely.helper" do + if Mime::Type.lookup_by_extension(:pdf) != 'application/pdf' + Mime::Type.register 'application/pdf', :pdf + end + + if ::Rails::VERSION::MAJOR >= 5 + ActiveSupport.on_load(:action_controller) do + prepend Princely::PdfHelper + end + else + ActiveSupport.on_load(:action_controller) do + include Princely::PdfHelper + end + end + if (::Rails::VERSION::MAJOR == 3 && ::Rails::VERSION::MINOR > 0) || + (::Rails::VERSION::MAJOR >= 4) + ActiveSupport.on_load(:action_controller) do + include Princely::AssetSupport + end + end + end + end +end