welcome

This is the homepage and weblog of Bryan P. Mills, a web designer and developer near Philadelphia.

more about me

projects

Oct 07 2008

Seeding attachment_fu image data using rake

I've only recently discovered the practice of using rake tasks to seed junk data in my database to help, but thanks to the Populator and faker gems, it couldn't be simpler. It's lot less frustrating than messing with fixtures or migrations.

Working on a site where users upload a lot of images, getting a bunch of random image data loaded in is key. Sadly, Populator isn't well suited to fill in all the fields required by attachment_fu, and it would get really complicated when dealing with thumbnails. However, I found a good solution by going straight through the model itself.

Here's a snippet of code I recently wrote using Populator for user data, but generated each user's avatar by going through the attachment_fu model:

task :populate => :environment do
  require 'populator'
  require 'faker'
  require 'action_controller/test_process'

  [User, Prompt, Avatar].each(&:delete_all)

  User.populate 20 do |user|
    user.login            =   Faker::Name.first_name
    user.email            =   Faker::Internet.email
    #password is 'test'
    user.crypted_password =   "00742970dc9e6319f8019fd54864d3ea740f04b1"
    user.salt             =   "7e3041ebc2fc05a40c60028e2c4901a81035d3cd"
    user.activation_code  =   "8f24789ae988411ccf33ab0c30fe9106fab32e9a"
    user.activated_at     =   [nil, (2.months.ago)]
    user.created_at       =   2.months.ago..Time.now
    user.enabled          =   1
    user.about            =   Populator.sentences(2..5)

    #create user's avatar
    path_prefix = RAILS_ROOT + "/public/avatars/seed_images/"
    seed_images = Dir.entries(path_prefix)
    seed_images.delete_if { |x| x == "." || x == ".."}

    TEST_FILE = path_prefix + seed_images[rand(seed_images.length)]
    avatar = Avatar.create
    avatar.uploaded_data =
        ActionController::TestUploadedFile.new(TEST_FILE,'image/jpg')

    avatar.send :process_attachment
    avatar.user_id = user.id
    avatar.save
  end
end

Essentially, I'm randomly picking a file from my seed_images folder to be passed through attachmentfu. The trick to getting attachmentfu to work it's magic outside of the browser is to simulate a file upload using the ActionController::TestUploadedFile method, assign that to the uploaded_data attribute on the model and call the process attachment method.

This same method will work for testing the mode as well. Although, in your rake task, don't forget to require 'action_controller/test_process' or it won't work.

Happy image seeding.

things

  • Populator

    my new favorite ruby gem of the moment

  • CSS2.1 Spec

    Read it. Learn it. Love it.

  • Upjax

    A little widget for ajax-y image uploads

  • Flight404

    The most stunning work I've seen in Processing

  • House of Cards

    Radiohead music video made with Processing

  • Expression Engine

    I'm pretty impressed so far.

  • Now on Slicehost

    Moved to Slicehost. I'm living on the edge, now.