Paperclip :style depending on model (has_many polymorphic images)

后端 未结 10 924
醉梦人生
醉梦人生 2021-02-03 11:33

I have set up my models to use a polymorphic Image model. This is working fine, however I am wondering if it is possible to change the :styles setting for each model. Found some

10条回答
  •  -上瘾入骨i
    2021-02-03 12:12

    the :styles property takes a Proc as argument, so you can do all kinds of fancy stuff :)

    class Image < AR::Base
      has_attached_file :file, :styles => Proc.new { |a| a.instance.file_styles }
    
      def file_styles; { :thumb => "150x150>", :normal => "492x600>" } end
    end
    
    class Didum < Image
      def file_styles; { :thumb => "50x50>", :normal => "492x600>" } end
    end
    

    Note - the above code should work, but honestly I have no setup to verify it, but looks like the Paperclip::Attachment#styles does call if it responds to it, see http://rdoc.info/github/thoughtbot/paperclip/master/Paperclip/Attachment:styles

    UPDATE the object passed into the Proc is not the instance, but the Paperclip::Attachment, but the instance is accessible through instance on the attachment

    PS: And I've seen this in some other places, but can't remember where...

提交回复
热议问题