Last active
September 2, 2018 05:59
-
-
Save ClayShentrup/b5d4dc711989b0fad3a99d2ae445367d to your computer and use it in GitHub Desktop.
Three ways to do STI factories
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
# assets.rb | |
FactoryBot.define do | |
factory(:asset) do | |
# definition | |
end | |
end | |
# audio_assets.rb | |
FactoryBot.define do | |
factory(:audio_asset, parent: :asset, class: AudioAsset) do | |
# definition | |
end | |
end |
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
# assets.rb | |
FactoryBot.define do | |
trait(:asset) do | |
# definition | |
end | |
factory(:audio_asset, traits: %i[asset]) do | |
# definition | |
end | |
# other subclasses, like video_asset, defined here too | |
end |
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
# shared_traits.rb | |
FactoryBot.define do | |
trait(:asset) do | |
# definition | |
end | |
end | |
# audio_assets.rb | |
FactoryBot.define do | |
factory(:audio_asset) do | |
asset # Ambiguous: you have to know this is a trait, not an association | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment