diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d2195d5..b509deb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- Update `ApplicationPolicy`` generator to qualify the `Scope` class name (#792) + - Policy generator uses `NoMethodError` to indicate `#resolve` is not implemented (#776) ## 2.3.1 (2023-07-17) diff --git a/README.md b/README.md index 36d74d13..d40d2f53 100644 --- a/README.md +++ b/README.md @@ -279,7 +279,7 @@ generator, or create your own base class to inherit from: ``` ruby class PostPolicy < ApplicationPolicy - class Scope < Scope + class Scope < ApplicationPolicy::Scope def resolve if user.admin? scope.all @@ -476,7 +476,7 @@ example, associations which might be `nil`. ```ruby class NilClassPolicy < ApplicationPolicy - class Scope < Scope + class Scope < ApplicationPolicy::Scope def resolve raise Pundit::NotDefinedError, "Cannot scope NilClass" end diff --git a/lib/generators/pundit/policy/templates/policy.rb b/lib/generators/pundit/policy/templates/policy.rb index 6798550b..c802b94c 100644 --- a/lib/generators/pundit/policy/templates/policy.rb +++ b/lib/generators/pundit/policy/templates/policy.rb @@ -1,6 +1,12 @@ <% module_namespacing do -%> class <%= class_name %>Policy < ApplicationPolicy - class Scope < Scope + # NOTE: Prior to Pundit v2.4.0, the inheritance was declared as + # `Scope < Scope` rather than `Scope < ApplicationPolicy`. In most cases the + # behavior will be identical, but if updating exisiting code, beware of + # possible changes to the ancestors: + # https://gist.github.com/Burgestrand/4b4bc22f31c8a95c425fc0e30d7ef1f5 + + class Scope < ApplicationPolicy::Scope # NOTE: Be explicit about which records you allow access to! # def resolve # scope.all