Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow traits to override the class #1722

Open
jeropaul opened this issue Jan 16, 2025 · 1 comment
Open

Allow traits to override the class #1722

jeropaul opened this issue Jan 16, 2025 · 1 comment
Labels

Comments

@jeropaul
Copy link

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.

@ildarkayumov
Copy link

hi @jeropaul

did you try to use Nested Factories
Something like

factory :generator_performance do
    # some common attributes are defined here

    factory :reactive_power_capability do
       # some specifics
    end
end

for STI

class GeneratorPerformance < ActiveRecord::Base
end

class ReactivePowerCapability < GeneratorPerformance
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants