class Shoulda::Matchers::ActiveModel::AllowMassAssignmentOfMatcher

@private

Attributes

failure_message[R]
failure_message_for_should[R]
failure_message_for_should_not[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 81
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 86
def as(role)
  if active_model_less_than_3_1?
    raise 'You can specify role only in Rails 3.1 or greater'
  end
  @options[:role] = role
  self
end
description() click to toggle source
# File lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb, line 119
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 94
def matches?(subject)
  @subject = subject
  if attr_mass_assignable?
    if whitelisting?
      @failure_message_when_negated = "#{@attribute} was made accessible"
    else
      if 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
    end
    true
  else
    if whitelisting?
      @failure_message = "Expected #{@attribute} to be accessible"
    else
      @failure_message = "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 143
def accessible_attributes
  @accessible_attributes ||= (@subject.class.accessible_attributes || [])
end
active_model_less_than_3_1?() click to toggle source
# File lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb, line 167
def active_model_less_than_3_1?
  ::ActiveModel::VERSION::STRING.to_f < 3.1
end
attr_mass_assignable?() click to toggle source
# File lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb, line 151
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 155
def authorizer
  if active_model_less_than_3_1?
    @subject.class.active_authorizer
  else
    @subject.class.active_authorizer[role]
  end
end
base_description() click to toggle source
# File lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb, line 125
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 163
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 139
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 135
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 129
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 147
def whitelisting?
  authorizer.kind_of?(::ActiveModel::MassAssignmentSecurity::WhiteList)
end