Skip to content

Commit

Permalink
Modified post_process to handle Thumbnail's new interface. Munged sty…
Browse files Browse the repository at this point in the history
…les into preferred format.
  • Loading branch information
Jon Yurek committed Dec 23, 2008
1 parent ee70992 commit f79a822
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
5 changes: 5 additions & 0 deletions lib/paperclip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ def bit_bucket
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:
Expand Down
32 changes: 19 additions & 13 deletions lib/paperclip/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ def initialize name, instance, options = {}
@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 = {}
Expand Down Expand Up @@ -181,7 +182,7 @@ def self.interpolations
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 },
Expand Down Expand Up @@ -255,9 +256,17 @@ def validate #:nodoc:

def normalize_style_definition
@styles.each do |name, args|
dimensions, format = [args, nil].flatten[0..1]
format = nil if format == ""
@styles[name] = [dimensions, format]
unless args.is_a? Hash
dimensions, format = [args, nil].flatten[0..1]
format = nil if format.blank?
@styles[name] = {
:processors => @processors,
:geometry => dimensions,
:format => format,
:whiny => @whiny,
:convert_options => extra_options_for(name)
}
end
end
end

Expand All @@ -277,15 +286,12 @@ def post_process #:nodoc:
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")
Expand Down
21 changes: 4 additions & 17 deletions test/attachment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
8 changes: 8 additions & 0 deletions test/paperclip_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f79a822

Please sign in to comment.