Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Thumbnail takes a hash of options instead of a list of args now
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Yurek committed Dec 23, 2008
1 parent 9dc9f71 commit ee70992
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
26 changes: 13 additions & 13 deletions lib/paperclip/thumbnail.rb
Expand Up @@ -2,32 +2,32 @@ module Paperclip
# Handles thumbnailing images that are uploaded.
class Thumbnail

attr_accessor :file, :current_geometry, :target_geometry, :format, :whiny_thumbnails, :convert_options
attr_accessor :file, :current_geometry, :target_geometry, :format, :whiny, :convert_options

# Creates a Thumbnail object set to work on the +file+ given. It
# will attempt to transform the image into one defined by +target_geometry+
# which is a "WxH"-style string. +format+ will be inferred from the +file+
# unless specified. Thumbnail creation will raise no errors unless
# +whiny_thumbnails+ is true (which it is, by default. If +convert_options+ is
# +whiny+ is true (which it is, by default. If +convert_options+ is
# set, the options will be appended to the convert command upon image conversion
def initialize file, target_geometry, format = nil, convert_options = nil, whiny_thumbnails = true
def initialize file, options = {}
geometry = options[:geometry]
@file = file
@crop = target_geometry[-1,1] == '#'
@target_geometry = Geometry.parse target_geometry
@current_geometry = Geometry.from_file file
@convert_options = convert_options
@whiny_thumbnails = whiny_thumbnails
@crop = geometry[-1,1] == '#'
@target_geometry = Geometry.parse geometry
@current_geometry = Geometry.from_file @file
@convert_options = options[:convert_options]
@whiny = options[:whiny].nil? ? true : options[:whiny]
@format = options[:format]

@current_format = File.extname(@file.path)
@basename = File.basename(@file.path, @current_format)

@format = format
end

# Creates a thumbnail, as specified in +initialize+, +make+s it, and returns the
# resulting Tempfile.
def self.make file, dimensions, format = nil, convert_options = nil, whiny_thumbnails = true
new(file, dimensions, format, convert_options, whiny_thumbnails).make
def self.make file, options = {}
new(file, options).make
end

# Returns true if the +target_geometry+ is meant to crop.
Expand Down Expand Up @@ -56,7 +56,7 @@ def make
begin
success = Paperclip.run("convert", command.gsub(/\s+/, " "))
rescue PaperclipCommandLineError
raise PaperclipError, "There was an error processing the thumbnail for #{@basename}" if @whiny_thumbnails
raise PaperclipError, "There was an error processing the thumbnail for #{@basename}" if @whiny
end

dst
Expand Down
16 changes: 10 additions & 6 deletions test/thumbnail_test.rb
Expand Up @@ -41,7 +41,7 @@ class ThumbnailTest < Test::Unit::TestCase
].each do |args|
context "being thumbnailed with a geometry of #{args[0]}" do
setup do
@thumb = Paperclip::Thumbnail.new(@file, args[0])
@thumb = Paperclip::Thumbnail.new(@file, :geometry => args[0])
end

should "start with dimensions of 434x66" do
Expand All @@ -68,7 +68,7 @@ class ThumbnailTest < Test::Unit::TestCase

context "being thumbnailed at 100x50 with cropping" do
setup do
@thumb = Paperclip::Thumbnail.new(@file, "100x50#")
@thumb = Paperclip::Thumbnail.new(@file, :geometry => "100x50#")
end

should "report its correct current and target geometries" do
Expand All @@ -80,8 +80,8 @@ class ThumbnailTest < Test::Unit::TestCase
assert_nil @thumb.format
end

should "have whiny_thumbnails turned on by default" do
assert @thumb.whiny_thumbnails
should "have whiny turned on by default" do
assert @thumb.whiny
end

should "have convert_options set to nil by default" do
Expand All @@ -103,7 +103,9 @@ class ThumbnailTest < Test::Unit::TestCase

context "being thumbnailed with convert options set" do
setup do
@thumb = Paperclip::Thumbnail.new(@file, "100x50#", format=nil, convert_options="-strip -depth 8", whiny_thumbnails=true)
@thumb = Paperclip::Thumbnail.new(@file,
:geometry => "100x50#",
:convert_options => "-strip -depth 8")
end

should "have convert_options value set" do
Expand All @@ -124,7 +126,9 @@ class ThumbnailTest < Test::Unit::TestCase

context "redefined to have bad convert_options setting" do
setup do
@thumb = Paperclip::Thumbnail.new(@file, "100x50#", format=nil, convert_options="-this-aint-no-option", whiny_thumbnails=true)
@thumb = Paperclip::Thumbnail.new(@file,
:geometry => "100x50#",
:convert_options => "-this-aint-no-option")
end

should "error when trying to create the thumbnail" do
Expand Down

0 comments on commit ee70992

Please sign in to comment.