Created
February 27, 2012 01:24
-
-
Save justinko/1920516 to your computer and use it in GitHub Desktop.
FactoryGirl for dev seeding and tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: '[email protected]', password: 'password' | |
entity = insert :entity | |
insert :user, entity: entity, email: '[email protected]', 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot, this really helped me out!