Support for headless policies
aried3r opened this issue · 5 comments
Are there any plans to add support for headless policies?
Currently, because of the references to record
, this happens:
NoMethodError: undefined method `record' for #<DashboardPolicy:0x007faee3454130>
Could you please paste or link to the code which is causing that error? I created a policy like this:
class DashboardPolicy < Struct.new(:user, :record)
def show?
true
end
end
With this corresponding spec:
require 'rails_helper'
describe DashboardPolicy do
subject { DashboardPolicy.new(user, :dashboard) }
context "being a visitor" do
let(:user) { nil }
it { should permit_action(:show) }
end
end
With this code, the test passed without error.
Sorry for the late reply. It was a mistake on my side, we were still using custom matchers with the exact same name and that was causing the problems, but only with headless policies which is why I thought it might be this gem.
Everything works now, however the error messages are not as useful for headless policies:
class DashboardPolicy < Struct.new(:user, :dashboard)
def index?
user.admin?
end
end
require 'rails_helper'
RSpec.describe DashboardPolicy do
subject { described_class.new(user, :dashboard) }
describe 'as an admin user' do
let(:user) { FactoryGirl.create :user, :admin }
it { is_expected.to forbid_action(:index) }
end
end
Result:
NoMethodError: undefined method `record' for #<DashboardPolicy:0x007fab38748d68>
The error messages could certainly be improved. This will require some refactoring. I'll leave the issue open; if/when there's time I'll see what can be done to make the error messages more intuitive.
Thanks. I've merged the PR and released it as version 1.3.1.