Class: ActiveStorageValidations::Matchers::ContentTypeValidatorMatcher

Inherits:
Object
  • Object
show all
Includes:
Validatable
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.



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

def initialize(attribute_name)
  @attribute_name = attribute_name
  @allowed_types = @rejected_types = []
  @custom_message = nil
end

Instance Method Details

#allowing(*types) ⇒ Object



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

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

#descriptionObject



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

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

#failure_messageObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/active_storage_validations/matchers/content_type_validator_matcher.rb', line 51

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

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

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

  message.join("\n")
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
# File 'lib/active_storage_validations/matchers/content_type_validator_matcher.rb', line 42

def matches?(subject)
  @subject = subject.is_a?(Class) ? subject.new : subject

  responds_to_methods &&
    all_allowed_types_allowed? &&
    all_rejected_types_rejected? &&
    validate_custom_message?
end

#rejecting(*types) ⇒ Object



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

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

#with_message(message) ⇒ Object



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

def with_message(message)
  @custom_message = message
  self
end