Created
January 31, 2020 16:11
-
-
Save henrik/ca24aa6cf7414d5bae2e6589c54c9ed5 to your computer and use it in GitHub Desktop.
FactoryBot extensions
This file contains 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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Copy-pasted out of a private repo.