diff --git a/CHANGELOG.md b/CHANGELOG.md index b16cddbc..6e9ff01f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Added + +- Register policies directories for Rails 8 code statistics (#833) + ## 2.4.0 (2024-08-26) ## Changed diff --git a/lib/pundit.rb b/lib/pundit.rb index a03f4d3a..92ae9e51 100644 --- a/lib/pundit.rb +++ b/lib/pundit.rb @@ -11,6 +11,7 @@ require "pundit/context" require "pundit/cache_store/null_store" require "pundit/cache_store/legacy_store" +require "pundit/railtie" if defined?(Rails) # @api private # To avoid name clashes with common Error naming when mixing in Pundit, diff --git a/lib/pundit/railtie.rb b/lib/pundit/railtie.rb new file mode 100644 index 00000000..30ba1631 --- /dev/null +++ b/lib/pundit/railtie.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Pundit + class Railtie < Rails::Railtie + if Rails.version.to_f >= 8.0 + initializer "pundit.stats_directories" do + require "rails/code_statistics" + + if Rails.root.join("app/policies").directory? + Rails::CodeStatistics.register_directory("Policies", "app/policies") + end + + if Rails.root.join("test/policies").directory? + Rails::CodeStatistics.register_directory("Policy tests", "test/policies", test_directory: true) + end + end + end + end +end