Class: Mongoid::Matchers::AllowMassAssignmentOfMatcher

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute) ⇒ AllowMassAssignmentOfMatcher

Returns a new instance of AllowMassAssignmentOfMatcher.



7
8
9
10
# File 'lib/matchers/allow_mass_assignment.rb', line 7

def initialize(attribute)
  @attribute = attribute.to_s
  @options = {}
end

Instance Attribute Details

#failure_messageObject (readonly)

Returns the value of attribute failure_message.



5
6
7
# File 'lib/matchers/allow_mass_assignment.rb', line 5

def failure_message
  @failure_message
end

#negative_failure_messageObject (readonly)

Returns the value of attribute negative_failure_message.



5
6
7
# File 'lib/matchers/allow_mass_assignment.rb', line 5

def negative_failure_message
  @negative_failure_message
end

Instance Method Details

#as(role) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/matchers/allow_mass_assignment.rb', line 12

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

#descriptionObject



45
46
47
# File 'lib/matchers/allow_mass_assignment.rb', line 45

def description
  "allow mass assignment of #{@attribute}"
end

#matches?(klass) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/matchers/allow_mass_assignment.rb', line 20

def matches?(klass)
  @klass = klass
  if attr_mass_assignable?
    if whitelisting?
      @negative_failure_message = "#{@attribute} was made accessible"
    else
      if protected_attributes.empty?
        @negative_failure_message = "no attributes were protected"
      else
        @negative_failure_message = "#{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