Class: Rails::AddOns::Shoulda::Matchers::ImplementUpdateActionMatcher

Inherits:
Object
  • Object
show all
Includes:
RSpec::Matchers
Defined in:
lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(spec) ⇒ ImplementUpdateActionMatcher


46
47
48
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 46

def initialize(spec)
  @spec = spec
end

Instance Method Details

#descriptionObject


178
179
180
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 178

def description
  "expose update action on #{@edit_path}"
end

#failure_messageObject


168
169
170
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 168

def failure_message
  "Should expose update action on #{@edit_path}. Error: #{@error}"
end

#failure_message_when_negatedObject Also known as: negative_failure_message


172
173
174
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 172

def failure_message_when_negated
  "Should not expose update action on #{@edit_path}. Error: #{@error}"
end

#for(resource) ⇒ Object

Resource that will be updated


51
52
53
54
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 51

def for(resource)
  @resource = resource
  self
end

#from(value) ⇒ Object


56
57
58
59
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 56

def from(value)
  @from = value
  self
end

#has_correct_attributes_afterObject


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 145

def has_correct_attributes_after
  expected = @to
  if @updating_block.present?
    given = @updating_block.call(@resource)

    if given == expected
      return true
    else
      @error = "After update state [#{given.inspect}] did not match expected [#{expected.inspect}]"
      return false
    end
  else
    given = @resource.attributes.with_indifferent_access.slice(*@to.keys)
    
    if given == expected
      true
    else
      @error = "Attributes after update [#{given}] did not match expected attributes [#{@expected_before_attributes}]"
      false
    end
  end
end

#has_correct_attributes_beforeObject


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 122

def has_correct_attributes_before
  expected = @from
  if @updating_block.present?
    given = @updating_block.call(@resource)

    if given == expected
      return true
    else
      @error = "Before update state [#{given.inspect}] did not match expected [#{expected.inspect}]"
      return false
    end
  else
    given = @resource.attributes.with_indifferent_access.slice(*@expected_before_attributes.keys)

    if given == expected
      true
    else
      @error = "Attributes before update [#{given}] did not match expected attributes [#{@expected_before_attributes}]"
      false
    end
  end
end

#has_correct_current_pathObject


113
114
115
116
117
118
119
120
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 113

def has_correct_current_path
  if @spec.current_path == @expected_path
    true
  else
    @error = "Wrong current path [#{@spec.current_path}] instead of [#{@expected_path}]"
    false
  end
end

#has_correct_status_codeObject


104
105
106
107
108
109
110
111
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 104

def has_correct_status_code
  if @spec.status_code == 200
    true
  else
    @error = "Wrong status code [#{@spec.status_code}] instead of [200]"
    false
  end
end

#idObject


78
79
80
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 78

def id
  @resource.to_param
end

#matches?(base_path) ⇒ Boolean


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 82

def matches?(base_path)
  @base_path = @spec.class.name.split('::')[0..2].join('::').constantize.description
  # @base_path     = base_path
  @show_path = "#{@base_path}/#{id}"
  @edit_path = "#{@base_path}/#{id}/edit"

  @expected_path = @show_path

  @spec.visit(@edit_path)

  return unless has_correct_attributes_before if @expected_before_attributes.present?

  @spec.within(@form_id) do
    @form_block.call
    @spec.find('input[name="commit"]').click
  end
              
  @resource.reload

  has_correct_status_code && has_correct_current_path && has_correct_attributes_after
end

#to(value) ⇒ Object


61
62
63
64
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 61

def to(value)
  @to = value
  self
end

#updating(&block) ⇒ Object


73
74
75
76
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 73

def updating(&block)
  @updating_block = block if block_given?
  self
end

#within_form(id, &block) ⇒ Object

Specifies the form css id to fill to create the resource.


67
68
69
70
71
# File 'lib/rails/add_ons/shoulda/matchers/implement_update_action_matcher.rb', line 67

def within_form(id, &block)
  @form_id = id
  @form_block = block
  self
end