class ActiveStorageValidations::Matchers::AttachedValidatorMatcher
Public Class Methods
new(attribute_name)
click to toggle source
# File lib/active_storage_validations/matchers/attached_validator_matcher.rb, line 10 def initialize(attribute_name) @attribute_name = attribute_name end
Public Instance Methods
description()
click to toggle source
# File lib/active_storage_validations/matchers/attached_validator_matcher.rb, line 14 def description "validate #{@attribute_name} must be attached" end
failure_message()
click to toggle source
# File lib/active_storage_validations/matchers/attached_validator_matcher.rb, line 23 def failure_message "is expected to validate attached of #{@attribute_name}" end
failure_message_when_negated()
click to toggle source
# File lib/active_storage_validations/matchers/attached_validator_matcher.rb, line 27 def failure_message_when_negated "is expected to not validate attached of #{@attribute_name}" end
matches?(subject)
click to toggle source
# File lib/active_storage_validations/matchers/attached_validator_matcher.rb, line 18 def matches?(subject) @subject = subject.is_a?(Class) ? subject.new : subject responds_to_methods && valid_when_attached && invalid_when_not_attached end
Private Instance Methods
attachable()
click to toggle source
# File lib/active_storage_validations/matchers/attached_validator_matcher.rb, line 54 def attachable { io: Tempfile.new('.'), filename: 'dummy.txt', content_type: 'text/plain' } end
invalid_when_not_attached()
click to toggle source
# File lib/active_storage_validations/matchers/attached_validator_matcher.rb, line 45 def invalid_when_not_attached @subject.public_send(@attribute_name).detach # Unset the direct relation since `detach` on an unpersisted record does not set `attached?` to false. @subject.public_send("#{@attribute_name}=", nil) @subject.validate @subject.errors.details[@attribute_name].include?(error: :blank) end
responds_to_methods()
click to toggle source
# File lib/active_storage_validations/matchers/attached_validator_matcher.rb, line 33 def responds_to_methods @subject.respond_to?(@attribute_name) && @subject.public_send(@attribute_name).respond_to?(:attach) && @subject.public_send(@attribute_name).respond_to?(:detach) end
valid_when_attached()
click to toggle source
# File lib/active_storage_validations/matchers/attached_validator_matcher.rb, line 39 def valid_when_attached @subject.public_send(@attribute_name).attach(attachable) unless @subject.public_send(@attribute_name).attached? @subject.validate @subject.errors.details[@attribute_name].exclude?(error: :blank) end