Commit f79a822f by Jon Yurek

Modified post_process to handle Thumbnail's new interface. Munged styles into preferred format.

parent ee70992b
......@@ -73,6 +73,11 @@ module Paperclip
def included base #:nodoc:
base.extend ClassMethods
end
def processor name
name = name.to_s.camelize
Paperclip.const_get(name) if Paperclip.const_defined?(name)
end
end
class PaperclipError < StandardError #:nodoc:
......
......@@ -33,8 +33,9 @@ module Paperclip
@validations = options[:validations]
@default_style = options[:default_style]
@storage = options[:storage]
@whiny_thumbnails = options[:whiny_thumbnails]
@whiny = options[:whiny_thumbnails]
@convert_options = options[:convert_options] || {}
@processors = [:thumbnail]
@options = options
@queued_for_delete = []
@queued_for_write = {}
......@@ -181,7 +182,7 @@ module Paperclip
attachment.original_filename.gsub(/#{File.extname(attachment.original_filename)}$/, "")
end,
:extension => lambda do |attachment,style|
((style = attachment.styles[style]) && style.last) ||
((style = attachment.styles[style]) && style[:format]) ||
File.extname(attachment.original_filename).gsub(/^\.+/, "")
end,
:id => lambda{|attachment,style| attachment.instance.id },
......@@ -255,9 +256,17 @@ module Paperclip
def normalize_style_definition
@styles.each do |name, args|
unless args.is_a? Hash
dimensions, format = [args, nil].flatten[0..1]
format = nil if format == ""
@styles[name] = [dimensions, format]
format = nil if format.blank?
@styles[name] = {
:processors => @processors,
:geometry => dimensions,
:format => format,
:whiny => @whiny,
:convert_options => extra_options_for(name)
}
end
end
end
......@@ -277,15 +286,12 @@ module Paperclip
logger.info("[paperclip] Post-processing #{name}")
@styles.each do |name, args|
begin
dimensions, format = args
dimensions = dimensions.call(instance) if dimensions.respond_to? :call
@queued_for_write[name] = Thumbnail.make(@queued_for_write[:original],
dimensions,
format,
extra_options_for(name),
@whiny_thumbnails)
@queued_for_write[name] = @queued_for_write[:original]
args[:processors].each do |processor|
@queued_for_write[name] = Paperclip.processor(processor).make(@queued_for_write[name], args)
end
rescue PaperclipError => e
(@errors[:processing] ||= []) << e.message if @whiny_thumbnails
(@errors[:processing] ||= []) << e.message if @whiny
end
end
callback(:"after_#{name}_post_process")
......
......@@ -117,6 +117,7 @@ class AttachmentTest < Test::Unit::TestCase
:thumb => "-thumbnailize"
}
@dummy = Dummy.new
@dummy.avatar
end
should "report the correct options when sent #extra_options_for(:thumb)" do
......@@ -127,23 +128,9 @@ class AttachmentTest < Test::Unit::TestCase
assert_equal "-do_stuff", @dummy.avatar.send(:extra_options_for, :large)
end
context "when given a file" do
setup do
@file = File.new(File.join(File.dirname(__FILE__),
"fixtures",
"5k.png"), 'rb')
Paperclip::Thumbnail.stubs(:make)
[:thumb, :large].each do |style|
@dummy.avatar.stubs(:extra_options_for).with(style)
end
end
[:thumb, :large].each do |style|
should "call extra_options_for(#{style})" do
@dummy.avatar.expects(:extra_options_for).with(style)
@dummy.avatar = @file
end
end
before_should "call extra_options_for(:thumb/:large)" do
Paperclip::Attachment.any_instance.expects(:extra_options_for).with(:thumb)
Paperclip::Attachment.any_instance.expects(:extra_options_for).with(:large)
end
end
......
......@@ -10,6 +10,14 @@ class PaperclipTest < Test::Unit::TestCase
end
end
should "return nil when sent #processor and the name of a class that doesn't exist" do
assert_nil Paperclip.processor(:boogey_man)
end
should "return a class when sent #processor and the name of a class under Paperclip" do
assert_equal ::Paperclip::Thumbnail, Paperclip.processor(:thumbnail)
end
context "Paperclip.bit_bucket" do
context "on systems without /dev/null" do
setup 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