Class: Shoulda::Matchers::Pundit::PermittedToMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/shoulda/matchers/pundit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action) ⇒ PermittedToMatcher

Returns a new instance of PermittedToMatcher.



97
98
99
# File 'lib/shoulda/matchers/pundit.rb', line 97

def initialize(action)
  @action = action
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



95
96
97
# File 'lib/shoulda/matchers/pundit.rb', line 95

def action
  @action
end

#policyObject (readonly)

Returns the value of attribute policy.



95
96
97
# File 'lib/shoulda/matchers/pundit.rb', line 95

def policy
  @policy
end

#recordObject (readonly)

Returns the value of attribute record.



95
96
97
# File 'lib/shoulda/matchers/pundit.rb', line 95

def record
  @record
end

Instance Method Details

#by(policy) ⇒ Object

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



116
117
118
119
120
121
122
# 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

#descriptionObject



138
139
140
# File 'lib/shoulda/matchers/pundit.rb', line 138

def description
  "permits #{action} on #{record}"
end

#failure_message_for_shouldObject



124
125
126
127
128
129
# 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_notObject



131
132
133
134
135
136
# 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) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
104
105
# 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) ⇒ Object



107
108
109
110
# File 'lib/shoulda/matchers/pundit.rb', line 107

def on(record)
  @record = record
  self
end