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

Refactor stories and chapters broadcast ? #43

Open
anthony-robin opened this issue Aug 15, 2023 · 0 comments
Open

Refactor stories and chapters broadcast ? #43

anthony-robin opened this issue Aug 15, 2023 · 0 comments
Labels
question Further information is requested refactoring Code refactoring

Comments

@anthony-robin
Copy link
Collaborator

Turbo stream doesn't play nicely with current_user reference in partial. This is because this variable is not defined in the context of the broadcast as the same partial is rendered to every subscribed streams.

This prevent to use policies in these views are the rely on current_user to work.

A workaround could be to render an intermediate partial that would asynchronously request the endpoint in the context of each user with turbo_frame_tag.

Pros:

  • we can safely use current_user and policies on the partial

Cons:

  • need an intermediate partial to tweak it
  • depending on the number of user subscribed to the stream it would make a server request for any of them and could be very heavy for a server

Implementation example

# app/models/story.rb

after_create_commit do
  broadcast_prepend_to :stories, partial: "stories/story_frame"
end
/ /app/views/stories/_story_frame.html.slim

= turbo_frame_tag story, src: story_path(story)
/ /app/views/stories/show.html.slim

= turbo_frame_tag @story do
  / here the view

Reference blog article: https://www.colby.so/posts/conditional-rendering-with-turbo-stream-broadcasts

@anthony-robin anthony-robin converted this from a draft issue Aug 15, 2023
@anthony-robin anthony-robin added question Further information is requested refactoring Code refactoring labels Aug 15, 2023
@anthony-robin anthony-robin moved this to 📋 Backlog in Fabelia backlog Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested refactoring Code refactoring
Projects
Status: 📋 Backlog
Development

No branches or pull requests

1 participant