Class: Shoulda::Matchers::ActiveRecord::HaveAttachedMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/shoulda/matchers/active_record/have_attached_matcher.rb

Constant Summary collapse

OPTION_METHODS =
{
  service: -> (value) { value },
  strict_loading: -> (value = true) { value },
  dependent: -> (value) { value },
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(macro, name) ⇒ HaveAttachedMatcher

Returns a new instance of HaveAttachedMatcher.



147
148
149
150
151
# File 'lib/shoulda/matchers/active_record/have_attached_matcher.rb', line 147

def initialize(macro, name)
  @macro = macro
  @name = name
  @options = {}
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



145
146
147
# File 'lib/shoulda/matchers/active_record/have_attached_matcher.rb', line 145

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



145
146
147
# File 'lib/shoulda/matchers/active_record/have_attached_matcher.rb', line 145

def options
  @options
end

Instance Method Details

#descriptionObject



153
154
155
# File 'lib/shoulda/matchers/active_record/have_attached_matcher.rb', line 153

def description
  "have a has_#{macro}_attached called #{name}"
end

#expectationObject



174
175
176
# File 'lib/shoulda/matchers/active_record/have_attached_matcher.rb', line 174

def expectation
  "#{model_class.name} to #{description}" + build_expectation_suffix
end

#failure_messageObject



157
158
159
160
161
162
# File 'lib/shoulda/matchers/active_record/have_attached_matcher.rb', line 157

def failure_message
  <<-MESSAGE
Expected #{expectation}, but this could not be proved.
  #{@failure}
  MESSAGE
end

#failure_message_when_negatedObject



168
169
170
171
172
# File 'lib/shoulda/matchers/active_record/have_attached_matcher.rb', line 168

def failure_message_when_negated
  <<-MESSAGE
Did not expect #{expectation}, but it does.
  MESSAGE
end

#failure_reasonObject



164
165
166
# File 'lib/shoulda/matchers/active_record/have_attached_matcher.rb', line 164

def failure_reason
  @failure
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


178
179
180
181
182
183
184
185
186
187
# File 'lib/shoulda/matchers/active_record/have_attached_matcher.rb', line 178

def matches?(subject)
  @subject = subject
  reader_attribute_exists? &&
    writer_attribute_exists? &&
    attachments_association_exists? &&
    blobs_association_exists? &&
    eager_loading_scope_exists? &&
    service_correct? &&
    dependent_option_correct?
end