lib/action_policy/railtie.rb in action_policy-0.6.5 vs lib/action_policy/railtie.rb in action_policy-0.6.6

- old
+ new

@@ -2,18 +2,10 @@ module ActionPolicy # :nodoc: require "action_policy/rails/controller" require "action_policy/rails/channel" - if defined?(::ActionController) - require "action_policy/rails/scope_matchers/action_controller_params" - end - - if defined?(::ActiveRecord) - require "action_policy/rails/scope_matchers/active_record" - end - class Railtie < ::Rails::Railtie # :nodoc: # Provides Rails-specific configuration, # accessible through `Rails.application.config.action_policy` module Config class << self @@ -63,34 +55,29 @@ end config.action_policy = Config initializer "action_policy.clear_per_thread_cache" do |app| - if ::Rails::VERSION::MAJOR >= 5 - app.executor.to_run { ActionPolicy::PerThreadCache.clear_all } - app.executor.to_complete { ActionPolicy::PerThreadCache.clear_all } - else - require "action_policy/cache_middleware" - app.middleware.use ActionPolicy::CacheMiddleware - end + app.executor.to_run { ActionPolicy::PerThreadCache.clear_all } + app.executor.to_complete { ActionPolicy::PerThreadCache.clear_all } end config.after_initialize do - next unless ::Rails.application.config.action_policy.instrumentation_enabled + if ::Rails.application.config.action_policy.instrumentation_enabled + require "action_policy/rails/policy/instrumentation" + require "action_policy/rails/authorizer" - require "action_policy/rails/policy/instrumentation" - require "action_policy/rails/authorizer" + ActionPolicy::Base.prepend ActionPolicy::Policy::Rails::Instrumentation + ActionPolicy::Authorizer.singleton_class.prepend ActionPolicy::Rails::Authorizer + end - ActionPolicy::Base.prepend ActionPolicy::Policy::Rails::Instrumentation - ActionPolicy::Authorizer.singleton_class.prepend ActionPolicy::Rails::Authorizer - end - - config.to_prepare do |_app| ActionPolicy::LookupChain.namespace_cache_enabled = ::Rails.application.config.action_policy.namespace_cache_enabled ActiveSupport.on_load(:action_controller) do + require "action_policy/rails/scope_matchers/action_controller_params" + next unless ::Rails.application.config.action_policy.auto_inject_into_controller ActionController::Base.include ActionPolicy::Controller next unless ::Rails.application.config.action_policy.controller_authorize_current_user @@ -108,9 +95,22 @@ ActionCable::Channel::Base.authorize :user, through: :current_user end ActiveSupport.on_load(:active_record) do require "action_policy/rails/ext/active_record" + require "action_policy/rails/scope_matchers/active_record" + end + + # Trigger load hooks of the components that extend ActionPolicy itself + # (e.g., scope matchers) + begin + ::ActionController::Base + rescue NameError + end + + begin + ::ActiveRecord::Base + rescue NameError end end end end