Class: Shoulda::Callback::Matchers::ActiveModel::CallbackMatcher

Inherits:
Object
  • Object
show all
Includes:
RailsVersionHelper
Defined in:
lib/shoulda/callback/matchers/active_model.rb

Overview

:nodoc:

Constant Summary collapse

VALID_OPTIONAL_LIFECYCLES =
[:validation, :commit, :rollback].freeze

Instance Method Summary collapse

Methods included from RailsVersionHelper

#rails_version

Constructor Details

#initialize(method) ⇒ CallbackMatcher

Returns a new instance of CallbackMatcher.



31
32
33
# File 'lib/shoulda/callback/matchers/active_model.rb', line 31

def initialize method
  @method = method
end

Instance Method Details

#callback_method_exists?(object, callback) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
85
86
# File 'lib/shoulda/callback/matchers/active_model.rb', line 76

def callback_method_exists? object, callback
  if is_class_callback?(object, callback) && !callback_object(object, callback).respond_to?(:"#{@hook}_#{@lifecycle}", true)
    @failure_message = "callback #{@method} is listed as a callback #{@hook} #{@lifecycle}#{optional_lifecycle_phrase}#{condition_phrase}, but the given object does not respond to #{@hook}_#{@lifecycle} (using respond_to?(:#{@hook}_#{@lifecycle}, true)"
    false
  elsif !is_class_callback?(object, callback) && !object.respond_to?(callback.filter, true)
    @failure_message = "callback #{@method} is listed as a callback #{@hook} #{@lifecycle}#{optional_lifecycle_phrase}#{condition_phrase}, but the model does not respond to #{@method} (using respond_to?(:#{@method}, true)"
    false
  else
    true
  end
end

#descriptionObject



100
101
102
# File 'lib/shoulda/callback/matchers/active_model.rb', line 100

def description
  "callback #{@method} #{@hook} #{@lifecycle}#{optional_lifecycle_phrase}#{condition_phrase}"
end

#failure_messageObject



88
89
90
# File 'lib/shoulda/callback/matchers/active_model.rb', line 88

def failure_message
  @failure_message || "expected #{@method} to be listed as a callback #{@hook} #{@lifecycle}#{optional_lifecycle_phrase}#{condition_phrase}, but was not"
end

#failure_message_when_negatedObject



92
93
94
# File 'lib/shoulda/callback/matchers/active_model.rb', line 92

def failure_message_when_negated
  @failure_message || "expected #{@method} not to be listed as a callback #{@hook} #{@lifecycle}#{optional_lifecycle_phrase}#{condition_phrase}, but was"
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/shoulda/callback/matchers/active_model.rb', line 63

def matches? subject
  check_preconditions!
  
  callbacks = subject.send :"_#{@lifecycle}_callbacks"
  callbacks.any? do |callback|
    has_callback?(subject, callback) &&
    matches_hook?(callback) && 
    matches_conditions?(callback) && 
    matches_optional_lifecycle?(callback) &&
    callback_method_exists?(subject, callback)
  end
end

#negative_failure_messageObject



96
97
98
# File 'lib/shoulda/callback/matchers/active_model.rb', line 96

def negative_failure_message
  failure_message_when_negated
end

#on(optional_lifecycle) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/shoulda/callback/matchers/active_model.rb', line 55

def on optional_lifecycle
  check_for_valid_optional_lifecycles!
  
  @optional_lifecycle = optional_lifecycle

  self
end