Class: ShouldaExt::Matchers::TriggerCallbackMatcher

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

Defined Under Namespace

Modules: ActiveRecordHooks

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attach_active_record_callback_hooks!Object

Attach the ActiveRecord callback hooks needed for using the trigger_callbacks matcher



74
75
76
77
78
79
80
# File 'lib/shoulda_ext/matchers/trigger_callback.rb', line 74

def self.attach_active_record_callback_hooks!
  puts "Attaching callback hooks into ActiveRecord"
  ActiveRecord::Base.descendants.each do |model|
    TriggerCallbackMatcher::ActiveRecordHooks.attach_to model
  end
  ActiveRecord::Base.send :include, TriggerCallbackMatcher::ActiveRecordHooks
end

Instance Method Details

#anyObject

Check to see if any of the callback types have been triggered



90
91
92
93
94
# File 'lib/shoulda_ext/matchers/trigger_callback.rb', line 90

def any
  @callback_types = CALLBACK_TYPES
  @any_callbacks = true
  self
end

#descriptionObject



104
105
106
# File 'lib/shoulda_ext/matchers/trigger_callback.rb', line 104

def description
  "check that #{@callback_types.join(', ')} callbacks were called"
end

#expectationObject



108
109
110
# File 'lib/shoulda_ext/matchers/trigger_callback.rb', line 108

def expectation
  @expectations.join("\n")
end

#failure_messageObject



96
97
98
# File 'lib/shoulda_ext/matchers/trigger_callback.rb', line 96

def failure_message
  "Expected #{@subject} #{expectation}:"
end

#for(*callback_types) ⇒ Object

Define the set of callback types (create, update, destroy, save) to test



83
84
85
86
87
# File 'lib/shoulda_ext/matchers/trigger_callback.rb', line 83

def for(*callback_types)
  @callback_types = Array.wrap(callback_types)
  @any_callbacks = false
  self
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/shoulda_ext/matchers/trigger_callback.rb', line 112

def matches?(subject)
  @subject = subject
  @expectations = []
  result = !@any_callbacks
  @callback_types.each do |ct|
    CALLBACK_EVENTS.each do |ce|
      called = @subject.send(:"called_#{ce}_#{ct}?")
      if @any_callbacks
        result ||= called
      else
        result &&= called
      end
      @expectations << "#{ce}_#{ct} callbacks to be triggered"
    end
  end
  result
end

#negative_failure_messageObject



100
101
102
# File 'lib/shoulda_ext/matchers/trigger_callback.rb', line 100

def negative_failure_message
  "Did not expect #{@subject} #{expectation}:"
end