Skip to content

Instantly share code, notes, and snippets.

@markedmondson
Created October 8, 2024 23:10
Show Gist options
  • Save markedmondson/696b90e5fc451e5295e7f180725a97e1 to your computer and use it in GitHub Desktop.
Save markedmondson/696b90e5fc451e5295e7f180725a97e1 to your computer and use it in GitHub Desktop.
Policy example
module FeaturePolicy
extend ActiveSupport::Concern
class_methods do
def policy_access_klass(klass)
self.policy_access_klass = klass
end
included do
class_attribute :policy_access_klass,
instance_accessor: false,
instance_predicate: false,
default: ApplicationPolicy
pre_check :access?
end
end
def access?
raise NotImplementedError, "FeaturePolicy#access? must be implemented in #{self.policy_feature_klass}"
end
end
class ApplicationPolicy
include FeaturePolicy
end
class Audit::ApplicationPolicy < ApplicationPolicy
policy_access_klass self
def access?
# Should we have a new OrganizationFeature model? checks could be backed by a standard feature flag format
OrganizationSetting.enabled?(:audit)
end
end
class Audit::ResultPolicy < Audit::ApplicationPolicy
def show
# ...
end
end
class Discuss::ApplicationPolicy < ApplicationPolicy
policy_access_klass self
def access?
# Should we have a new OrganizationFeature model? checks could be backed by a standard feature flag format
OrganizationSetting.enabled?(:discuss)
end
end
class Discuss::GroupPolicy < Discuss::ApplicationPolicy
def show
# ...
end
end
class Discuss::MessagePolicy < Discuss::ApplicationPolicy
def show
# ...
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment