created channel for action cable and friendship relationship in database

This commit is contained in:
Beatrice Olivera 2018-08-27 16:55:44 +01:00
parent 7d67f04c63
commit f9cbb7c680
7 changed files with 59 additions and 2 deletions

View File

@ -225,7 +225,7 @@ DEPENDENCIES
webpacker webpacker
RUBY VERSION RUBY VERSION
ruby 2.3.0p0 ruby 2.4.4p296
BUNDLED WITH BUNDLED WITH
1.16.2 1.16.2

View File

@ -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

14
app/models/connection.rb Normal file
View File

@ -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

View File

@ -3,4 +3,12 @@ class User < ApplicationRecord
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable, devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable :recoverable, :rememberable, :validatable
has_many :connections
def contacts
self.connections.map do |connection|
connection.contact
end
end
end end

View File

@ -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

View File

@ -10,11 +10,18 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" 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| create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false t.string "email", default: "", null: false
t.string "encrypted_password", 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 t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end end
add_foreign_key "connections", "users"
add_foreign_key "connections", "users", column: "contact_id"
end end

View File

@ -0,0 +1,7 @@
require 'test_helper'
class ContactTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end