From e65efc29cb43693e4ecb2d3ec636b57bc770427e Mon Sep 17 00:00:00 2001 From: Greg Flaherty Date: Wed, 25 May 2022 12:42:19 -0400 Subject: [PATCH 1/7] Refactor: change chat channel to social channel --- app/channels/chat_channel.rb | 11 ----------- app/channels/social_channel.rb | 11 +++++++++++ 2 files changed, 11 insertions(+), 11 deletions(-) delete mode 100644 app/channels/chat_channel.rb create mode 100644 app/channels/social_channel.rb diff --git a/app/channels/chat_channel.rb b/app/channels/chat_channel.rb deleted file mode 100644 index 1c4eaec..0000000 --- a/app/channels/chat_channel.rb +++ /dev/null @@ -1,11 +0,0 @@ -class ChatChannel < ApplicationCable::Channel - def subscribed - user = params[:name] - stream_for 'public_chat' - ActionCable.server.broadcast 'public_chat', "#{user} joined!" - end - - def unsubscribed - # Any cleanup needed when channel is unsubscribed - end -end diff --git a/app/channels/social_channel.rb b/app/channels/social_channel.rb new file mode 100644 index 0000000..46d6186 --- /dev/null +++ b/app/channels/social_channel.rb @@ -0,0 +1,11 @@ +class SocialChannel < ApplicationCable::Channel + def subscribed + social = Social.first + stream_for social +# stream_from "some_channel" + end + + def unsubscribed + # Any cleanup needed when channel is unsubscribed + end +end From 22d0127392da40b3f5d598997c3018a7421035b1 Mon Sep 17 00:00:00 2001 From: Greg Flaherty Date: Wed, 25 May 2022 12:42:44 -0400 Subject: [PATCH 2/7] Feat: create social controller for creating messages --- app/controllers/api/v1/social_controller.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/social_controller.rb b/app/controllers/api/v1/social_controller.rb index bf0ff85..abae6e4 100644 --- a/app/controllers/api/v1/social_controller.rb +++ b/app/controllers/api/v1/social_controller.rb @@ -2,12 +2,21 @@ class Api::V1::SocialController < ApplicationController def index messages = Message.all - render json: messages end def create - + user = User.find_by(name: params[:name]) + channel = Social.first + message = Message.create!( + content: params[:content], + name: user.name, + social_id: channel.id, + user_id: user.id + ) + + SocialChannel.broadcast_to(channel, message) + render json: message end end From d2d768726b446af5356aaf062dbece0811527710 Mon Sep 17 00:00:00 2001 From: Greg Flaherty Date: Wed, 25 May 2022 12:43:28 -0400 Subject: [PATCH 3/7] Feat: create message model and social model --- app/models/message.rb | 2 +- app/models/social.rb | 3 +++ spec/factories/socials.rb | 5 +++++ spec/models/social_spec.rb | 5 +++++ 4 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 app/models/social.rb create mode 100644 spec/factories/socials.rb create mode 100644 spec/models/social_spec.rb diff --git a/app/models/message.rb b/app/models/message.rb index d234471..e7be94f 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -1,5 +1,5 @@ class Message < ApplicationRecord validates_presence_of :content belongs_to :user - + belongs_to :social end diff --git a/app/models/social.rb b/app/models/social.rb new file mode 100644 index 0000000..0f803fd --- /dev/null +++ b/app/models/social.rb @@ -0,0 +1,3 @@ +class Social < ApplicationRecord + has_many :messages +end diff --git a/spec/factories/socials.rb b/spec/factories/socials.rb new file mode 100644 index 0000000..c90d5c2 --- /dev/null +++ b/spec/factories/socials.rb @@ -0,0 +1,5 @@ +FactoryBot.define do + factory :social do + + end +end diff --git a/spec/models/social_spec.rb b/spec/models/social_spec.rb new file mode 100644 index 0000000..13c160d --- /dev/null +++ b/spec/models/social_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Social, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end From 44b38ff8d335f36011e301db8f8a48a57794eece Mon Sep 17 00:00:00 2001 From: Greg Flaherty Date: Wed, 25 May 2022 12:43:41 -0400 Subject: [PATCH 4/7] Feat: create messages and socials table --- db/migrate/20220524222711_create_messages.rb | 2 +- db/migrate/20220525161153_create_socials.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20220525161153_create_socials.rb diff --git a/db/migrate/20220524222711_create_messages.rb b/db/migrate/20220524222711_create_messages.rb index 4031665..ce800ed 100644 --- a/db/migrate/20220524222711_create_messages.rb +++ b/db/migrate/20220524222711_create_messages.rb @@ -3,7 +3,7 @@ def change create_table :messages do |t| t.text :content t.references :user, foreign_key: true - + t.timestamps end end diff --git a/db/migrate/20220525161153_create_socials.rb b/db/migrate/20220525161153_create_socials.rb new file mode 100644 index 0000000..51ca00e --- /dev/null +++ b/db/migrate/20220525161153_create_socials.rb @@ -0,0 +1,8 @@ +class CreateSocials < ActiveRecord::Migration[5.2] + def change + create_table :socials do |t| + + t.timestamps + end + end +end From ca60276b121e4d296eee9d0880570a1624a0055f Mon Sep 17 00:00:00 2001 From: Greg Flaherty Date: Wed, 25 May 2022 12:43:51 -0400 Subject: [PATCH 5/7] Feat: add relationship for messages and social --- db/migrate/20220525161203_add_socials_to_messages.rb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 db/migrate/20220525161203_add_socials_to_messages.rb diff --git a/db/migrate/20220525161203_add_socials_to_messages.rb b/db/migrate/20220525161203_add_socials_to_messages.rb new file mode 100644 index 0000000..5f3d586 --- /dev/null +++ b/db/migrate/20220525161203_add_socials_to_messages.rb @@ -0,0 +1,5 @@ +class AddSocialsToMessages < ActiveRecord::Migration[5.2] + def change + add_reference :messages, :social, foreign_key: true + end +end From f90f89544860d925c20363e42abd0738ae81bce6 Mon Sep 17 00:00:00 2001 From: Greg Flaherty Date: Wed, 25 May 2022 12:44:06 -0400 Subject: [PATCH 6/7] Feat: migrate new schema to include messages and social tables --- db/schema.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/db/schema.rb b/db/schema.rb index 9ac4505..8959a9c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_05_24_223708) do +ActiveRecord::Schema.define(version: 2022_05_25_161203) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -36,9 +36,16 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "name" + t.bigint "social_id" + t.index ["social_id"], name: "index_messages_on_social_id" t.index ["user_id"], name: "index_messages_on_user_id" end + create_table "socials", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "users", force: :cascade do |t| t.string "name" t.string "password" @@ -65,6 +72,7 @@ end add_foreign_key "intervals", "workout_exercises" + add_foreign_key "messages", "socials" add_foreign_key "messages", "users" add_foreign_key "workout_exercises", "exercises" add_foreign_key "workout_exercises", "workouts" From 34c54d1568e8b74e0096f7a9ff8880baf5c35ee6 Mon Sep 17 00:00:00 2001 From: Greg Flaherty Date: Wed, 25 May 2022 12:44:14 -0400 Subject: [PATCH 7/7] Feat: add social to seed data --- db/seeds.rb | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/db/seeds.rb b/db/seeds.rb index 31c8183..8a85853 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -10,10 +10,10 @@ Exercise.destroy_all Workout.destroy_all User.destroy_all +Social.destroy_all Message.destroy_all - user_1 = User.create!(name: 'user_1', password: 'password1') user_2 = User.create!(name: 'user_2', password: 'password2') user_3 = User.create!(name: 'user_3', password: 'password3') @@ -131,16 +131,19 @@ Interval.create!(reps: 12, weight_lbs: (125 + ((1 + i) * 5)), workout_exercise_id: "#{workout_exercise_31.id}") end -message_1 = user_1.messages.create(content: "message 1", name: user_1.name) -message_2 = user_2.messages.create(content: "message 2", name: user_2.name) -message_3 = user_2.messages.create(content: "message 3", name: user_2.name) -message_4 = user_3.messages.create(content: "message 4", name: user_3.name) -message_5 = user_4.messages.create(content: "message 5", name: user_4.name) -message_6 = user_5.messages.create(content: "message 6", name: user_5.name) -message_7 = user_1.messages.create(content: "message 7", name: user_1.name) -message_8 = user_4.messages.create(content: "message 8", name: user_4.name) -message_9 = user_3.messages.create(content: "message 9", name: user_3.name) -message_10 = user_2.messages.create(content: "message 10", name: user_2.name) +social = Social.create! + + +message_1 = user_1.messages.create(content: "message 1", name: user_1.name, social_id: social.id) +message_2 = user_2.messages.create(content: "message 2", name: user_2.name, social_id: social.id) +message_3 = user_2.messages.create(content: "message 3", name: user_2.name, social_id: social.id) +message_4 = user_3.messages.create(content: "message 4", name: user_3.name, social_id: social.id) +message_5 = user_4.messages.create(content: "message 5", name: user_4.name, social_id: social.id) +message_6 = user_5.messages.create(content: "message 6", name: user_5.name, social_id: social.id) +message_7 = user_1.messages.create(content: "message 7", name: user_1.name, social_id: social.id) +message_8 = user_4.messages.create(content: "message 8", name: user_4.name, social_id: social.id) +message_9 = user_3.messages.create(content: "message 9", name: user_3.name, social_id: social.id) +message_10 = user_2.messages.create(content: "message 10", name: user_2.name, social_id: social.id) # arms ^^ # set_13 = Interval.create!(reps: 12, weight_lbs: 135.0, workout_exercise_id: "#{workout_exercise_11.id}")