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

Usage with Turbo::Broadcastable #910

Open
Alexey1100 opened this issue Jun 18, 2021 · 1 comment · May be fixed by #929
Open

Usage with Turbo::Broadcastable #910

Alexey1100 opened this issue Jun 18, 2021 · 1 comment · May be fixed by #929

Comments

@Alexey1100
Copy link

Alexey1100 commented Jun 18, 2021

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
Copy link
Author

This is the quick solution I'm using currently, overriding the Turbo::Streams::ActionBroadcastJob job, as pointed by dhh in hotwired/turbo-rails#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

Alexander-Senko added a commit to Alexander-Senko/draper that referenced this issue Feb 6, 2024
Overriding defaults for Turbo broadcast jobs allows one to get decorated
objects in model partials by default.

Resolves drapergem#910.
Requires drapergem#928.
@Alexander-Senko Alexander-Senko linked a pull request Feb 6, 2024 that will close this issue
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant