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

Attachment styles lose :format, :geometry and :processors options when Rails class caching is turned on #277

Closed
pivotal-creationmix opened this issue Aug 13, 2010 · 1 comment

Comments

@pivotal-creationmix
Copy link

When Paperclip initializes Paperclip::Style objects, the Style initializer removes the :format, :geometry and :processors options from the options hash and stores them as an instance variable. The args hash that is passed in to the Style initializer is a reference to the class inheritable attribute "attachment_definitions", and these three options are removed from that hash.

When Rails class caching is on, every Style initialized after the first one will not have the :format, :geometry and :processors options passed in, because they were deleted from the class. This breaks a lot of things, particularly in our case path and url interpolations for styles with different file extensions.

We've written some Rails controller tests that demonstrate this. Unfortunately I couldn't write a test in Paperclip itself to show this problem, because there is no Rails environment in the Paperclip test suite:

class Material::Video < Material
  has_attached_file :resource, {
                      :styles => {
                        :Ogg_preview  => {:format => :ogg, :codec => "theora"},
                        :H264_preview => {:format => :mp4, :codec => "h264"}
                      },
                      :processors => [:video]
                    }

end

describe Material::Video do
  describe "attached file" do
    before do
      file = fixture_file_upload("files/test.mov", 'video/quicktime')
      @video = MaterialFactory.create_material({:resource => file}, creative_works(:film1).id)
      @video_stem = "/system/resources/#{@video.id}"
    end

    it "should have an original version" do
      @video.resource.url.should match "#{@video_stem}/original/test.mov?"
    end

    it "should create H264 preview (for Safari Users)" do
      @video.resource.url(:H264_preview).should match "#{@video_stem}/H264_preview/test.mp4"
    end

    it "should create Ogg preview (for Firefox Users)" do
      @video.resource.url(:Ogg_preview).should match "#{@video_stem}/Ogg_preview/test.ogg"
    end
  end
end

The fix is quite simple, just dup the style args when initializing a new Style object (in attachment.rb line 58) so that the options are not deleted from the cached class variable.

We've forked the repository and made the fix here:
git://github.com/pivotal-creationmix/paperclip.git

@jyurek
Copy link

jyurek commented Aug 17, 2010

I've pulled this into master. Thanks!

This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants