Last active
July 30, 2016 15:29
-
-
Save travisofthenorth/d9eba777a2e4eef193dfe2baae047280 to your computer and use it in GitHub Desktop.
Override factory girl to enable first_or_create type functionality
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
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