Class: Shoulda::Matchers::ActiveModel::Callbacks

Inherits:
Object
  • Object
show all
Defined in:
lib/shoulda/matchers/active_model/callbacks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(occures, action, method_name) ⇒ Callbacks

Returns a new instance of Callbacks.


21
22
23
24
25
# File 'lib/shoulda/matchers/active_model/callbacks.rb', line 21

def initialize(occures, action, method_name)
  @occures = occures.to_sym
  @action = action.to_sym
  @method_name = method_name.to_sym
end

Instance Attribute Details

#failure_message_for_shouldObject (readonly)

Returns the value of attribute failure_message_for_should


19
20
21
# File 'lib/shoulda/matchers/active_model/callbacks.rb', line 19

def failure_message_for_should
  @failure_message_for_should
end

#failure_message_for_should_notObject (readonly)

Returns the value of attribute failure_message_for_should_not


19
20
21
# File 'lib/shoulda/matchers/active_model/callbacks.rb', line 19

def failure_message_for_should_not
  @failure_message_for_should_not
end

Instance Method Details

#action_callbacksObject


46
47
48
49
50
51
52
# File 'lib/shoulda/matchers/active_model/callbacks.rb', line 46

def action_callbacks
  begin
    @subject.send("_#{@action}_callbacks")
  rescue NoMethodError
    raise "Subject must be Rails model class"
  end
end

#callback_nameObject


54
55
56
# File 'lib/shoulda/matchers/active_model/callbacks.rb', line 54

def callback_name
  "#{@occures}_#{@action}"
end

#callbacksObject


42
43
44
# File 'lib/shoulda/matchers/active_model/callbacks.rb', line 42

def callbacks
  action_callbacks.select{|cb| cb.kind.eql?(@occures) }.collect(&:filter)
end

#descriptionObject


38
39
40
# File 'lib/shoulda/matchers/active_model/callbacks.rb', line 38

def description
  "have #{@occures.to_s} #{@action.to_s} callback"
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)

27
28
29
30
31
32
33
34
35
36
# File 'lib/shoulda/matchers/active_model/callbacks.rb', line 27

def matches?(subject)
  @subject = subject
  if callbacks.include?(@method_name)
    @failure_message_for_should_not = "Didn't expect #{@subject.model_name} model to have #{callback_name} callback on method :#{@method_name}"
  else
    @failure_message_for_should = "Expected #{@subject.model_name} model to have #{callback_name} callback on method :#{@method_name}"
    return false
  end
  true
end