-
-
Save maunovaha/c9a32e217f5dab5d45a7055c69460e21 to your computer and use it in GitHub Desktop.
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 FactoryGirl | |
module Syntax | |
module Methods | |
def find_or_create(name, *attributes, &block) | |
attributes = FactoryGirl.attributes_for(name, *attributes) | |
klass = FactoryGirl.factory_by_name(name).build_class | |
enums = klass.defined_enums | |
find_attributes = attributes.clone | |
find_attributes.keys.each do |key| | |
find_attributes[key] = enums[key.to_s][find_attributes[key]] if enums.has_key?(key.to_s) | |
end | |
result = klass.find_by(find_attributes, &block) | |
result || FactoryGirl.create(name, attributes, &block) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment