Commit 861ab211 by Scott Carleton Committed by Jon Yurek

Adding Default Format for styles

Allows a lambda to be passed to decide what format a style should
be processed as
parent 9ba82638
...@@ -29,7 +29,7 @@ module Paperclip ...@@ -29,7 +29,7 @@ module Paperclip
@geometry, @format = [definition, nil].flatten[0..1] @geometry, @format = [definition, nil].flatten[0..1]
@other_args = {} @other_args = {}
end end
@format = nil if @format.blank? @format = default_format if @format.blank?
end end
# retrieves from the attachment the processors defined in the has_attached_file call # retrieves from the attachment the processors defined in the has_attached_file call
...@@ -99,5 +99,11 @@ module Paperclip ...@@ -99,5 +99,11 @@ module Paperclip
end end
end end
# defaults to default format (nil by default)
def default_format
base = attachment.options[:default_format]
base.respond_to?(:call) ? base.call(attachment, name) : base
end
end end
end end
...@@ -210,4 +210,42 @@ class StyleTest < Test::Unit::TestCase ...@@ -210,4 +210,42 @@ class StyleTest < Test::Unit::TestCase
assert_equal "-do_extra_stuff", @attachment.styles[:large].processor_options[:source_file_options] assert_equal "-do_extra_stuff", @attachment.styles[:large].processor_options[:source_file_options]
end end
end end
context "A style rule supplied with default format" do
setup do
@attachment = attachment :default_format => :png,
:styles => {
:asstring => "300x300#",
:aslist => ["300x300#", :jpg],
:ashash => {
:geometry => "300x300#",
:convert_options => "-do_stuff"
}
}
end
should "have the right number of styles" do
assert_kind_of Hash, @attachment.styles
assert_equal 3, @attachment.styles.size
end
should "have styles as Style objects" do
[:aslist, :ashash, :aslist].each do |s|
assert_kind_of Paperclip::Style, @attachment.styles[s]
end
end
should "have the right geometries" do
[:aslist, :ashash, :aslist].each do |s|
assert_equal @attachment.styles[s].geometry, "300x300#"
end
end
should "have the right formats" do
assert_equal @attachment.styles[:aslist].format, :jpg
assert_equal @attachment.styles[:ashash].format, :png
assert_equal @attachment.styles[:asstring].format, :png
end
end
end end
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