maxiperez
maxiperez

Reputation: 1480

carrierwave cache images and activerecord

I am using Rails 3 and carrierwave gem.

My problem is the next:

I am coding a wizard form with multiple models and relations. So My application has products which has many images through Image model (Its has been mounted with carrierwave).

Because the form has many steps I want save temporaly the images with cache option in CarrierWave. then i last step i want recover the images and load my product. So I create a model called TempData which save temporaly data (In this case cache_name), a type_identifier and a mark can_be_deleted.

For example these are my models:

Product Model:

class Product < ActiveRecord::Base
  has_many :images, :as => :imageable, :dependent => :destroy
  def save_images_in_temporal_folder(images_attributes)
    begin
      uploader = ImageUploader.new
      images_attributes.to_hash.each do |image|
        uploader.cache!(image[1]['filename'])
        TempData.create(:data => uploader.cache_name, :can_deleted => false, :type_name => TempDataType.product_image_upload_to_words)
      end
    rescue

    end
  end

  def load_images_from_temporal_folder
    begin
      uploader = ImageUploader.new
      tmp_images = []
      TempData.find_all_by_can_deleted_and_type_name(false, TempDataType.product_image_upload_to_words).map(&:data).each do |cache_name|
        tmp_images << uploader.retrieve_from_cache!(cache_name)
      end
      tmp_images
    rescue

    end
  end
end

Image model

class Image < ActiveRecord::Base
    attr_accessible :imageable_type, :imageable_id, :filename

    belongs_to :imageable, :polymorphic => true
    mount_uploader :filename, ImageUploader

    #validates :filename, :presence => true
end

My ImageUploader:

class ImageUploader < CarrierWave::Uploader::Base
  configure do |config|
    config.remove_previously_stored_files_after_update = false
  end

  include CarrierWave::RMagick

  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def self.store_dir_for_file(model, mounted, filename, version = "")
    filename = "#{version}_#{filename}" unless version.blank?
    "uploads/#{model.class.to_s.underscore}/#{mounted}/#{model.id}/#{filename}"
  end

  process :resize_to_limit => [200, 300]

  version :thumb do
    process :resize_to_limit => [50, 50]
  end

  version :big do
    process :resize_to_limit => [400, 400]
  end

  def extension_white_list
    %w(jpg jpeg gif png)
  end

end

So How i save/cache the images with model and mount information?. How retrieve the cache and load my product with images in cache?

Thanks in advance. Sorry for my english and if you need more information comment this post.

Upvotes: 0

Views: 3123

Answers (1)

maxiperez
maxiperez

Reputation: 1480

View the next link:

CarrierWave with ActiveResource

Also I create a model which storage temporal data. Here I save the cache_name of carrierwave. When i save the product mark these register as can_be_deleted. Then I write a task which delete these registers and clean Carrierwave cache files.

Upvotes: 1

Related Questions