Skip to content

Namespace policy classes #3424

Open
Open
@Paul-Bob

Description

@Paul-Bob

In certain scenarios, it's desired to place policy classes that impact Avo within a specific namespace, like Avo::, to distinguish authorization logic between Avo resources and those from other segments of the application. This approach helps keep authorization files organized, ensuring a clear separation between Avo policies and others.

In this discussion, we converged on a solution to achieve this (thanks to @coatezy for sharing!):

module Avo
  class BaseResource < Avo::Resources::Base
    def initialize(...)
      super(...)

      namespace_authorization_policy
    end

    private

    def namespace_authorization_policy
      policy_class = "Avo::#{model_class}Policy".safe_constantize

      self.class.authorization_policy ||= policy_class || Avo::DefaultPolicy
    end
  end
end

Let's turn this discussion into a recipe

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions