Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@henrik 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