Created
May 21, 2013 12:22
-
-
Save divineforest/5619400 to your computer and use it in GitHub Desktop.
This file contains 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
class Seed | |
@ids = {} | |
@classes = {} | |
def self.create(factory_name, attributes = nil) | |
raise "attributes must be a hash" if attributes && !attributes.is_a?(Hash) | |
model = FactoryGirl.create(factory_name, attributes) | |
@ids[factory_name] = model.id | |
@classes[factory_name] = model.class | |
model | |
end | |
def self.[](factory_name) | |
seed_id = @ids[factory_name] | |
if seed_id | |
seed_class = @classes[factory_name] | |
seed_class.find_by_id(seed_id) || create(factory_name) | |
else | |
create(factory_name) | |
end | |
end | |
end | |
def seed(factory_name) | |
Seed[factory_name] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment