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

Action cable gf #28

Merged
merged 7 commits into from
May 25, 2022
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
11 changes: 0 additions & 11 deletions app/channels/chat_channel.rb

This file was deleted.

11 changes: 11 additions & 0 deletions app/channels/social_channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class SocialChannel < ApplicationCable::Channel
def subscribed
Copy link
Collaborator

Choose a reason for hiding this comment

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

Fantastic method in going about the group social channel.

social = Social.first
stream_for social
# stream_from "some_channel"
end

def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
13 changes: 11 additions & 2 deletions app/controllers/api/v1/social_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Copy link
Collaborator

Choose a reason for hiding this comment

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

I like how you broke this down for readability.

content: params[:content],
name: user.name,
social_id: channel.id,
user_id: user.id
)

SocialChannel.broadcast_to(channel, message)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Great formatting of the broadcasting the desired information.

render json: message
end

end
2 changes: 1 addition & 1 deletion app/models/message.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Message < ApplicationRecord
validates_presence_of :content
belongs_to :user

belongs_to :social
end
3 changes: 3 additions & 0 deletions app/models/social.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Social < ApplicationRecord
has_many :messages
end
2 changes: 1 addition & 1 deletion db/migrate/20220524222711_create_messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def change
create_table :messages do |t|
t.text :content
t.references :user, foreign_key: true

t.timestamps
end
end
Expand Down
8 changes: 8 additions & 0 deletions db/migrate/20220525161153_create_socials.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateSocials < ActiveRecord::Migration[5.2]
def change
create_table :socials do |t|

t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20220525161203_add_socials_to_messages.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddSocialsToMessages < ActiveRecord::Migration[5.2]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hopefully this is all we need to get WebSockets live!

def change
add_reference :messages, :social, foreign_key: true
end
end
10 changes: 9 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down
25 changes: 14 additions & 11 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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}")
Expand Down
5 changes: 5 additions & 0 deletions spec/factories/socials.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :social do

end
end
5 changes: 5 additions & 0 deletions spec/models/social_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Social, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end