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

CableReady Operation Mode #219

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

marcoroth
Copy link
Member

Type of PR

Feature

Description

I had this one sitting around for a while, but thought I'll open this as a draft PR for visibility.

This PR allows CableReady and/or CableCar to operate in the cable_ready or turbo_stream operation mode.

# config/initializers/cable_ready.rb

CableReady.configure do |config|
  # Specify operations payload output format, options:
  # `:cable_ready` or `:turbo_stream`

  config.operation_mode = :turbo_stream
end

When the turbo_stream operation mode is set, it will output any enqueued CableReady operations as <turbo-stream> tags.

Example:

include CableReady::Broadcaster

cable_car.inner_html(selector: "#time", html: "<div>#{Time.now}</div>").dispatch

# => "<turbo-stream action=\"replace\" target=\"time\"><template><div>2022-08-29 23:20:03 +0200</div></template></turbo-stream>"

When the operation mode is set is set to cable_ready you can still output the turbo_stream format by calling .to_turbo_stream after the last operation:

cable_car.inner_html(selector: "#smelly", html: "<div>#{Time.now}</div>").to_turbo_stream

# => "<turbo-stream action=\"replace\" target=\"time\"><template><div>2022-08-29 23:20:03 +0200</div></template></turbo-stream>"

This also allows CableCar to be used in contexts where you usually would use the turbo_stream helper:

# app/controllers/some_controller.rb

def show
  render turbo_stream: cable_car.inner_html(selector: "#time", html: "<div>#{Time.now}</div>").dispatch_event(name: "requested", selector: "body")
end

Checklist

  • My code follows the style guidelines of this project
  • Checks (StandardRB & Prettier-Standard) are passing
  • This is not a documentation update

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.

None yet

1 participant