Skip to content

Commit

Permalink
Refactor style specific headers code
Browse files Browse the repository at this point in the history
Allow either a hash or a Style object to set the headers.
  • Loading branch information
leshill authored and Jon Yurek committed Feb 15, 2013
1 parent 74b829f commit dcd2774
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
7 changes: 4 additions & 3 deletions lib/paperclip/storage/s3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,10 @@ def flush_writes #:nodoc:
end

style_specific_options = styles[style]
if style_specific_options.is_a?(Hash)
merge_s3_headers( style_specific_options[:s3_headers], @s3_headers, @s3_metadata) if style_specific_options.has_key?(:s3_headers)
@s3_metadata.merge!(style_specific_options[:s3_metadata]) if style_specific_options.has_key?(:s3_metadata)

if style_specific_options
merge_s3_headers( style_specific_options[:s3_headers], @s3_headers, @s3_metadata) if style_specific_options[:s3_headers]
@s3_metadata.merge!(style_specific_options[:s3_metadata]) if style_specific_options[:s3_metadata]
end

write_options[:metadata] = @s3_metadata unless @s3_metadata.empty?
Expand Down
8 changes: 5 additions & 3 deletions test/storage/s3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ class << @dummy

context "An attachment that uses S3 for storage and has a proc for styles" do
setup do
rebuild_model :styles => lambda { |attachment| attachment.instance.counter; {:thumbnail => '20x20#'} },
rebuild_model :styles => lambda { |attachment| attachment.instance.counter; {:thumbnail => { :geometry => "50x50#", :s3_headers => {'Cache-Control' => 'max-age=31557600'}} }},
:storage => :s3,
:bucket => "bucket",
:path => ":attachment/:style/:basename.:extension",
Expand All @@ -317,8 +317,10 @@ def counter
@dummy.avatar = @file

object = stub
@dummy.avatar.stubs(:s3_object).returns(object)
object.expects(:write).with(anything, anything).twice
@dummy.avatar.stubs(:s3_object).with(:original).returns(object)
@dummy.avatar.stubs(:s3_object).with(:thumbnail).returns(object)
object.expects(:write).with(anything, :content_type => 'image/png', :acl => :public_read)
object.expects(:write).with(anything, :content_type => 'image/png', :acl => :public_read, :cache_control => 'max-age=31557600')
@dummy.save
end

Expand Down

0 comments on commit dcd2774

Please sign in to comment.