Commit ee70992b by Jon Yurek

Thumbnail takes a hash of options instead of a list of args now

parent 9dc9f71f
...@@ -2,32 +2,32 @@ module Paperclip ...@@ -2,32 +2,32 @@ module Paperclip
# Handles thumbnailing images that are uploaded. # Handles thumbnailing images that are uploaded.
class Thumbnail 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 # Creates a Thumbnail object set to work on the +file+ given. It
# will attempt to transform the image into one defined by +target_geometry+ # 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+ # which is a "WxH"-style string. +format+ will be inferred from the +file+
# unless specified. Thumbnail creation will raise no errors unless # 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 # 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 @file = file
@crop = target_geometry[-1,1] == '#' @crop = geometry[-1,1] == '#'
@target_geometry = Geometry.parse target_geometry @target_geometry = Geometry.parse geometry
@current_geometry = Geometry.from_file file @current_geometry = Geometry.from_file @file
@convert_options = convert_options @convert_options = options[:convert_options]
@whiny_thumbnails = whiny_thumbnails @whiny = options[:whiny].nil? ? true : options[:whiny]
@format = options[:format]
@current_format = File.extname(@file.path) @current_format = File.extname(@file.path)
@basename = File.basename(@file.path, @current_format) @basename = File.basename(@file.path, @current_format)
@format = format
end end
# Creates a thumbnail, as specified in +initialize+, +make+s it, and returns the # Creates a thumbnail, as specified in +initialize+, +make+s it, and returns the
# resulting Tempfile. # resulting Tempfile.
def self.make file, dimensions, format = nil, convert_options = nil, whiny_thumbnails = true def self.make file, options = {}
new(file, dimensions, format, convert_options, whiny_thumbnails).make new(file, options).make
end end
# Returns true if the +target_geometry+ is meant to crop. # Returns true if the +target_geometry+ is meant to crop.
...@@ -56,7 +56,7 @@ module Paperclip ...@@ -56,7 +56,7 @@ module Paperclip
begin begin
success = Paperclip.run("convert", command.gsub(/\s+/, " ")) success = Paperclip.run("convert", command.gsub(/\s+/, " "))
rescue PaperclipCommandLineError 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 end
dst dst
......
...@@ -41,7 +41,7 @@ class ThumbnailTest < Test::Unit::TestCase ...@@ -41,7 +41,7 @@ class ThumbnailTest < Test::Unit::TestCase
].each do |args| ].each do |args|
context "being thumbnailed with a geometry of #{args[0]}" do context "being thumbnailed with a geometry of #{args[0]}" do
setup do setup do
@thumb = Paperclip::Thumbnail.new(@file, args[0]) @thumb = Paperclip::Thumbnail.new(@file, :geometry => args[0])
end end
should "start with dimensions of 434x66" do should "start with dimensions of 434x66" do
...@@ -68,7 +68,7 @@ class ThumbnailTest < Test::Unit::TestCase ...@@ -68,7 +68,7 @@ class ThumbnailTest < Test::Unit::TestCase
context "being thumbnailed at 100x50 with cropping" do context "being thumbnailed at 100x50 with cropping" do
setup do setup do
@thumb = Paperclip::Thumbnail.new(@file, "100x50#") @thumb = Paperclip::Thumbnail.new(@file, :geometry => "100x50#")
end end
should "report its correct current and target geometries" do should "report its correct current and target geometries" do
...@@ -80,8 +80,8 @@ class ThumbnailTest < Test::Unit::TestCase ...@@ -80,8 +80,8 @@ class ThumbnailTest < Test::Unit::TestCase
assert_nil @thumb.format assert_nil @thumb.format
end end
should "have whiny_thumbnails turned on by default" do should "have whiny turned on by default" do
assert @thumb.whiny_thumbnails assert @thumb.whiny
end end
should "have convert_options set to nil by default" do should "have convert_options set to nil by default" do
...@@ -103,7 +103,9 @@ class ThumbnailTest < Test::Unit::TestCase ...@@ -103,7 +103,9 @@ class ThumbnailTest < Test::Unit::TestCase
context "being thumbnailed with convert options set" do context "being thumbnailed with convert options set" do
setup 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 end
should "have convert_options value set" do should "have convert_options value set" do
...@@ -124,7 +126,9 @@ class ThumbnailTest < Test::Unit::TestCase ...@@ -124,7 +126,9 @@ class ThumbnailTest < Test::Unit::TestCase
context "redefined to have bad convert_options setting" do context "redefined to have bad convert_options setting" do
setup 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 end
should "error when trying to create the thumbnail" do should "error when trying to create the thumbnail" do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment