concept TicketPolicy in category rails

appears as: TicketPolicy, TicketPolicy
Rails 4 in Action: Revised Edition of Rails 3 in Action

This is an excerpt from Manning's book Rails 4 in Action: Revised Edition of Rails 3 in Action.

You’ve called authorize with an instance of a Project, so Pundit has determined that it should use a ProjectPolicy class to check permissions for it. (If you had tried to authorize a Ticket instance, it would look for a TicketPolicy class instead. This is another example of convention over configuration.)

8.3.2. Testing the TicketPolicy

You can adopt your new testing style when you write your new TicketPolicy specs. Open spec/policies/ticket_policy_spec.rb and replace the contents of the file with the following listing.

Listing 8.41. spec/policies/ticket_policy_spec.rb
require "rails_helper" RSpec.describe TicketPolicy do context "permissions" do subject { TicketPolicy.new(user, ticket) } let(:user) { FactoryGirl.create(:user) } let(:project) { FactoryGirl.create(:project) } let(:ticket) { FactoryGirl.create(:ticket, project: project) } context "for anonymous users" do let(:user) { nil } it { should_not permit_action :show } end context "for viewers of the project" do before { assign_role!(user, :viewer, project) } it { should permit_action :show } end context "for editors of the project" do before { assign_role!(user, :editor, project) } it { should permit_action :show } end context "for managers of the project" do before { assign_role!(user, :manager, project) } it { should permit_action :show } end context "for managers of other projects" do before do assign_role!(user, :manager, FactoryGirl.create(:project)) end it { should_not permit_action :show } end context "for administrators" do let(:user) { FactoryGirl.create :user, :admin } it { should permit_action :show } end end end