Class: ActiveStorageValidations::Matchers::ContentTypeValidatorMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/active_storage_validations/matchers/content_type_validator_matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(attribute_name) ⇒ ContentTypeValidatorMatcher

Returns a new instance of ContentTypeValidatorMatcher.



12
13
14
# File 'lib/active_storage_validations/matchers/content_type_validator_matcher.rb', line 12

def initialize(attribute_name)
  @attribute_name = attribute_name
end

Instance Method Details

#allowing(*types) ⇒ Object



20
21
22
23
# File 'lib/active_storage_validations/matchers/content_type_validator_matcher.rb', line 20

def allowing(*types)
  @allowed_types = types.flatten
  self
end

#descriptionObject



16
17
18
# File 'lib/active_storage_validations/matchers/content_type_validator_matcher.rb', line 16

def description
  "validate the content types allowed on attachment #{@attribute_name}"
end

#failure_messageObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/active_storage_validations/matchers/content_type_validator_matcher.rb', line 35

def failure_message
  <<~MESSAGE
    Expected #{@attribute_name}

      Accept content types: #{allowed_types.join(", ")}
        #{accepted_types_and_failures}

      Reject content types: #{rejected_types.join(", ")}
        #{rejected_types_and_failures}
  MESSAGE
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/active_storage_validations/matchers/content_type_validator_matcher.rb', line 30

def matches?(subject)
  @subject = subject.is_a?(Class) ? subject.new : subject
  responds_to_methods && allowed_types_allowed? && rejected_types_rejected?
end

#rejecting(*types) ⇒ Object



25
26
27
28
# File 'lib/active_storage_validations/matchers/content_type_validator_matcher.rb', line 25

def rejecting(*types)
  @rejected_types = types.flatten
  self
end