处理版本时如何跳过载波存储回调 [英] How do I skip a carrierwave store callback when processing versions

查看:95
本文介绍了处理版本时如何跳过载波存储回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在载波的video uploader类中定义了一个回调
after :store, :my_method
我有文件的三个版本. originalstandardlow

I have a callback defined in my video uploader class of carrierwave
after :store, :my_method
and I have three versions of the files. original,standard,low

my_method在处理每个版本时执行,即执行三次,我只需要回调执行一次即可.

my_method executes when each version is processed ie, three times, I just need the callback to execute once.

推荐答案

我知道这是一个很晚的答复,但是最近我遇到了同样的问题,所以我决定发布解决该问题"的方法,因为看来没有在carrierwave的github页面上记录(或者无论如何我都找不到).

I know this is a very late response but I was just having the same problem lately so I decided to post how I solved this "problem" since it seems it isn't documented on carrierwave's github page (or I wasn't able to find it anyway).

好吧,关于after :store, :my_method回调,如果将其放在上载器类的主体"中,那么它将在每次存储文件时执行,因此在您的情况下,我认为它甚至不会执行仅适用于您的3个版本,也适用于您的原始文件.

Ok, regarding the after :store, :my_method callback if you place it in the "main body" of your uploader class, then it's going to be executed every single time a file is stored, so in your case I think it even executes not only for your 3 versions but for your original file as well.

假设以下代码定义了您的载波上传器:

Let's say the following code defines your carrierwave uploader:

class PhotoUploader < CarrierWave::Uploader::Base
  after :store, :my_method

  version :original do
    process :resize_to_limit => [1280,960]
  end

  version :standard, from_version: :original do
    process :resize_to_limit => [640,480]
  end

  version :low, from_version: :standard do
    process :resize_to_limit => [320,240]
  end

  protected
    def my_method
      puts self.version_name
    end
end

这样,将对每个存储的文件执行after :store,但是,如果仅希望执行该文件,那么对于:low版本,您要做的就是移动该行.在您的版本定义中.像这样:

That way, the after :store is going to be executed for every file stored, but if you only want it to be executed, let's say, for the :low version, all you have to do is to move that line inside your version definition. Like this:

class PhotoUploader < CarrierWave::Uploader::Base

  version :original do
    process :resize_to_limit => [1280,960]
  end

  version :standard, from_version: :original do
    process :resize_to_limit => [640,480]
  end

  version :low, from_version: :standard do
    process :resize_to_limit => [320,240]
    after :store, :my_method
  end

  protected
    def my_method
      puts self.version_name
    end
end

我已经在代码中对其进行了测试,并且可以正常工作...我知道自您发布此问题以来已经有很长时间了,并且可能您也找到了与我相同的解决方案.因此,我决定回答该问题,以供日后遇到相同问题的人参考.

I tested it on my code and it works... I know it's been a long time since you posted this question and probably you arrived at the same solution as me. So I decided to answer it for future reference for anyone getting the same problem.

这篇关于处理版本时如何跳过载波存储回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆