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
15
# File 'lib/active_storage_validations/matchers/content_type_validator_matcher.rb', line 12

def initialize(attribute_name)
  @attribute_name = attribute_name
  @custom_message = nil
end

Instance Method Details

#allowing(*types) ⇒ Object



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

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

#descriptionObject



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

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

#failure_messageObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/active_storage_validations/matchers/content_type_validator_matcher.rb', line 41

def failure_message
  message = ["Expected #{@attribute_name}"]

  if @allowed_types
    message << "Accept content types: #{allowed_types.join(", ")}"
    message << "#{@missing_allowed_types.join(", ")} were rejected"
  end

  if @rejected_types
    message << "Reject content types: #{rejected_types.join(", ")}"
    message << "#{@missing_rejected_types.join(", ")} were accepted"
  end

  message.join("\n")
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/active_storage_validations/matchers/content_type_validator_matcher.rb', line 36

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

#rejecting(*types) ⇒ Object



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

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

#with_message(message) ⇒ Object



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

def with_message(message)
  @custom_message = message
  self
end