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