Class: Rails::AddOns::Shoulda::Matchers::ImplementDeleteActionMatcher

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

Instance Method Summary collapse

Constructor Details

#initialize(spec) ⇒ ImplementDeleteActionMatcher


25
26
27
# File 'lib/rails/add_ons/shoulda/matchers/implement_delete_action_matcher.rb', line 25

def initialize(spec)
  @spec = spec
end

Instance Method Details

#by(expected_reduction) ⇒ Object


44
45
46
47
# File 'lib/rails/add_ons/shoulda/matchers/implement_delete_action_matcher.rb', line 44

def by(expected_reduction)
  @expected_reduction = expected_reduction
  self
end

64
65
66
# File 'lib/rails/add_ons/shoulda/matchers/implement_delete_action_matcher.rb', line 64

def delete_link_text
  @delete_link_text ||= I18n.t('resources_controller.base.show_actions.delete')
end

#descriptionObject


105
106
107
# File 'lib/rails/add_ons/shoulda/matchers/implement_delete_action_matcher.rb', line 105

def description
  "expose delete action on #{@base_path} [#{delete_link_text}]"
end

#failure_messageObject


95
96
97
# File 'lib/rails/add_ons/shoulda/matchers/implement_delete_action_matcher.rb', line 95

def failure_message
  "Should expose delete action on #{@base_path} [#{delete_link_text}], clicking on #{delete_link_text}. Error: #{@error}"
end

#failure_message_when_negatedObject Also known as: negative_failure_message


99
100
101
# File 'lib/rails/add_ons/shoulda/matchers/implement_delete_action_matcher.rb', line 99

def failure_message_when_negated
  "Should not expose delete action on #{@base_path} [#{delete_link_text}], clicking on #{delete_link_text}. Error: #{@error}"
end

#for(resource) ⇒ Object

Resource that will be deleted


30
31
32
33
# File 'lib/rails/add_ons/shoulda/matchers/implement_delete_action_matcher.rb', line 30

def for(resource)
  @resource = resource
  self
end

#has_correct_current_pathObject


86
87
88
89
90
91
92
93
# File 'lib/rails/add_ons/shoulda/matchers/implement_delete_action_matcher.rb', line 86

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


68
69
70
71
72
73
74
75
# File 'lib/rails/add_ons/shoulda/matchers/implement_delete_action_matcher.rb', line 68

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

#has_reduced_resource_countObject


77
78
79
80
81
82
83
84
# File 'lib/rails/add_ons/shoulda/matchers/implement_delete_action_matcher.rb', line 77

def has_reduced_resource_count
  if (@before_delete_count - @after_delete_count) == @expected_reduction
    true
  else
    @error = "Did not reduce by expected [#{@expected_reduction}] but by [#{@before_delete_count - @after_delete_count}]"
    false
  end
end

#idObject


35
36
37
# File 'lib/rails/add_ons/shoulda/matchers/implement_delete_action_matcher.rb', line 35

def id
  @resource.to_param
end

#matches?(base_path) ⇒ Boolean


49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rails/add_ons/shoulda/matchers/implement_delete_action_matcher.rb', line 49

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

  @spec.visit(@show_path)

  @before_delete_count = @block.call
  @spec.click_link(delete_link_text)
  @after_delete_count = @block.call

  has_correct_status_code && has_correct_current_path && has_reduced_resource_count
end

#reducing(&block) ⇒ Object


39
40
41
42
# File 'lib/rails/add_ons/shoulda/matchers/implement_delete_action_matcher.rb', line 39

def reducing(&block)
  @block = block
  self
end