From 2b4c400dea2b1910a9972a991a320b3f6f827d06 Mon Sep 17 00:00:00 2001 From: Bin Le Date: Mon, 15 Jun 2020 17:27:01 -0400 Subject: [PATCH 1/4] update ruby version and gemset --- .ruby-version | 2 +- Gemfile | 2 +- Gemfile.lock | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.ruby-version b/.ruby-version index a9eea9a..e4b8527 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -ruby-2.3.8 +ruby-2.4.9 diff --git a/Gemfile b/Gemfile index be415d9..24266b5 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,7 @@ source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } -ruby '2.3.8' +ruby '2.4.9' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.2.0' diff --git a/Gemfile.lock b/Gemfile.lock index 8e6cef6..b8616ce 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -232,7 +232,7 @@ DEPENDENCIES web-console (>= 3.3.0) RUBY VERSION - ruby 2.3.8p459 + ruby 2.4.9p362 BUNDLED WITH 1.17.3 From 6f17843d3150432dc9928a699bfd492f1a46abed Mon Sep 17 00:00:00 2001 From: Bin Le Date: Mon, 15 Jun 2020 17:27:18 -0400 Subject: [PATCH 2/4] add user seed data --- db/seeds.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/db/seeds.rb b/db/seeds.rb index 1beea2a..bdedf93 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,3 +5,8 @@ # # movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) # Character.create(name: 'Luke', movie: movies.first) + +users = User.create([ + {name: 'Test user 1'}, + {name: 'Test user 2', email: 'test_user@testmicropost.com'} +]) From 386abb8d45665edc3ded27befd703b95ecbaa2e3 Mon Sep 17 00:00:00 2001 From: Bin Le Date: Mon, 15 Jun 2020 17:28:22 -0400 Subject: [PATCH 3/4] update ignore list --- .gitignore | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.gitignore b/.gitignore index ec0b196..9f07894 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,8 @@ # Ignore master key for decrypting credentials and more. /config/master.key + +# IDE files +.idea + + From e4369a5f69b108524d20ea1ff2b85f905f92f6e3 Mon Sep 17 00:00:00 2001 From: Bin Le Date: Mon, 15 Jun 2020 18:28:46 -0400 Subject: [PATCH 4/4] test websocket rails channel --- app/assets/javascripts/channels/microposts.coffee | 11 +++++++++++ app/channels/microposts_channel.rb | 9 +++++++++ app/controllers/microposts_controller.rb | 9 +++++++++ app/views/layouts/application.html.erb | 9 +++++++++ 4 files changed, 38 insertions(+) create mode 100644 app/assets/javascripts/channels/microposts.coffee create mode 100644 app/channels/microposts_channel.rb diff --git a/app/assets/javascripts/channels/microposts.coffee b/app/assets/javascripts/channels/microposts.coffee new file mode 100644 index 0000000..685c5e7 --- /dev/null +++ b/app/assets/javascripts/channels/microposts.coffee @@ -0,0 +1,11 @@ +App.microposts = App.cable.subscriptions.create "MicropostsChannel", + connected: -> + # Called when the subscription is ready for use on the server + + disconnected: -> + # Called when the subscription has been terminated by the server + + received: (data) -> + # Called when there's incoming data on the websocket for this channel + document.getElementById("message-bar").innerText = data.text + diff --git a/app/channels/microposts_channel.rb b/app/channels/microposts_channel.rb new file mode 100644 index 0000000..83bfc8e --- /dev/null +++ b/app/channels/microposts_channel.rb @@ -0,0 +1,9 @@ +class MicropostsChannel < ApplicationCable::Channel + def subscribed + stream_from "microposts" + end + + def unsubscribed + # Any cleanup needed when channel is unsubscribed + end +end diff --git a/app/controllers/microposts_controller.rb b/app/controllers/microposts_controller.rb index d01c574..cfd828e 100644 --- a/app/controllers/microposts_controller.rb +++ b/app/controllers/microposts_controller.rb @@ -44,6 +44,15 @@ def update if @micropost.update(micropost_params) format.html { redirect_to @micropost, notice: 'Micropost was successfully updated.' } format.json { render :show, status: :ok, location: @micropost } + + # action cable channel broadcasting + # + # @microposts = Micropost.all + # + # Broadcast messages typically consist of Ruby hashes, which are converted to JSON to + # go across the wire and end up as JavaScript objects. + ActionCable.server.broadcast 'microposts', + text: "post has been updated: #{@micropost.inspect}" else format.html { render :edit } format.json { render json: @micropost.errors, status: :unprocessable_entity } diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index ba7576e..fafb3c9 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -13,7 +13,16 @@
+ +
+

+
+
+ + <%= yield %>