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..db6eddda --- /dev/null +++ b/lib/pundit/railtie.rb @@ -0,0 +1,21 @@ +# 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" + + Rails::CodeStatistics.register_directory( + "Policies", Rails.root.join("app/policies").to_s + ) + + %w[test/policies spec/policies].each do |dir| + path = Rails.root.join(dir) + + Rails::CodeStatistics.register_directory("Policies tests", path.to_s, test_directory: true) if path.exist? + end + end + end + end +end