class Shoulda::Matchers::Pundit::PermittedToMatcher

Attributes

action[R]
policy[R]
record[R]

Public Class Methods

new(action) click to toggle source
# File lib/shoulda/matchers/pundit.rb, line 97
def initialize(action)
  @action = action
end

Public Instance Methods

by(policy) click to toggle source

alias_method :on_a, :on alias_method :a, :on alias_method :to, :on

# File lib/shoulda/matchers/pundit.rb, line 116
def by(policy)
  @policy = case policy
    when Class then lambda { |user, record| policy.new(user, record) }
    else policy
  end
  self
end
description() click to toggle source
# File lib/shoulda/matchers/pundit.rb, line 138
def description
  "permits #{action} on #{record}"
end
failure_message_for_should() click to toggle source
# File lib/shoulda/matchers/pundit.rb, line 124
        def failure_message_for_should
          <<-message
            User is not permitted to #{action} a #{policy.record} by #{policy.class}.
            #{policy.user.inspect}.
          message
        end
failure_message_for_should_not() click to toggle source
# File lib/shoulda/matchers/pundit.rb, line 131
        def failure_message_for_should_not
          <<-message
            User is not forbidden to #{action} a #{policy.record} by #{policy.class}.
            #{policy.user.inspect}.
          message
        end
matches?(user) click to toggle source
# File lib/shoulda/matchers/pundit.rb, line 101
def matches?(user)
  @policy ||= infer_policy(user)
  @policy = policy.call(user, record) if @policy.is_a? Proc
  policy.public_send("#{action}?")
end
on(record) click to toggle source
# File lib/shoulda/matchers/pundit.rb, line 107
def on(record)
  @record = record
  self
end

Private Instance Methods

infer_policy(user) click to toggle source
# File lib/shoulda/matchers/pundit.rb, line 144
def infer_policy(user)
  "#{record.class.name}Policy".constantize.new(user, record)
end