Skip to content

Commit 917acb1

Browse files
committed
secure the actioncable channel
restrict to company
1 parent 11f928b commit 917acb1

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

NOTES.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,24 @@ When our user clicks on the "Cancel" link for the edit quote form:
3535
The link is within a Turbo Frame of id dom_id(quote), so Turbo will only replace the content of this frame
3636
The link navigates to the Quotes#index page that contains a Turbo Frame with id dom_id(quote) that contains the HTML for this quote
3737
Turbo replaces the content of the dom_id(quote) Frame containing the form with the HTML for this quote
38+
39+
Wrap up
40+
41+
Transforming our application into a real-time one only took two lines of code, thanks to Turbo Rails.
42+
43+
In our Quote model, we set three callbacks to broadcast creations, updates, and deletions to the "quotes" stream. Thanks to the broadcasts_to method, those three callbacks can be defined in one line.
44+
In our Quotes#index view, we explicitly mentioned that we want to subscribe to the changes that are broadcasted to the "quotes" stream.
45+
46+
47+
DEVISE IN TESTS:
48+
in test_helper.rb: (ActiveSupport::TestCase)
49+
include Devise::Test::IntegrationHelpers
50+
use helper for any tests requiring user:
51+
sign_in users(:accountant)
52+
53+
CHANNEL SECURITY FOR ACTIONCABLE;
54+
model:
55+
broadcasts_to ->(quote) { [quote.company, "quotes"] }, inserts_by: :prepend
56+
view:
57+
<%= turbo_stream_from(current_company, "quotes") %>
58+

app/models/quote.rb

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,5 @@ class Quote < ApplicationRecord
55

66
scope :desc_id_ordered, -> { order(id: :desc) }
77

8-
# QUESTIONS:
9-
# - how to limit access / specify policy for channels?
10-
# - how to test
11-
# after_create_commit -> { broadcast_prepend_to "quotes", target: "quotes", partial: "quotes/quote", locals: { quote: self } }
12-
# after_create_commit -> { broadcast_prepend_later_to "quotes" }
13-
# after_update_commit -> { broadcast_replace_later_to "quotes" }
14-
# Can't use self.id to broadcast later.
15-
# after_destroy_commit -> { broadcast_remove_to "quotes" }
16-
# The above three callbacks are equivalent to the following single line
17-
broadcasts_to ->(quote) { "quotes" }, inserts_by: :prepend
8+
broadcasts_to ->(quote) { [quote.company, "quotes"] }, inserts_by: :prepend
189
end

app/views/quotes/index.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ COMMENT
2828
data: { turbo_frame: dom_id(Quote.new) } %>
2929
</div>
3030

31-
<%= turbo_stream_from "quotes" %>
31+
<%= turbo_stream_from current_company, "quotes" %>
3232
<%= turbo_frame_tag Quote.new %>
3333
<%= turbo_frame_tag "quotes" do %>
3434
<%= render @quotes %>

0 commit comments

Comments
 (0)