pundit icon indicating copy to clipboard operation
pundit copied to clipboard

feat: Add customizable permit description

Open barelyknown opened this issue 2 years ago • 2 comments

In this common example, the default description is noisy and doesn't add any information:

🔈 Default output:

ActionItemPolicy
  show?
    grants access to a customer manager member
    when the user is a customer operations member
      when there is no relationship to the action item
        is expected not to permit #<User id: 2, email_address: "[email protected]", mobile_number: nil, created_at: "2023-02-18 14:3...ation_event_at: nil, release_note_feed_token: nil, opt_out_of_check_in_request_notifications: false> and #<ActionItem id: 2, title: "Fix all the things!", responsible_organization_type: "Customer", responsi...quires_xbe_feature: false, meeting_id: nil, created_by_id: nil, project_id: nil, root_cause_id: nil>

This PR enables you to customize the description.

Pundit::RSpec::Matchers.description do
  "permit the user"
end

💆 Customized output:

ActionItemPolicy
  show?
    grants access to a customer manager member
    when the user is a customer operations member
      when there is no relationship to the action item
        is expected not to permit the user
  • closes #759

barelyknown avatar Feb 18 '23 14:02 barelyknown

Thank you! I'm positive about this feature, but I'm not sure about the interface. I also need to borrow myself some time to test it out before I'm willing to merge it.

  • I'm leaning to a regular module-level attr_accessor is more than good enough, similar (but not exactly) like what's in #761
module Pundit
  module RSpec
    module Matchers
      class << self
        attr_accessor(:description)
      end
    end
  end
end
  • If we are to use a block, passing the user and record might be prudent to allow for this:
Pundit::RSpec::Matchers.description = ->(user, record) { "permit user with role #{user.role}" }

Burgestrand avatar Mar 10 '23 11:03 Burgestrand

I merged my branch from https://github.com/varvet/pundit/pull/761 into this one

bf4 avatar Jul 12 '23 17:07 bf4

Hi! Merged in #806

Burgestrand avatar May 13 '24 07:05 Burgestrand