class ActiveStorageValidations::Matchers::DimensionValidatorMatcher

Public Class Methods

new(attribute_name) click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 10
def initialize(attribute_name)
  @attribute_name = attribute_name
  @width_min = @width_max = @height_min = @height_max = nil
  @custom_message = nil
end

Public Instance Methods

description() click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 16
def description
  "validate image dimensions of #{@attribute_name}"
end
failure_message() click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 72
      def failure_message
        <<~MESSAGE
          is expected to validate dimensions of #{@attribute_name}
            width between #{@width_min} and #{@width_max}
            height between #{@height_min} and #{@height_max}
        MESSAGE
      end
height(height) click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 60
def height(height)
  @height_min = @height_max = height
  self
end
height_between(range) click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 55
def height_between(range)
  @height_min, @height_max = range.first, range.last
  self
end
height_max(height) click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 45
def height_max(height)
  @height_max = height
  self
end
height_min(height) click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 40
def height_min(height)
  @height_min = height
  self
end
matches?(subject) click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 65
def matches?(subject)
  @subject = subject.is_a?(Class) ? subject.new : subject
  responds_to_methods &&
    width_smaller_than_min? && width_larger_than_min? && width_smaller_than_max? && width_larger_than_max? && width_equals? &&
    height_smaller_than_min? && height_larger_than_min? && height_smaller_than_max? && height_larger_than_max? && height_equals?
end
width(width) click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 35
def width(width)
  @width_min = @width_max = width
  self
end
width_between(range) click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 50
def width_between(range)
  @width_min, @width_max = range.first, range.last
  self
end
width_max(width) click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 25
def width_max(width)
  @width_max = width
  self
end
width_min(width) click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 20
def width_min(width)
  @width_min = width
  self
end
with_message(message) click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 30
def with_message(message)
  @custom_message = message
  self
end

Protected Instance Methods

attachment_for(width, height) click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 147
def attachment_for(width, height)
  { io: Tempfile.new('Hello world!'), filename: 'test.png', content_type: 'image/png' }
end
height_equals?() click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 132
def height_equals?
  @height_min.nil? || @height_min != @height_max || passes_validation_with_dimensions(valid_width, @height_min, 'height')
end
height_larger_than_max?() click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 128
def height_larger_than_max?
  @height_max.nil? || !passes_validation_with_dimensions(valid_width, @height_max + 1, 'height')
end
height_larger_than_min?() click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 120
def height_larger_than_min?
  @height_min.nil? || @height_min == @height_max || passes_validation_with_dimensions(valid_width, @height_min + 1, 'height')
end
height_smaller_than_max?() click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 124
def height_smaller_than_max?
  @height_max.nil? || @height_min == @height_max || passes_validation_with_dimensions(valid_width, @height_max - 1, 'height')
end
height_smaller_than_min?() click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 116
def height_smaller_than_min?
  @height_min.nil? || !passes_validation_with_dimensions(valid_width, @height_min - 1, 'height')
end
passes_validation_with_dimensions(width, height, check) click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 136
def passes_validation_with_dimensions(width, height, check)
  @subject.public_send(@attribute_name).attach attachment_for(width, height)

  attachment = @subject.public_send(@attribute_name)
  Matchers.mock_metadata(attachment, width, height) do
    @subject.validate
    exclude_error_message = @custom_message || "dimension_#{check}"
    @subject.errors.details[@attribute_name].all? { |error| error[:error].to_s.exclude?(exclude_error_message) }
  end
end
responds_to_methods() click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 82
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_height() click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 92
def valid_height
  ((@height_min || 0) + (@height_max || 2000)) / 2
end
valid_width() click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 88
def valid_width
  ((@width_min || 0) + (@width_max || 2000)) / 2
end
width_equals?() click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 112
def width_equals?
  @width_min.nil? || @width_min != @width_max || passes_validation_with_dimensions(@width_min, valid_height, 'width')
end
width_larger_than_max?() click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 108
def width_larger_than_max?
  @width_max.nil? || !passes_validation_with_dimensions(@width_max + 1, valid_height, 'width')
end
width_larger_than_min?() click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 100
def width_larger_than_min?
  @width_min.nil? || @width_min == @width_max || passes_validation_with_dimensions(@width_min + 1, valid_height, 'width')
end
width_smaller_than_max?() click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 104
def width_smaller_than_max?
  @width_max.nil? || @width_min == @width_max || passes_validation_with_dimensions(@width_max - 1, valid_height, 'width')
end
width_smaller_than_min?() click to toggle source
# File lib/active_storage_validations/matchers/dimension_validator_matcher.rb, line 96
def width_smaller_than_min?
  @width_min.nil? || !passes_validation_with_dimensions(@width_min - 1, valid_height, 'width')
end