Skip to content

Instantly share code, notes, and snippets.

@travisofthenorth
Last active July 30, 2016 15:29
Show Gist options
  • Save travisofthenorth/d9eba777a2e4eef193dfe2baae047280 to your computer and use it in GitHub Desktop.
Save travisofthenorth/d9eba777a2e4eef193dfe2baae047280 to your computer and use it in GitHub Desktop.
Override factory girl to enable first_or_create type functionality
module FactoryGirlHelper
BLACKLIST = [:user]
def first(args)
name = args.first
klass = name.to_s.camelize.constantize
conditions = args.last
if conditions.present? && conditions.is_a?(Hash)
klass.where(conditions).first
end
end
def first_or_create(*args)
result = first(args) if BLACKLIST.include?(args.first)
result || FactoryGirl.create(*args)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment