thoughtbot / thoughtbot/factory_bot
Allow traits to override the class
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 8.2k
- Forks
- 2.6k
- PR merge metrics
- No merged PRs in 30d
Description
Problem this feature will solve
I'm refactoring code to use Single Table Inheritance and I'd really like to be able to override the class on each trait as opposed to defining a completely new factory for each subclass.
Desired solution
From an API perspective providing options to the trait call would be nice
factory :generator_performance do
trait :reactive_power_capability, class: '...' do
end
end
Alternatives considered
I'll either extract these to concrete classes now OR try the solution referenced at https://dev.to/epigene/simple-trick-to-make-factorybot-work-with-sti-j09
Additional context
In my case I have an ActiveRecord Model that represents a bunch of different documents. The current model definition and its validations are loose enough to allow for the many shapes but it is a mess. There is a large core of shared properties. Currently each of the document types are defined as FactoryBot traits.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No files or tests are named in the issue. Start by tracing the existing trait API and how FactoryBot resolves a factory's class, then compare that path with the proposed Single Table Inheritance use case. Done means a trait can override its class through the proposed option syntax without requiring a separate factory, with coverage for that behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rails, ruby
- Domain
- testing
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100