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

Missing a controller-like layer when using Turbo::Broadcastable #190

Closed
Alexey1100 opened this issue Jun 18, 2021 · 2 comments
Closed

Missing a controller-like layer when using Turbo::Broadcastable #190

Alexey1100 opened this issue Jun 18, 2021 · 2 comments

Comments

@Alexey1100
Copy link

Take this example:

class Clearance < ApplicationRecord
  belongs_to :petitioner, class_name: "Contact"
  belongs_to :examiner,   class_name: "User"

  after_create_commit :broadcast_later

  private
  
    # Sends <turbo-stream action="replace" target="clearance_5"><template><div id="clearance_5">Other partial</div></template></turbo-stream>
    # to the stream named "identity:2:clearances"
    def broadcast_later
      broadcast_replace_later_to examiner.identity, :clearances, target: self, partial: "clearances/other_partial", locals: { clearance: self }
    end
end

We are scheduling a job to render the template and broadcast it later.
Overall it’s pretty similar to the way we send emails, right?
However for the emails we have an ActionMailer which acts as a controller layer and initialises the objects before passing to views.

Here we don’t have any intermittent layer, so the background job just loads the objects from database and passes them directly to views, leaving us without an option to modify or set up the objects before rendering, e.g. apply a decorator.

So I’m wondering if there were already any thoughts on implementing something like a Broadcaster layer, similar to Mailer?

@dhh
Copy link
Member

dhh commented Jun 19, 2021

The job enqueued by broadcast_replace_later_to is incredibly simple: https://github.com/hotwired/turbo-rails/blob/main/app/jobs/turbo/streams/broadcast_job.rb. It's very easy just to setup your own job with custom handling. Don't think we need a separate controller layer here on top.

@dhh dhh closed this as completed Jun 19, 2021
@Alexey1100
Copy link
Author

Alexey1100 commented Jun 19, 2021

@dhh thanks for the reference :)
Sure, we can always customize the job.

I was just wondering if there any thoughts on more structured approach, given it’s very early days of Hotwire itself and the rails integration in particular.

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

No branches or pull requests

2 participants