在对象初始化之前调用回形针动态 Proc 样式

发布于 2024-10-28 08:37:49 字数 2355 浏览 8 评论 0原文

我有以下回形针设置。发生的情况是我正在使用一个过程来设置各种样式的尺寸。但是,该过程会在 new 和 super 调用期间被调用。我浏览了调试器,似乎它首先处理 :photo 参数,因此它初始化附件并调用样式过程,此时实际对象(照片)尚未由传入的参数初始化(特别是 photo.gallery_id所以它没有正确设置样式,我什至尝试了重新处理,但没有帮助我花了几天时间,但仍然没有任何帮助!

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦太阳 2024-11-04 08:37:49

据我所知,参数的顺序很重要。我:

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

这并没有正确设置样式。他们是零。我将其更改为:

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

然后代码:

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

完美运行。希望这有帮助。

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.

萝莉病 2024-11-04 08:37:49

感谢您的回答!

我已经为此奋斗了几个星期。我使用 Paperclip 和 FFMPEG 来制作上传视频的缩略图。我可以选择设置用作缩略图的帧。

我还使用嵌套表单(很棒的嵌套表单)来上传资源。 所以我所做的是将帧时间参数放在文件浏览按钮之前。这为我解决了问题,因为我没有使用构建器。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文