Paperclip :style depending on model (has_many polymorphic images)

后端 未结 10 927
醉梦人生
醉梦人生 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条回答
  •  梦如初夏
    2021-02-03 12:17

    I've had a similar question when dealing with dynamic behavioral changes on my models. Playing around with irb, I found out that this works:

    module Foo
       attr_accessor :bar
    end
    class Bar
       extends Foo
    end
    bar.bar = 'test' # 'test'
    bar.bar # 'test'
    # also works for instances of Bar!
    

    So i would create an attribute called image_style that could be changed to whatever module you want to add, by using this code on Image initialization:

      def after_initialize
        if self.image_style?
           extend Object.const_get(image_style)
        else
           extend DefaultImageStyle
        end
      end
    

    I just wonder if this works with paperclip since the after_initialize method may be called after paperclip does it's magic.. Worth a try though!

提交回复
热议问题