如何在carrierwave版本处理中使用模型属性/ activerecord方法?

时间:2011-06-22 21:17:07

标签: ruby-on-rails ruby ruby-on-rails-3 carrierwave

这是追求this question答案的另一个问题。

在我的水印处理器中,如果我将水印图像的路径设置为静态水印图像,一切正常。

我有三种型号:水印,图库和照片。水印has_many :galleries。图库belongs_to :watermarkhas_many :photos。照片belongs_to :gallerymount_uploader :image, PhotoUploader

以下是我想在photo_uploader.rb中做的事情:

  version :large do
    process :watermark
    process :resize_to_limit => [600, 600]
  end

  def watermark
    manipulate! do |img|
      watermark = Magick::Image.read(model.gallery.watermark.image_url).first
      img = img.composite(watermark, Magick::CenterGravity, Magick::OverCompositeOp)
    end
  end

虽然在store_dir方法中使用模型方法/属性可以正常工作,但它在水印处理器中不起作用。如何将model.gallery.watermark.image_url参数传递给水印处理器?

1 个答案:

答案 0 :(得分:1)

问题在于,当图像安装在模型上时,上传器类中的watemark def内的变量不可用。那是做了什么。我正在使用mongoid来处理mongoDB。我在模型中设置了一个参数,它将保存我想要的值,例如。值the_current_model.gallery.watermark.image_url。这个参数将是user_image。

class Asset
  include Mongoid::Document
  mount_uploader :image, AssetUploader

  field :user_image

  attr_accessible :user_image

  after_save :mark_it

  private
  def mark_it
    image.recreate_versions! if user_image.present?
  end

end

解决此问题的关键是在数据保存到数据库之后重新创建映像(正如您在调用mark_it def的模型上看到的那样),并且param user_image允许用于您的上传器类。

class AssetUploader < CarrierWave::Uploader::Base

  include CarrierWave::RMagick

  storage :file
  # storage :fog

  after :cache, :unlink_original_cache

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end

  process :resize_to_fit => [800, 800]
  process :quality => 80

  version :thumb do
    process :resize_to_fit => [200, 200]
  end

  version :large do
    process :watermark
  end

  def watermark
    if model.user_name.present?
      manipulate! do |img|
        text = Magick::Draw.new 
        text.gravity = Magick::CenterGravity
        text.fill = 'white'    
        text.pointsize = 40 
        text.stroke = 'none' 
        text.annotate(img, 0, 0, 0, 0, "#{model.user_name}") 
        img
      end
    end
  end

  def unlink_original(file)
    File.delete path if version_name.blank?
  end

  def unlink_original_cache(file)
    File.delete if version_name.blank?
  end

  def extension_white_list
    %w(jpg jpeg gif png)
  end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end

end

现在关键在于:

def watermark
        if model.user_name.present?
          manipulate! do |img|
            text = Magick::Draw.new 
            text.gravity = Magick::CenterGravity
            text.fill = 'white'    
            text.pointsize = 40 
            text.stroke = 'none' 
            text.annotate(img, 0, 0, 0, 0, "#{model.user_name}") 
            img
          end
        end
      end

如果设置了模型上的user_name参数(在数据库上创建),则水印方法仅创建带有水印的图像。因此,当数据保存在数据库中时,此参数可供您上传者类使用。

这就是我所做的,而且效果非常好! 我希望它可以提供帮助!