Nested class definition

I have a pundit policy class that declares another class inside:

class PaymentPolicy < ApplicationPolicy
  class Scope < Scope
    def resolve
      return scope.all if user.root?
      scope.where(user: user)
    end
  end
end

      

Now I want to use the same inner class Scope

elsewhere. One way is to define it in a superclass and override it, but for that I need to create another class class NewClass < ApplicationPolicy

and inherit PaymentPolicy

from it. But I like ActiveSupport::Concern

and don't know how to put the class definition inside the module. it

module UserAllowed
  extend ActiveSupport::Concern
  included do
    class Scope < Scope
      def resolve
        return scope.all if user.root?
        scope.where(user: user)
      end
    end
  end
end

      

does not work. Nothing:

   module UserAllowed
      extend ActiveSupport::Concern
      class Scope < Scope
        def resolve
          return scope.all if user.root?
          scope.where(user: user
        end
      end
    end

      

How can I put the nested class definition inside a helper?

+3


source to share





All Articles