Skip to content

Add support for Turbo-streaming ViewComponents.#2595

Open
joelhawksley wants to merge 1 commit intomainfrom
1106
Open

Add support for Turbo-streaming ViewComponents.#2595
joelhawksley wants to merge 1 commit intomainfrom
1106

Conversation

@joelhawksley
Copy link
Copy Markdown
Member

Closes #1106.

"messages",
action: :append,
target: "messages",
renderable: MessageComponent.serializable(message: self),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is surprising. I thought serializable was producing an artifact for passing into ActiveJob, but that renderable was describing something that could render HTML for the front end. Isn't this mixing unrelated things?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conceptually, it's that we're making a renderable that can pass through the Active Job boundary by being serializable.

@bensheldon
Copy link
Copy Markdown

This definitely does what it says it does, and is very simple by simply intercepting new to assign a property. So I'm 👍🏻 on this without any project-lifecycle context.

Specifically as to project lifecycle, I think this API is fairly limiting and I wonder if it becomes difficult to extend. e.g. do the limitations become inherent to this implementation.

I'd offer that one alternative would be to create a Proxy object that looked something like this:

class ViewProxy
  def initialize(target, **kwargs)
    @target = target
    @initialize_kwargs = kwargs
    @with_component_kwargs ||= []
  end
  
  def with_component(**kwargs)
    @with_component_kwargs << kwargs
  end
  # etc. whatever
  
  def render
    @target.new(@initialize_kwargs).then do |instance|
      @with_component_kwargs.each { |k| instance.with_component(**kwargs) }
      # etc.
    end.render
  end
end

module Serializable
  def serializable(**kwargs)
    ViewProxy.new(self.class, **kwargs)
  end
end

MyComponent.serializable(foo: "bar").with_component(foo: "bar")

Co-authored-by: Ben Sheldon <47554+bensheldon@users.noreply.github.com>
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 this pull request may close these issues.

Turbo-Streaming ViewComponents

3 participants