Redline Software Inc. - Winnipeg's Leader in Ruby on Rails Development

File_column Image Regeneration

If you use file_column for some projects and find that you want to change the thumbnail sizes that you’ve been using for models, then hopefully this little bit of code can help.

Add this as a rake task and run it as rake "filecolumn:regenerate[Model, field]" RAILS_ENV=production where Model is the model you want to regenerate images for and field is the filecolumn field on the model. The task uses Rake arguments, so you’ll need Rake 0.8.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
namespace :filecolumn do
  desc "Regenerate filecolumn images"
  task :regenerate, :model, :field, :needs => :environment do |task,args|
    if args.any?{|k,v| v.blank?}
      puts "Usage: #{task.name}[Model,field]"
    else
      klass, field = args.model.constantize, args.field

      klass.all.each do |obj|
        puts "Regenerating #{klass} #{obj.id}"
        next if obj.send(field).nil?
        state = obj.send("#{field}_state")
        state.instance_variable_set(:@just_uploaded, true)
        state.transform_with_magick
      end
    end
  end
end

The code essentially boils down to 2 lines…

  • setting @just_uploaded to true on the state object
  • calling the transform_with_magick method on the state object

Comments