Skip to content

Instantly share code, notes, and snippets.

@henrik
Created January 31, 2020 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henrik/ca24aa6cf7414d5bae2e6589c54c9ed5 to your computer and use it in GitHub Desktop.
Save henrik/ca24aa6cf7414d5bae2e6589c54c9ed5 to your computer and use it in GitHub Desktop.
FactoryBot extensions
# Do this in a factory to get this method:
# require "factory_bot_extensions"
# We should just load it once somewhere, but have yet to figure out where because FactoryBot is loaded in some fancy way.
#
# Example usage:
#
# factory :item do
# transient do
# company {
# if passed_in?(:contract)
# contract.company
# else
# FactoryBot.build(:company)
# end
# }
# end
# end
class FactoryBot::Evaluator
def passed_in?(name)
# Also check that we didn't pass in nil.
passed_in_even_if_nil?(name) && send(name)
end
def passed_in_even_if_nil?(name)
# https://groups.google.com/forum/?fromgroups#!searchin/factory_girl/stack$20level/factory_girl/MyYKwbq76d0/JrKJZCgaXMIJ
__override_names__.include?(name)
end
end
@henrik
Copy link
Author

henrik commented Jan 31, 2020

Copy-pasted out of a private repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment