Video encoding processor for CarrierWave

Recently I was wondering if it's really easy to create own processor / converter for CarrierWave. Well, the answer is YES. We've needed video converting utility in our app, that would output given video file in mp4 / webm format (suitable for both flash and HTML5 video players). Recently I also found new gem called voyeur - a nice ffmpeg wrapper (needs to be added in your Gemfile).
Custom processor will then look like this (put this in lib/carrierwave_processing/ dir): Usign this in uploader is very easy. But remember to define custom filename for version - encoded file will have different extension I also have custom initializer, that automagically loads all processors: And that's it - from this moment on your app will be able to convert uploaded video files to "displayable" format.
I hope that above solution will save you some time.

Video encoding processor for CarrierWave

Directory partitioning in Carrierwave

Lately I’ve switched upload handler gem from paperclip into carrierwave. Mostly because I wanted to try something else. But as I found out, making carrierwave behave as I expect it to is much easier. But that could be subject for different post. Anyway - while configuring uploader to my expectations, I’ve found out, that there is no pre-build way to define directory partitioning (for performance reasons it good to have limited number of files inside a directory). Luckily this can be done in very easy way - we just have to define our own method responsible for partition generation. Sample below make three partitions based on id, ie. 001/234/567/ for id=1234567

# encoding: utf-8
class PictureUploader < CarrierWave::Uploader::Base
  storage :file
  after :remove, :delete_empty_upstream_dirs

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{get_last_dir_part(model.id)}"
  end
  
  ## define how to partition directory
  def get_last_dir_part(modelid)
    p = modelid.to_s.rjust(9, '0')
    "#{p[0,3]}/#{p[3,3]}/#{p[6,3]}"
  end

  ## TAKEN FROM GITHUB
  ## https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Make-a-fast-lookup-able-storage-directory-structure
  def delete_empty_upstream_dirs
    path = ::File.expand_path(store_dir, root)
    Dir.delete(path) # fails if path not empty dir
  rescue SystemCallError
    true # nothing, the dir is not empty
  end

end

Directory partitioning in Carrierwave

Devise - send confirmation mail manually (or delay them)

Sometimes (well, let's be honest - this is completelly not a common situation) you want to send confirmation mails manually. Ie. after completing two step registration process - in first step user is created, on second step he fill rest of required data and AFTER that he receives confirmation mail. Normally, for confirmable user models, confirmation mail would be send just after completing step one. How to change this? Well, very easy. Just override

send_confirmation_instructions
method in your model:
class User < ActiveRecord::Base

  ## DEVISE
  devise :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable, :validatable

  ## ACCESSIBLE
  attr_accessible :email, :password, :password_confirmation, :remember_me

  def send_confirmation_instructions(send=false)
    generate_confirmation_token! if self.confirmation_token.nil?
    ::Devise.mailer.confirmation_instructions(self).deliver if send
  end
end


Of course, as Piotr noticed, similar method can also be used to delay confirmation mails (using delayed_job):
  def send_confirmation_instructions
    generate_confirmation_token! if self.confirmation_token.nil?
    ::Devise.mailer.delay.confirmation_instructions(self)
  end

Devise - send confirmation mail manually (or delay them)

Upgrading to Rails 3.1.1 and problems with asset pipeline

Today, after upgrading app to rails 3.1.1 (from 3.0) I've noticed, that for some reason sass-rails's helper

image-path()
didn't work as expected. It should generate something like this:
url('/assets/image.png')

instead it was generating this code:
url('/images/image.png')

Result? No images loaded from within css file were displayed. It was caused by a known bug in sass-rails gem, which supposed to be fixed by now. After upgrading to suggested version (3.1.4), paths were still not generated as they supposed to be. I had to define explicitly
rc1
version of this gem (
gem 'sass-rails', "~> 3.1.5.rc.1"
), to make it work. And, well, clear
tmp/cache/
folder.

Upgrading to Rails 3.1.1 and problems with asset pipeline

Fork me on Github And spread the word!