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

Rework CableReady's ActionController::Renderers #260

Merged
merged 5 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions docs/guide/cable-car.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ One of the most exciting possibilities for Cable Car is to send operations in re

```ruby
class HomeController < ApplicationController
include CableReady::Broadcaster

def index
render operations: cable_car.console_log(message: "hi")
render cable_ready: cable_car.console_log(message: "hi")
end
end
```
Expand Down Expand Up @@ -106,7 +108,7 @@ class HomeController < ApplicationController
include CableReady::Broadcaster

def ride
render operations: cable_car.inner_html("#users", html: "<span>winning</span>")
render cable_ready: cable_car.inner_html("#users", html: "<span>winning</span>")
end
end
```
Expand Down
4 changes: 3 additions & 1 deletion docs/guide/cableready-everywhere.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ patch 'users/:id/message', to: 'users#message'

```ruby [app/controllers/users_controller.rb]
class UsersController < ApplicationController
include CableReady::Broadcaster

def message
render operations: cable_car.console_log(message: "Hi!")
render cable_ready: cable_car.console_log(message: "Hi!")
end
end
```
Expand Down
17 changes: 15 additions & 2 deletions lib/cable_ready/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,27 @@ class Engine < Rails::Engine
SanityChecker.check! unless Rails.env.production?
end

initializer "cable_ready.mimetype" do
Mime::Type.register "text/vnd.cable-ready.json", :cable_ready
end

initializer "cable_ready.renderer" do
ActiveSupport.on_load(:action_controller) do
ActionController::Renderers.add :operations do |operations, options|
warn "DEPRECATED: CableReady's `render operations:` call has been renamed to `render cable_ready:`. Please update your render call."

response.content_type ||= Mime[:cable_ready]
render json: operations.dispatch
response.headers["X-Cable-Ready-Version"] = CableReady::VERSION

render json: operations.respond_to?(:dispatch) ? operations.dispatch : operations
end

Mime::Type.register "application/vnd.cable-ready.json", :cable_ready
ActionController::Renderers.add :cable_ready do |operations, options|
response.content_type ||= Mime[:cable_ready]
response.headers["X-Cable-Ready-Version"] = CableReady::VERSION
Copy link
Contributor

Choose a reason for hiding this comment

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

The addition of the version header will be helpful.


render json: operations.respond_to?(:dispatch) ? operations.dispatch : operations
end
end
end

Expand Down