Skip to content

Instantly share code, notes, and snippets.

@justinko
Created February 27, 2012 01:24
Show Gist options
  • Save justinko/1920516 to your computer and use it in GitHub Desktop.
Save justinko/1920516 to your computer and use it in GitHub Desktop.
FactoryGirl for dev seeding and tests
# lib/tasks/app.rake
namespace :app do
desc 'Seed the current env db with "dummy" data'
task seed: :environment do
require './lib/seed'
Seed.start
end
end
# Make sure to add factory_girl_rails to the "development" and "test" bundler groups.
module Seed
extend Factory::Syntax::Methods
def self.start
puts 'Clearing the existing data'
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean
insert :admin, email: 'admin@test.com', password: 'password'
entity = insert :entity
insert :user, entity: entity, email: 'user@test.com', password: 'password'
program = insert :program
insert :member, entity: entity
puts 'Done'
end
def self.insert(factory, attributes = {})
attributes.reverse_merge! attributes_for(factory)
puts "Inserting: #{factory}, #{attributes}"
create factory, attributes
end
end
@hadees
Copy link

hadees commented Aug 24, 2012

Thanks a lot, this really helped me out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment