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

Commit

Permalink
Adding Default Format for styles
Browse files Browse the repository at this point in the history
Allows a lambda to be passed to decide what format a style should
be processed as
  • Loading branch information
ScotterC authored and Jon Yurek committed Feb 7, 2014
1 parent 9ba8263 commit 861ab21
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/paperclip/style.rb
Expand Up @@ -29,7 +29,7 @@ def initialize name, definition, attachment
@geometry, @format = [definition, nil].flatten[0..1]
@other_args = {}
end
@format = nil if @format.blank?
@format = default_format if @format.blank?
end

# retrieves from the attachment the processors defined in the has_attached_file call
Expand Down Expand Up @@ -99,5 +99,11 @@ def []=(key, value)
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
38 changes: 38 additions & 0 deletions test/style_test.rb
Expand Up @@ -210,4 +210,42 @@ class StyleTest < Test::Unit::TestCase
assert_equal "-do_extra_stuff", @attachment.styles[:large].processor_options[:source_file_options]
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

0 comments on commit 861ab21

Please sign in to comment.