Guest User

Untitled

a guest
Jun 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. module FactoryBot
  2. module Syntax
  3. module Methods
  4. def find_or_create(name, *attributes, &block)
  5. attributes = FactoryBot.attributes_for(name, *attributes)
  6. klass = FactoryBot.factory_by_name(name).build_class
  7. enums = klass.defined_enums
  8. find_attributes = attributes.clone
  9. find_attributes.keys.each do |key|
  10. find_attributes[key] = enums[key.to_s][find_attributes[key]] if enums.has_key?(key.to_s)
  11. end
  12. result = klass.find_by(find_attributes, &block)
  13. result || FactoryBot.create(name, attributes, &block)
  14. end
  15. end
  16. end
  17. end
Add Comment
Please, Sign In to add comment