draper icon indicating copy to clipboard operation
draper copied to clipboard

Usage with `Turbo::Broadcastable`

Open Alexey1100 opened this issue 3 years ago • 1 comments

Looking for a good practice when using decorated objects inside templates broadcasted with Turbo::Broadcastable.

Since the rendering happens in Turbo::Streams::ActionBroadcastJob, the objects are getting serialised (#817) and the decoration is lost.

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

Alexey1100 avatar Jun 18 '21 13:06 Alexey1100

This is the quick solution I'm using currently, overriding the Turbo::Streams::ActionBroadcastJob job, as pointed by dhh in https://github.com/hotwired/turbo-rails/issues/190

class Turbo::Streams::ActionBroadcastJob < ApplicationJob
  def perform(stream, action:, target:, **rendering)
    rendering[:locals]&.transform_values! do |v|
      v.decorate
    rescue Draper::UninferrableDecoratorError, NoMethodError
      v
    end

    Turbo::StreamsChannel.broadcast_action_to stream, action: action, target: target, **rendering
  end
end

Alexey1100 avatar Jun 23 '21 18:06 Alexey1100