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

Method Chaining #39

Open
rickychilcott opened this issue Dec 3, 2023 · 0 comments
Open

Method Chaining #39

rickychilcott opened this issue Dec 3, 2023 · 0 comments

Comments

@rickychilcott
Copy link

rickychilcott commented Dec 3, 2023

This for this gem, it's making my cable_ready migration possible without totally re-writing. Would you be interested in adding method chaining? Similar to cable_car from cable_ready?

I worked this out:

class ApplicationController < ActionController::Base
  def stream_car
    StreamCar.new(method(:turbo_stream))
  end

  class StreamCar
    def initialize(meth)
      @meth = meth
      @buffer = ActiveSupport::SafeBuffer.new
    end

    def to_s
      @buffer.to_s
    end

    def to_str
      @buffer.to_str
    end

    def method_missing(name, *args, **kwargs)
      @buffer << @meth.call.send(name, *args, **kwargs).html_safe

      self
    end
  end
end

Use by...

  def update
    render turbo_stream: stream_car.remove_css_class("#launch-card").set_value("#user_invite_form_name", value: "")
  end

Surprisingly, it works. Happy to make a PR and battle-test it more; just wanted to ask.

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

No branches or pull requests

1 participant