Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

callback issue with carrierwave and mongoid

I am using carrierwave and mongoid on a rails 3 application and am having an issue with an after_save callback. Consider the following

class Video
  include Mongoid::Document

  field :name  

  mount_uploader :file, VideoUploader

  after_create :enqueue_for_encoding

  protected

  def enqueue_for_encoding
     // point your encoding service to where it expects the permanent file to reside
     // in my case on s3 
  end

end

My issue is that in my enqueue_for_encoding method, file.url points to the local tmp directory not the s3 directory.

How do I get my enqueue_for_encoding method to be called when file.url points to s3?

Thanks!

Jonathan

like image 624
Jonathan Avatar asked Mar 18 '11 14:03

Jonathan


2 Answers

Check out carrierwave's howto page on Callbacks

https://github.com/jnicklas/carrierwave/wiki/How-to%3A-use-callbacks

It worked for me

like image 140
mindwork Avatar answered Sep 18 '22 23:09

mindwork


Okay, I figured it out. To took a bit of hacking. So currently carrierwave does not expose an after_create hook, all of it persisting and processing happens in the after_save callback. Here is the code I used to work around it:

# Video.rb

  mount_uploader :file, VideoUploader

  # overwrite the file setting to flag the model that we are creating rather than saving
  def file=(obj)
    @new_file = true
    super(obj)
  end

  # chain the store_file! method to enqueue_for_encoding after storing the file AND
  # if the file is new
  alias_method :orig_store_file!, :store_file!
  def store_file!
    orig_store_file!
    if @new_file #means dirty
      @new_file = false
      enqueue_for_encoding
    end
    true
  end

UPDATE

Woops -- that didn't work. It almost did -- the url is correct, but it is being fired permanently. Meaning the file is still in process of being loaded, and is not fully stored when enqueue_for_encoding is called

like image 23
Jonathan Avatar answered Sep 17 '22 23:09

Jonathan