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.



126
127
128
129
130
# File 'lib/shoulda/matchers/active_record/have_attached_matcher.rb', line 126

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



124
125
126
# File 'lib/shoulda/matchers/active_record/have_attached_matcher.rb', line 124

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



124
125
126
# File 'lib/shoulda/matchers/active_record/have_attached_matcher.rb', line 124

def options
  @options
end

Instance Method Details

#descriptionObject



132
133
134
# File 'lib/shoulda/matchers/active_record/have_attached_matcher.rb', line 132

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

#expectationObject



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

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

#failure_messageObject



136
137
138
139
140
141
# File 'lib/shoulda/matchers/active_record/have_attached_matcher.rb', line 136

def failure_message
  "Expected \#{expectation}, but this could not be proved.\n  \#{@failure}\n  MESSAGE\nend\n"

#failure_message_when_negatedObject



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

def failure_message_when_negated
  "Did not expect \#{expectation}, but it does.\n  MESSAGE\nend\n"

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


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

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