Skip to content

Instantly share code, notes, and snippets.

@divineforest
Created May 21, 2013 12:22
Show Gist options
  • Save divineforest/5619400 to your computer and use it in GitHub Desktop.
Save divineforest/5619400 to your computer and use it in GitHub Desktop.
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