The easiest way to do this is probably to rescue from the Pundit::NotDefinedError
raised by Pundit::PolicyFinder#policy!
.
Given your default policy:
# app/policies/application_policy.rb:
ApplicationPolicy = Struct.new :user, :record do
def index?
true
end
def show?
true
end
# ... other sensible defaults
end
Assuming you are using this in a Rails controller:
# app/controllers/application_controller.rb:
class ApplicationController
include Pundit
# ...
private
def policy(record)
super
rescue Pundit::NotDefinedError
ApplicationPolicy.new pundit_user, record
end
end
Disclaimer: I should also state that creating implicit default policies is generally frowned upon. Except in the simplest cases or with the most pessimistic defaults (all false), it is all too easy for an overly permissive policy to slip through.