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

Commit

Permalink
Process :original style before all other styles
Browse files Browse the repository at this point in the history
  • Loading branch information
jimryan authored and Jon Yurek committed Jan 9, 2012
1 parent 9fb9255 commit d3db7a1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
25 changes: 14 additions & 11 deletions lib/paperclip/attachment.rb
Expand Up @@ -427,18 +427,21 @@ def post_process(*style_args) #:nodoc:
end

def post_process_styles(*style_args) #:nodoc:
styles.each do |name, style|
begin
if style_args.empty? || style_args.include?(name)
raise RuntimeError.new("Style #{name} has no processors defined.") if style.processors.blank?
@queued_for_write[name] = style.processors.inject(@queued_for_write[:original]) do |file, processor|
Paperclip.processor(processor).make(file, style.processor_options, self)
end
end
rescue PaperclipError => e
log("An error was received while processing: #{e.inspect}")
(@errors[:processing] ||= []) << e.message if @options[:whiny]
post_process_style(:original, styles[:original]) if styles.include?(:original) && (style_args.empty? || style_args.include?(:original))
styles.reject{ |name, style| name == :original }.each do |name, style|
post_process_style(name, style) if style_args.empty? || style_args.include?(name)
end
end

def post_process_style(name, style) #:nodoc:
begin
raise RuntimeError.new("Style #{name} has no processors defined.") if style.processors.blank?
@queued_for_write[name] = style.processors.inject(@queued_for_write[:original]) do |file, processor|
Paperclip.processor(processor).make(file, style.processor_options, self)
end
rescue PaperclipError => e
log("An error was received while processing: #{e.inspect}")
(@errors[:processing] ||= []) << e.message if @options[:whiny]
end
end

Expand Down
14 changes: 14 additions & 0 deletions test/attachment_test.rb
Expand Up @@ -5,6 +5,20 @@
class Dummy; end

class AttachmentTest < Test::Unit::TestCase

should "process :original style first" do
file = File.new(File.join(File.dirname(__FILE__), "fixtures", "50x50.png"), 'rb')
rebuild_class :styles => { :small => '100x>', :original => '42x42#' }
dummy = Dummy.new
dummy.avatar = file
dummy.save

# :small avatar should be 42px wide (processed original), not 50px (preprocessed original)
assert_equal `identify -format "%w" "#{dummy.avatar.path(:small)}"`.strip, "42"

file.close
end

should "handle a boolean second argument to #url" do
mock_url_generator_builder = MockUrlGeneratorBuilder.new
attachment = Paperclip::Attachment.new(:name, :instance, :url_generator => mock_url_generator_builder)
Expand Down

0 comments on commit d3db7a1

Please sign in to comment.