Bashir
Bashir

Reputation: 21

Paperclip dynamic Proc styles called before object initialized

I have the following paperclip setup. What happens is that I'm using a proc to set the sizes for various styles. However, the proc gets called on new and during the super call. I walked through the debugger and it seems like it processes the :photo parameter first so it initializes the attachment and calls the styles proc at which point the actual object (Photo) has not been initialized by the passed in params(particularly the photo.gallery_id so it doesn't set the styles correctly. I even tried reprocessing and it didn't help. I've spent a couple of days on this and still no luck. any help is appreciated!

class Photo < ActiveRecord::Base
  has_and_belongs_to_many :staffs
 has_attached_file :photo, 
                    :storage => :s3,
                    :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
                    :path => "/assets/:id/:class/:style/:image_name.:extension",
                    :url => "/assets/:id/:class/:style/:image_name.:extension",
                    :styles => Proc.new { |clip| clip.instance.attachment_styles}

  def attachment_styles
    if self.gallery.nil?
        { :original => {
                        :processors => [:watermark],
                        :geometry =>"600x800!",
                        :watermark_path => ':rails_root/public/images/watermark.png',
                        :position => 'SouthEast'},
          :thumbnail => {
                        :processors => [:watermark],
                        :geometry => "200x300!",
                        :watermark_path => ':rails_root/public/images/watermark.png',
                        :position => 'SouthEast'}
        }
    elsif self.photo.styles.empty?
        gallery_type = GalleryType.find_by_id(self.gallery_id)
        { :original => {
                        :processors => [:watermark],
                        :geometry =>"#{gallery_type.width_max}x#{gallery_type.height_max}!",
                        :watermark_path => ':rails_root/public/images/watermark.png',
                        :position => 'SouthEast'},
          :thumbnail => {
                        :processors => [:watermark],
                        :geometry => "#{gallery_type.width_min}x#{gallery_type.height_min}!",
                        :watermark_path => ':rails_root/public/images/watermark.png',
                        :position => 'SouthEast'}
        }
    else
        self.photo.styles
    end
  end


  def reprocess_att
    self.photo.reprocess!
  end

  def initialize(galleryid, params = {}) 
    begin
        param.merge!({"gallery_id" => galleryid.to_s})
        super(params)
    rescue => e
      puts e.message()
    end
  end

Upvotes: 0

Views: 1426

Answers (2)

Phill Kenoyer
Phill Kenoyer

Reputation: 516

Thanks for the answer!

I've been fighting with this for a few weeks now. I'm using Paperclip with FFMPEG to make thumbnails of uploaded videos. I have an option to set what frame to use as the thumbnail.

I'm also using a nested form (awesome nested forms) for my asset upload. So what I did was put the frame time param before the file browse button. This solved the problem for me since I'm not using builder.

Upvotes: 0

carlwoodward
carlwoodward

Reputation: 46

From what I can see, the order of the parameters is important. I had:

attachments.build(:upload => File.new(File.dirname(__FILE__) + '/../fixtures/test-image.jpg'),                                                                                                                                                                                               
:styles => {:small => ['100x100#', :jpg], :medium => ['250x250', :jpg]})

And this wasn't setting up the styles correctly. They they were nil. I changed it to:

attachments.build(:styles => {:small => ['100x100#', :jpg], :medium => ['250x250', :jpg]},                                                                                                                                                                                               
:upload => File.new(File.dirname(__FILE__) + '/../fixtures/test-image.jpg'))

And then the code:

:styles => lambda { |a| a.instance.styles || {} }

worked perfectly. Hope this helps.

Upvotes: 1

Related Questions