class Shoulda::Matchers::ActiveModel::AllowMassAssignmentOfMatcher

@private

Attributes

failure_message[R]
failure_message_when_negated[R]

Public Class Methods

new(attribute) click to toggle source
# File lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb, line 78
def initialize(attribute)
  @attribute = attribute.to_s
  @options = {}
end

Public Instance Methods

as(role) click to toggle source
# File lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb, line 83
def as(role)
  @options[:role] = role
  self
end
description() click to toggle source
# File lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb, line 114
def description
  [base_description, role_description].compact.join(' ')
end
matches?(subject) click to toggle source
# File lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb, line 88
def matches?(subject)
  @subject = subject
  if attr_mass_assignable?
    if whitelisting?
      @failure_message_when_negated = "#{@attribute} was made "\
        'accessible'
    elsif protected_attributes.empty?
      @failure_message_when_negated = 'no attributes were protected'
    else
      @failure_message_when_negated =
        "#{class_name} is protecting " <<
        "#{protected_attributes.to_a.to_sentence}, " <<
        "but not #{@attribute}."
    end
    true
  else
    @failure_message =
      if whitelisting?
        "Expected #{@attribute} to be accessible"
      else
        "Did not expect #{@attribute} to be protected"
      end
    false
  end
end

Private Instance Methods

accessible_attributes() click to toggle source
# File lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb, line 138
def accessible_attributes
  @_accessible_attributes ||=
    (@subject.class.accessible_attributes || [])
end
attr_mass_assignable?() click to toggle source
# File lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb, line 147
def attr_mass_assignable?
  !authorizer.deny?(@attribute)
end
authorizer() click to toggle source
# File lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb, line 151
def authorizer
  @subject.class.active_authorizer[role]
end
base_description() click to toggle source
# File lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb, line 120
def base_description
  "allow mass assignment of #{@attribute}"
end
class_name() click to toggle source
# File lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb, line 155
def class_name
  @subject.class.name
end
protected_attributes() click to toggle source
# File lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb, line 134
def protected_attributes
  @_protected_attributes ||= (@subject.class.protected_attributes || [])
end
role() click to toggle source
# File lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb, line 130
def role
  @options[:role] || :default
end
role_description() click to toggle source
# File lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb, line 124
def role_description
  if role != :default
    "as #{role}"
  end
end
whitelisting?() click to toggle source
# File lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb, line 143
def whitelisting?
  authorizer.is_a?(::ActiveModel::MassAssignmentSecurity::WhiteList)
end