-
Notifications
You must be signed in to change notification settings - Fork 9
/
instructions.txt
175 lines (130 loc) · 4.75 KB
/
instructions.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
Rails 6.1 ActionCable
# Create rails app:
rails new cableguy --database=mysql
cd cableguy
# create a model
rails g resource libraryBook title status status_date:datetime
rails db:create
rails db:migrate
add seeds:
libary_books = LibraryBook.create([{title: 'Moby Dick', status: 'on-shelf'}, {title: 'Cruel Shoes', status: 'on-shelf'}])
rails db:seed
# update routes
root "library_books#index"
# get all the books in the controller
def index
@books = LibraryBook.all
end
# create index in app/views/libary_books/index.html.erb
<h1>Our Books</h1>
<% if [email protected]? %>
<% for book in @books %>
<% end %>
<% end %>
# add bootstrap to layout application.html.erb
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.bundle.min.js" integrity="sha384-LtrjvnR4Twt/qOuYxE721u19sVFLVSA4hf/rRt6PrZTmiPltdZcI7q7PXQBYTKyf" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
# wrap yeild in container div
<div class='container'>
<%= yield %>
</div>
# quick check of books index:
<div class='row'>
<div class='col'>
<%= book.title%>
</div>
<div class='col'>
<%= book.status.titleize%>
</div>
</div>
# create a partial to render a book
# move code into it
<div class='row'>
<div class='col'>
<%= book.title%>
</div>
<div class='col'>
<%= book.status.titleize%>
</div>
</div>
# call partial in index.html.erb
<%= render partial: "one_book", locals: {book: book} %>
######## end of the basics ########
# add stimulus
yarn add stimulus
# add stimulus stuff to application.js
import { Application } from "stimulus"
import { definitionsFromContext } from "stimulus/webpack-helpers"
const application = Application.start()
const context = require.context("../controllers", true, /\.js$/)
application.load(definitionsFromContext(context))
# create a stimulus controller app/javascript/contollers/book_status_controller.js
import { Controller } from "stimulus"
import consumer from '../channels/consumer';
export default class extends Controller {
static targets = ['bookstatus']
connect() {
console.log('Will create subscription to: channel: "LibraryBookStatusChannel" library_book_id: ' + this.data.get('bookid'));
this.channel = consumer.subscriptions.create({ channel: 'LibraryBookStatusChannel', library_book_id: this.data.get('bookid') }, {
connected: this._cableConnected.bind(this),
disconnected: this._cableDisconnected.bind(this),
received: this._cableReceived.bind(this),
});
}
_cableConnected() {
// Called when the subscription is ready for use on the server
console.log('_cableConnected');
}
_cableDisconnected() {
// Called when the subscription has been terminated by the server
console.log('_cableDisconnected');
}
_cableReceived(data) {
console.log('_cableReceived');
// Called when there's incoming data on the websocket for this channel
this.bookstatusTarget.innerHTML = data.message;
if (data.status.localeCompare('completed', 'en', { sensitivity: 'base' })) {
console.log('completed');
this.bookstatusTarget.innerHTML = data.message;
} else {
console.log(data.status);
this.bookstatusTarget.innerHTML = data.message;
}
}
}
# add stimulus stuff to index.html.erb
<div data-controller="book-status" data-book-status-bookid="<%= book.id %>">
<div data-book-status-target='bookstatus'>
<%= render partial: "one_book", locals: {book: book} %>
</div>
</div>
#restart server
# see output in chrome console
# see nothing gets update even we update model
#### model stuff ####
# create channel in app/channels/library_book_status_channel.rb
class LibraryBookStatusChannel < ApplicationCable::Channel
def subscribed
# the param name is set in the stimulus controller
stream_from "LibraryBookStatusChannel:#{params[:library_book_id]}"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
stop_all_streams
end
end
# add callback on after_commit in libary_book.rb
after_commit :broadcast_me
def broadcast_me
ActionCable.server.broadcast "LibraryBookStatusChannel:#{id}", {
status: status.titleize,
message: LibraryBooksController.render(partial: 'one_book', locals: { book: self }).squish
}
end
#### why nothing works!?
# add redis
gem 'redis'
#update cable.yml
development:
adapter: redis