class Paperclip::Shoulda::Matchers::ValidateAttachmentDimensionsMatcher

Public Class Methods

new(attachment_name) click to toggle source
# File lib/paperclip/matchers/validate_attachment_dimensions_matcher.rb, line 11
def initialize(attachment_name)
  unless defined?(ChunkyPNG)
    puts 'WARNING: Add chunky_png or oily_png to your Gemfile to use paperclip dimension matchers'
  end

  @attachment_name = attachment_name
end

Public Instance Methods

description() click to toggle source
# File lib/paperclip/matchers/validate_attachment_dimensions_matcher.rb, line 50
def description
  "validate width and height in pixels for attachment #{@attachment_name}"
end
failure_message() click to toggle source
# File lib/paperclip/matchers/validate_attachment_dimensions_matcher.rb, line 35
def failure_message
  message = "Attachment #{@attachment_name} must have"
  message += " a height of #{@height}px" if @height
  message += " and" if @height && @width
  message += " a width of #{@width}px" if @width
end
failure_message_when_negated() click to toggle source
# File lib/paperclip/matchers/validate_attachment_dimensions_matcher.rb, line 42
def failure_message_when_negated
  message = "Attachment #{@attachment_name} cannot have"
  message += " a height of #{@height}px" if @height
  message += " and" if @height && @width
  message += " a width of #{@width}px" if @width
end
Also aliased as: negative_failure_message
height(height) click to toggle source
# File lib/paperclip/matchers/validate_attachment_dimensions_matcher.rb, line 19
def height(height)
  @height = height
  self
end
matches?(subject) click to toggle source
# File lib/paperclip/matchers/validate_attachment_dimensions_matcher.rb, line 29
def matches?(subject)
  @subject = subject
  @subject = @subject.new if @subject.class == Class
  shorter_than_height? && taller_than_height? && smaller_than_width? && larger_than_width?
end
negative_failure_message()
width(width) click to toggle source
# File lib/paperclip/matchers/validate_attachment_dimensions_matcher.rb, line 24
def width(width)
  @width = width
  self
end

Protected Instance Methods

generate_png(height, width) click to toggle source
# File lib/paperclip/matchers/validate_attachment_dimensions_matcher.rb, line 83
def generate_png(height, width)
  file = Tempfile.new([Time.now.to_i.to_s, '.png'])
  file.binmode
  ChunkyPNG::Image.new(width || 1, height || 1, ChunkyPNG::Color('black')).save(file.path)
  file
end
larger_than_width?() click to toggle source
# File lib/paperclip/matchers/validate_attachment_dimensions_matcher.rb, line 79
def larger_than_width?
  @width.nil? || validation_with_width(@width + 1)
end
shorter_than_height?() click to toggle source
# File lib/paperclip/matchers/validate_attachment_dimensions_matcher.rb, line 67
def shorter_than_height?
  @height.nil? || validation_with_height(@height - 1)
end
smaller_than_width?() click to toggle source
# File lib/paperclip/matchers/validate_attachment_dimensions_matcher.rb, line 75
def smaller_than_width?
  @width.nil? || validation_with_width(@width - 1)
end
taller_than_height?() click to toggle source
# File lib/paperclip/matchers/validate_attachment_dimensions_matcher.rb, line 71
def taller_than_height?
  @height.nil? || validation_with_height(@height + 1)
end
validation_with_height(height) click to toggle source
# File lib/paperclip/matchers/validate_attachment_dimensions_matcher.rb, line 55
def validation_with_height(height)
  @subject.send(@attachment_name).assign(generate_png(height, @width))
  @subject.valid?
  @subject.errors[@attachment_name].include?("must have a height of #{@height}px was #{height}px")
end
validation_with_width(width) click to toggle source
# File lib/paperclip/matchers/validate_attachment_dimensions_matcher.rb, line 61
def validation_with_width(width)
  @subject.send(@attachment_name).assign(generate_png(@height, width))
  @subject.valid?
  @subject.errors[@attachment_name].include?("must have a width of #{@width}px was #{width}px")
end