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

Configurable ID factory for build_stubbed #1605

Open
olivier-thatch opened this issue Dec 8, 2023 · 0 comments
Open

Configurable ID factory for build_stubbed #1605

olivier-thatch opened this issue Dec 8, 2023 · 0 comments
Labels

Comments

@olivier-thatch
Copy link
Contributor

olivier-thatch commented Dec 8, 2023

Problem this feature will solve

We use a slightly unconventional ID scheme in our DB. Rather than the usual auto-incrementing IDs or UUIDs, we use string IDs like those found in Stripe's API. (Basically our IDs look like <prefix>_<uuid> where prefix is unique to each model class, but that's not relevant to the request.)

Today, FactoryBot only supports auto-incrementing IDs or UUIDs when building stubbed models.

Desired solution

It would be great if there was a way to provide a callable to FactoryBot to be used to generate IDs for stubbed models. What I had in mind was something like this:

FactoryBot.build_stubbed_id_factory = lambda do |result_instance|
  if funky_model_has_custom_id_generator?(result_instance)
    generate_custom_id(result_instance)
  end

FactoryBot would use the ID factory if it's defined and returns a non-nil result, or fall back to the current behavior otherwise.

Alternatives considered

Right now we're directly patching FactoryBot, but that's obviously not great.

Here's our patch FWIW:

module FactoryBot
  module Strategy
    class Stub
      orig_next_id = instance_method(:next_id)
      define_method(:next_id) do |result_instance|
        if result_instance.class.respond_to?(:generate_id)
          result_instance.class.generate_id
        else
          orig_next_id.bind_call(self, result_instance)
        end
      end
      private :next_id
    end
  end
end

Additional context

I'm happy to submit a PR for this feature if you think it would be a nice addition to the gem.

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

1 participant