From f9cbb7c680dd8b27d05b5911a99988dfa6cf74c7 Mon Sep 17 00:00:00 2001 From: Beatrice Olivera Date: Mon, 27 Aug 2018 16:55:44 +0100 Subject: [PATCH] created channel for action cable and friendship relationship in database --- Gemfile.lock | 2 +- app/channels/video_session_channel.rb | 11 +++++++++++ app/models/connection.rb | 14 ++++++++++++++ app/models/user.rb | 8 ++++++++ db/migrate/20180827151325_create_connections.rb | 8 ++++++++ db/schema.rb | 11 ++++++++++- test/models/contact_test.rb | 7 +++++++ 7 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 app/channels/video_session_channel.rb create mode 100644 app/models/connection.rb create mode 100644 db/migrate/20180827151325_create_connections.rb create mode 100644 test/models/contact_test.rb diff --git a/Gemfile.lock b/Gemfile.lock index d29c8fb..1afadce 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -225,7 +225,7 @@ DEPENDENCIES webpacker RUBY VERSION - ruby 2.3.0p0 + ruby 2.4.4p296 BUNDLED WITH 1.16.2 diff --git a/app/channels/video_session_channel.rb b/app/channels/video_session_channel.rb new file mode 100644 index 0000000..174da47 --- /dev/null +++ b/app/channels/video_session_channel.rb @@ -0,0 +1,11 @@ +class VideoSessionChannel < ApplicationCable::Channel + def subscribed + # video session + # stream_from "chat_room_#{params[:chat_room_id]}" + end + + + def unsubscribed + # Any cleanup needed when channel is unsubscribed + end +end diff --git a/app/models/connection.rb b/app/models/connection.rb new file mode 100644 index 0000000..0028286 --- /dev/null +++ b/app/models/connection.rb @@ -0,0 +1,14 @@ +class Connection < ApplicationRecord + belongs_to :user + belongs_to :contact, class_name: 'User', foreign_key: 'contact_id' + + after_create :create_inverted_connection + + private + + def create_inverted_connection + unless Connection.where('user_id = ? and contact_id = ?', self.contact.id, self.user.id).length > 0 + Connection.create!(user: self.contact, contact: self.user) + end + end +end diff --git a/app/models/user.rb b/app/models/user.rb index 4756799..dfa148e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,4 +3,12 @@ class User < ApplicationRecord # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable + + has_many :connections + + def contacts + self.connections.map do |connection| + connection.contact + end + end end diff --git a/db/migrate/20180827151325_create_connections.rb b/db/migrate/20180827151325_create_connections.rb new file mode 100644 index 0000000..8d24a1c --- /dev/null +++ b/db/migrate/20180827151325_create_connections.rb @@ -0,0 +1,8 @@ +class CreateConnections < ActiveRecord::Migration[5.2] + def change + create_table :connections do |t| + t.references :user, foreign_key: true, index: true + t.references :contact, foreign_key: { to_table: :users }, index: true + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 7ed42c6..cc819c1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,11 +10,18 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2018_08_27_143700) do +ActiveRecord::Schema.define(version: 2018_08_27_151325) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "connections", force: :cascade do |t| + t.bigint "user_id" + t.bigint "contact_id" + t.index ["contact_id"], name: "index_connections_on_contact_id" + t.index ["user_id"], name: "index_connections_on_user_id" + end + create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false @@ -27,4 +34,6 @@ ActiveRecord::Schema.define(version: 2018_08_27_143700) do t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end + add_foreign_key "connections", "users" + add_foreign_key "connections", "users", column: "contact_id" end diff --git a/test/models/contact_test.rb b/test/models/contact_test.rb new file mode 100644 index 0000000..ccef1f9 --- /dev/null +++ b/test/models/contact_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ContactTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end