Problem: When wrapping a model using SimpleDelegator
and passing to authorize!
an undefined method _policy_cache_key
error occurs.
This is, I think, because ActionPolicy uses refinements to add a method _policy_cache_key
to Object
.
However SimpleDelegator
does not inherit from Object
, but maybe BasicObject
.
To fix this we need to pass the unwrapped model to authorize!
or add the missing method, _policy_cache_key
, to our wrapper object.
authorize!(wrapped_model.__getobj__, ...)
or automatically:
def policy_for(record:, **opts)
record.class.ancestors.include?(SimpleDelegator) ? record.__getobj__ : record
super(record: record, **opts)
end
def _policy_cache_key(*)
SecureRandom.uuid
end
also see: