class MiniPaperclip::Shoulda::Matchers::ValidateAttachmentGeometryMatcher

Constants

CallError

Public Class Methods

new(attachment_name) click to toggle source
# File lib/mini_paperclip/shoulda/matchers/validate_attachment_geometry_matcher.rb, line 23
def initialize(attachment_name)
  @attachment_name = attachment_name.to_sym
  @width = {}
  @height = {}
  @format = nil
end

Public Instance Methods

failure_message() click to toggle source
# File lib/mini_paperclip/shoulda/matchers/validate_attachment_geometry_matcher.rb, line 61
def failure_message
  [
    "Attachment :#{@attachment_name} got details",
    @subject.errors.details[@attachment_name]
  ].join("\n")
end
failure_message_when_negated() click to toggle source
# File lib/mini_paperclip/shoulda/matchers/validate_attachment_geometry_matcher.rb, line 68
def failure_message_when_negated
  [
    "Attachment :#{@attachment_name} got details",
    @subject.errors.details[@attachment_name]
  ].join("\n")
end
format(format) click to toggle source
# File lib/mini_paperclip/shoulda/matchers/validate_attachment_geometry_matcher.rb, line 30
def format(format)
  @format = format
  self
end
height(less_than_or_equal_to:) click to toggle source
# File lib/mini_paperclip/shoulda/matchers/validate_attachment_geometry_matcher.rb, line 40
def height(less_than_or_equal_to:)
  @height[:less_than_or_equal_to] = less_than_or_equal_to
  self
end
matches?(subject) click to toggle source
# File lib/mini_paperclip/shoulda/matchers/validate_attachment_geometry_matcher.rb, line 45
def matches?(subject)
  @subject = subject.class == Class ? subject.new : subject

  unless @format && !@width.empty? && !@height.empty?
    raise CallError, [
      "should call like this",
      "  it { should validate_attachment_geometry(:image)",
      "                .format(:png)",
      "                .width(less_than_or_equal_to: 3000)",
      "                .height(less_than_or_equal_to: 3000) }"
    ].join("\n")
  end

  when_valid && when_invalid
end
width(less_than_or_equal_to:) click to toggle source
# File lib/mini_paperclip/shoulda/matchers/validate_attachment_geometry_matcher.rb, line 35
def width(less_than_or_equal_to:)
  @width[:less_than_or_equal_to] = less_than_or_equal_to
  self
end

Private Instance Methods

create_dummy_image(width:, height:) { |f| ... } click to toggle source
# File lib/mini_paperclip/shoulda/matchers/validate_attachment_geometry_matcher.rb, line 97
def create_dummy_image(width:, height:)
  Tempfile.create(['MiniPaperclip::Shoulda::Matchers::ValidateAttachmentGeometryMatcher', ".#{@format}"]) do |f|
    f.binmode
    MiniMagick::Tool::Convert.new do |convert|
      convert.size("#{width}x#{height}")
      convert.xc("none")
      convert.strip
      convert << f.path
    end
    yield f
  end
end
when_invalid() click to toggle source
# File lib/mini_paperclip/shoulda/matchers/validate_attachment_geometry_matcher.rb, line 85
def when_invalid
  create_dummy_image(width: @width[:less_than_or_equal_to] + 1, height: @height[:less_than_or_equal_to] + 1) do |f|
    @subject.public_send("#{@attachment_name}=", f)
  end
  @subject.valid?
  detail = @subject.errors.details[:image]&.find { |d| d[:error] == :geometry }
  return false unless detail
  return false unless detail[:expected_width_less_than_or_equal_to] == @width[:less_than_or_equal_to]
  return false unless detail[:expected_height_less_than_or_equal_to] == @height[:less_than_or_equal_to]
  true
end
when_valid() click to toggle source
# File lib/mini_paperclip/shoulda/matchers/validate_attachment_geometry_matcher.rb, line 77
def when_valid
  create_dummy_image(width: @width[:less_than_or_equal_to], height: @height[:less_than_or_equal_to]) do |f|
    @subject.public_send("#{@attachment_name}=", f)
  end
  @subject.valid?
  @subject.errors.details[:image]&.find { |d| d[:error] == :geometry }.nil?
end