Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Interface for providing :source_file_options

  • Loading branch information...
commit eab19f82aa1f0e1ba175736a7462c65f53191b99 1 parent cde2256
@murbanski murbanski authored
View
13 lib/paperclip/attachment.rb
@@ -14,6 +14,7 @@ def self.default_options
:only_process => [],
:processors => [:thumbnail],
:convert_options => {},
+ :source_file_options => {},
:default_url => "/:attachment/:style/missing.png",
:default_style => :original,
:storage => :filesystem,
@@ -26,7 +27,7 @@ def self.default_options
}
end
- attr_reader :name, :instance, :default_style, :convert_options, :queued_for_write, :whiny, :options
+ attr_reader :name, :instance, :default_style, :convert_options, :queued_for_write, :whiny, :options, :source_file_options
attr_accessor :post_processing
# Creates an Attachment object. +name+ is the name of the attachment,
@@ -55,6 +56,7 @@ def initialize name, instance, options = {}
@hash_data = options[:hash_data]
@hash_secret = options[:hash_secret]
@convert_options = options[:convert_options]
+ @source_file_options = options[:source_file_options]
@processors = options[:processors]
@preserve_files = options[:preserve_files]
@options = options
@@ -341,6 +343,15 @@ def extra_options_for(style) #:nodoc:
[ style_options, all_options ].compact.join(" ")
end
+ def extra_source_file_options_for(style) #:nodoc:
+ all_options = source_file_options[:all]
+ all_options = all_options.call(instance) if all_options.respond_to?(:call)
+ style_options = source_file_options[style]
+ style_options = style_options.call(instance) if style_options.respond_to?(:call)
+
+ [ style_options, all_options ].compact.join(" ")
+ end
+
def post_process(*style_args) #:nodoc:
return if @queued_for_write[:original].nil?
instance.run_paperclip_callbacks(:post_process) do
View
10 lib/paperclip/style.rb
@@ -49,6 +49,10 @@ def convert_options
attachment.send(:extra_options_for, name)
end
+ def source_file_options
+ attachment.send(:extra_source_file_options_for, name)
+ end
+
# returns the geometry string for this style
# if a proc has been supplied, we call it here
def geometry
@@ -63,7 +67,7 @@ def processor_options
@other_args.each do |k,v|
args[k] = v.respond_to?(:call) ? v.call(attachment) : v
end
- [:processors, :geometry, :format, :whiny, :convert_options].each do |k|
+ [:processors, :geometry, :format, :whiny, :convert_options, :source_file_options].each do |k|
(arg = send(k)) && args[k] = arg
end
args
@@ -72,7 +76,7 @@ def processor_options
# Supports getting and setting style properties with hash notation to ensure backwards-compatibility
# eg. @attachment.styles[:large][:geometry]@ will still work
def [](key)
- if [:name, :convert_options, :whiny, :processors, :geometry, :format, :animated].include?(key)
+ if [:name, :convert_options, :whiny, :processors, :geometry, :format, :animated, :source_file_options].include?(key)
send(key)
elsif defined? @other_args[key]
@other_args[key]
@@ -80,7 +84,7 @@ def [](key)
end
def []=(key, value)
- if [:name, :convert_options, :whiny, :processors, :geometry, :format, :animated].include?(key)
+ if [:name, :convert_options, :whiny, :processors, :geometry, :format, :animated, :source_file_options].include?(key)
send("#{key}=".intern, value)
else
@other_args[key] = value
View
30 test/attachment_test.rb
@@ -263,6 +263,29 @@ class AttachmentTest < Test::Unit::TestCase
end
end
+ context "An attachment with :source_file_options" do
+ setup do
+ rebuild_model :styles => {
+ :thumb => "100x100",
+ :large => "400x400"
+ },
+ :source_file_options => {
+ :all => "-density 400",
+ :thumb => "-depth 8"
+ }
+ @dummy = Dummy.new
+ @dummy.avatar
+ end
+
+ should "report the correct options when sent #extra_source_file_options_for(:thumb)" do
+ assert_equal "-depth 8 -density 400", @dummy.avatar.send(:extra_source_file_options_for, :thumb), @dummy.avatar.source_file_options.inspect
+ end
+
+ should "report the correct options when sent #extra_source_file_options_for(:large)" do
+ assert_equal "-density 400", @dummy.avatar.send(:extra_source_file_options_for, :large)
+ end
+ end
+
context "An attachment with :only_process" do
setup do
rebuild_model :styles => {
@@ -476,7 +499,12 @@ class Paperclip::Test < Paperclip::Processor; end
end
before_should "call #make with the right parameters passed as second argument" do
- expected_params = @style_params[:once].merge({:processors => [:thumbnail, :test], :whiny => true, :convert_options => ""})
+ expected_params = @style_params[:once].merge({
+ :processors => [:thumbnail, :test],
+ :whiny => true,
+ :convert_options => "",
+ :source_file_options => ""
+ })
Paperclip::Thumbnail.expects(:make).with(anything, expected_params, anything).returns(@file)
end
View
32 test/integration_test.rb
@@ -283,6 +283,38 @@ class IntegrationTest < Test::Unit::TestCase
end
end
+ context "A model with no source_file_options setting" do
+ setup do
+ rebuild_model :styles => { :large => "300x300>",
+ :medium => "100x100",
+ :thumb => ["32x32#", :gif] },
+ :default_style => :medium,
+ :url => "/:attachment/:class/:style/:id/:basename.:extension",
+ :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
+ @dummy = Dummy.new
+ end
+
+ should "have its definition return nil when asked about source_file_options" do
+ assert ! Dummy.attachment_definitions[:avatar][:source_file_options]
+ end
+
+ context "redefined to have source_file_options setting" do
+ setup do
+ rebuild_model :styles => { :large => "300x300>",
+ :medium => "100x100",
+ :thumb => ["32x32#", :gif] },
+ :source_file_options => "-density 400",
+ :default_style => :medium,
+ :url => "/:attachment/:class/:style/:id/:basename.:extension",
+ :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
+ end
+
+ should "have its definition return source_file_options value when asked about source_file_options" do
+ assert_equal "-density 400", Dummy.attachment_definitions[:avatar][:source_file_options]
+ end
+ end
+ end
+
context "A model with a filesystem attachment" do
setup do
rebuild_model :styles => { :large => "300x300>",
View
20 test/style_test.rb
@@ -117,6 +117,26 @@ class StyleTest < Test::Unit::TestCase
end
end
+ context "An attachment with :source_file_options" do
+ setup do
+ @attachment = attachment :path => ":basename.:extension",
+ :styles => {:thumb => "100x100", :large => "400x400"},
+ :source_file_options => {:all => "-density 400", :thumb => "-depth 8"}
+ @style = @attachment.styles[:thumb]
+ @file = StringIO.new("...")
+ @file.stubs(:original_filename).returns("file.jpg")
+ end
+
+ before_should "not have called extra_source_file_options_for(:thumb/:large) on initialization" do
+ @attachment.expects(:extra_source_file_options_for).never
+ end
+
+ should "call extra_options_for(:thumb/:large) when convert options are requested" do
+ @attachment.expects(:extra_source_file_options_for).with(:thumb)
+ @attachment.styles[:thumb].source_file_options
+ end
+ end
+
context "A style rule with its own :processors" do
setup do
@attachment = attachment :path => ":basename.:extension",
View
4 test/thumbnail_test.rb
@@ -102,6 +102,10 @@ class ThumbnailTest < Test::Unit::TestCase
assert_equal nil, @thumb.convert_options
end
+ should "have source_file_options set to nil by default" do
+ assert_equal nil, @thumb.source_file_options
+ end
+
should "send the right command to convert when sent #make" do
Paperclip.expects(:run).with do |*arg|
arg[0] == 'convert' &&
Please sign in to comment.
Something went wrong with that request. Please try again.