class Shoulda::Matchers::ActiveRecord::HaveAttachedMatcher

@private

Attributes

macro[R]
name[R]
subject[R]

Public Class Methods

new(macro, name) click to toggle source
# File lib/shoulda/matchers/active_record/have_attached_matcher.rb, line 58
def initialize(macro, name)
  @macro = macro
  @name = name
end

Public Instance Methods

description() click to toggle source
# File lib/shoulda/matchers/active_record/have_attached_matcher.rb, line 63
def description
  "have a has_#{macro}_attached called #{name}"
end
expectation() click to toggle source
# File lib/shoulda/matchers/active_record/have_attached_matcher.rb, line 80
def expectation
  "#{model_class.name} to #{description}"
end
failure_message() click to toggle source
# File lib/shoulda/matchers/active_record/have_attached_matcher.rb, line 67
        def failure_message
          <<-MESSAGE
Expected #{expectation}, but this could not be proved.
  #{@failure}
          MESSAGE
        end
failure_message_when_negated() click to toggle source
# File lib/shoulda/matchers/active_record/have_attached_matcher.rb, line 74
        def failure_message_when_negated
          <<-MESSAGE
Did not expect #{expectation}, but it does.
          MESSAGE
        end
matches?(subject) click to toggle source
# File lib/shoulda/matchers/active_record/have_attached_matcher.rb, line 84
def matches?(subject)
  @subject = subject
  reader_attribute_exists? &&
    writer_attribute_exists? &&
    attachments_association_exists? &&
    blobs_association_exists? &&
    eager_loading_scope_exists?
end

Private Instance Methods

attachments_association_exists?() click to toggle source
# File lib/shoulda/matchers/active_record/have_attached_matcher.rb, line 115
def attachments_association_exists?
  if attachments_association_matcher.matches?(subject)
    true
  else
    @failure = attachments_association_matcher.failure_message
    false
  end
end
attachments_association_matcher() click to toggle source
# File lib/shoulda/matchers/active_record/have_attached_matcher.rb, line 124
def attachments_association_matcher
  @_attachments_association_matcher ||=
    AssociationMatcher.new(
      :"has_#{macro}",
      attachments_association_name,
    ).
      conditions(name: name).
      class_name('ActiveStorage::Attachment').
      inverse_of(:record)
end
attachments_association_name() click to toggle source
# File lib/shoulda/matchers/active_record/have_attached_matcher.rb, line 135
def attachments_association_name
  case macro
  when :one then "#{name}_attachment"
  when :many then "#{name}_attachments"
  end
end
blobs_association_exists?() click to toggle source
# File lib/shoulda/matchers/active_record/have_attached_matcher.rb, line 142
def blobs_association_exists?
  if blobs_association_matcher.matches?(subject)
    true
  else
    @failure = blobs_association_matcher.failure_message
    false
  end
end
blobs_association_matcher() click to toggle source
# File lib/shoulda/matchers/active_record/have_attached_matcher.rb, line 151
def blobs_association_matcher
  @_blobs_association_matcher ||=
    AssociationMatcher.new(
      :"has_#{macro}",
      blobs_association_name,
    ).
      through(attachments_association_name).
      class_name('ActiveStorage::Blob').
      source(:blob)
end
blobs_association_name() click to toggle source
# File lib/shoulda/matchers/active_record/have_attached_matcher.rb, line 162
def blobs_association_name
  case macro
  when :one then "#{name}_blob"
  when :many then "#{name}_blobs"
  end
end
eager_loading_scope_exists?() click to toggle source
# File lib/shoulda/matchers/active_record/have_attached_matcher.rb, line 169
def eager_loading_scope_exists?
  if model_class.respond_to?("with_attached_#{name}")
    true
  else
    @failure = "#{model_class.name} does not have a " \
               ":with_attached_#{name} scope."
    false
  end
end
model_class() click to toggle source
# File lib/shoulda/matchers/active_record/have_attached_matcher.rb, line 179
def model_class
  subject.class
end
reader_attribute_exists?() click to toggle source
# File lib/shoulda/matchers/active_record/have_attached_matcher.rb, line 97
def reader_attribute_exists?
  if subject.respond_to?(name)
    true
  else
    @failure = "#{model_class.name} does not have a :#{name} method."
    false
  end
end
writer_attribute_exists?() click to toggle source
# File lib/shoulda/matchers/active_record/have_attached_matcher.rb, line 106
def writer_attribute_exists?
  if subject.respond_to?("#{name}=")
    true
  else
    @failure = "#{model_class.name} does not have a :#{name}= method."
    false
  end
end