From 97aee2c10f5bac756ba4c67b6390e85cb7e7afc4 Mon Sep 17 00:00:00 2001 From: Beatrice Olivera Date: Wed, 29 Aug 2018 11:39:51 +0100 Subject: [PATCH 1/4] added room id to connections table --- app/models/connection.rb | 10 +++++++++- db/migrate/20180829102007_add_column_to_connections.rb | 5 +++++ db/schema.rb | 3 ++- 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20180829102007_add_column_to_connections.rb diff --git a/app/models/connection.rb b/app/models/connection.rb index 0028286..521a5f7 100644 --- a/app/models/connection.rb +++ b/app/models/connection.rb @@ -2,13 +2,21 @@ class Connection < ApplicationRecord belongs_to :user belongs_to :contact, class_name: 'User', foreign_key: 'contact_id' + after_create :set_room_id after_create :create_inverted_connection private + def set_room_id + self.room_id = "#{self.user.email}+#{self.contact.email}" + self.save! + end + 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) + inverted = Connection.create!(user: self.contact, contact: self.user) + inverted.room_id = "#{self.user.email}+#{self.contact.email}" + inverted.save! end end end diff --git a/db/migrate/20180829102007_add_column_to_connections.rb b/db/migrate/20180829102007_add_column_to_connections.rb new file mode 100644 index 0000000..080b162 --- /dev/null +++ b/db/migrate/20180829102007_add_column_to_connections.rb @@ -0,0 +1,5 @@ +class AddColumnToConnections < ActiveRecord::Migration[5.2] + def change + add_column :connections, :room_id, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index cc819c1..9b9880d 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: 2018_08_27_151325) do +ActiveRecord::Schema.define(version: 2018_08_29_102007) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -18,6 +18,7 @@ ActiveRecord::Schema.define(version: 2018_08_27_151325) do create_table "connections", force: :cascade do |t| t.bigint "user_id" t.bigint "contact_id" + t.string "room_id" t.index ["contact_id"], name: "index_connections_on_contact_id" t.index ["user_id"], name: "index_connections_on_user_id" end From 82079d71cdb95427fbdede9482de846ae2c6fabc Mon Sep 17 00:00:00 2001 From: Beatrice Olivera Date: Wed, 29 Aug 2018 13:15:37 +0100 Subject: [PATCH 2/4] removed room id from connections table --- app/models/connection.rb | 8 -------- db/migrate/20180829102007_add_column_to_connections.rb | 5 ----- db/schema.rb | 3 +-- 3 files changed, 1 insertion(+), 15 deletions(-) delete mode 100644 db/migrate/20180829102007_add_column_to_connections.rb diff --git a/app/models/connection.rb b/app/models/connection.rb index 521a5f7..f66456a 100644 --- a/app/models/connection.rb +++ b/app/models/connection.rb @@ -2,21 +2,13 @@ class Connection < ApplicationRecord belongs_to :user belongs_to :contact, class_name: 'User', foreign_key: 'contact_id' - after_create :set_room_id after_create :create_inverted_connection private - def set_room_id - self.room_id = "#{self.user.email}+#{self.contact.email}" - self.save! - end - def create_inverted_connection unless Connection.where('user_id = ? and contact_id = ?', self.contact.id, self.user.id).length > 0 inverted = Connection.create!(user: self.contact, contact: self.user) - inverted.room_id = "#{self.user.email}+#{self.contact.email}" - inverted.save! end end end diff --git a/db/migrate/20180829102007_add_column_to_connections.rb b/db/migrate/20180829102007_add_column_to_connections.rb deleted file mode 100644 index 080b162..0000000 --- a/db/migrate/20180829102007_add_column_to_connections.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddColumnToConnections < ActiveRecord::Migration[5.2] - def change - add_column :connections, :room_id, :string - end -end diff --git a/db/schema.rb b/db/schema.rb index 9b9880d..cc819c1 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: 2018_08_29_102007) 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" @@ -18,7 +18,6 @@ ActiveRecord::Schema.define(version: 2018_08_29_102007) do create_table "connections", force: :cascade do |t| t.bigint "user_id" t.bigint "contact_id" - t.string "room_id" t.index ["contact_id"], name: "index_connections_on_contact_id" t.index ["user_id"], name: "index_connections_on_user_id" end From 085e402c4dbf49b62775ba9c06ba08e5ea15991d Mon Sep 17 00:00:00 2001 From: Beatrice Olivera Date: Wed, 29 Aug 2018 13:44:10 +0100 Subject: [PATCH 3/4] created models for chatroom --- app/models/chat_room.rb | 5 ++++ app/models/chat_room_participation.rb | 4 +++ app/models/request.rb | 4 +++ app/models/user.rb | 3 ++ .../20180829122818_create_chat_rooms.rb | 8 +++++ ...9122930_create_chat_room_participations.rb | 10 +++++++ db/migrate/20180829123049_create_requests.rb | 11 +++++++ ...0180829123204_change_column_in_requests.rb | 5 ++++ db/schema.rb | 30 ++++++++++++++++++- test/models/chat_room_participation_test.rb | 7 +++++ test/models/chat_room_test.rb | 7 +++++ test/models/request_test.rb | 7 +++++ 12 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 app/models/chat_room.rb create mode 100644 app/models/chat_room_participation.rb create mode 100644 app/models/request.rb create mode 100644 db/migrate/20180829122818_create_chat_rooms.rb create mode 100644 db/migrate/20180829122930_create_chat_room_participations.rb create mode 100644 db/migrate/20180829123049_create_requests.rb create mode 100644 db/migrate/20180829123204_change_column_in_requests.rb create mode 100644 test/models/chat_room_participation_test.rb create mode 100644 test/models/chat_room_test.rb create mode 100644 test/models/request_test.rb diff --git a/app/models/chat_room.rb b/app/models/chat_room.rb new file mode 100644 index 0000000..078aced --- /dev/null +++ b/app/models/chat_room.rb @@ -0,0 +1,5 @@ +class ChatRoom < ApplicationRecord + has_many :users, through: :chat_room_participations + has_many :chat_room_participations + has_many :requests +end diff --git a/app/models/chat_room_participation.rb b/app/models/chat_room_participation.rb new file mode 100644 index 0000000..86931ec --- /dev/null +++ b/app/models/chat_room_participation.rb @@ -0,0 +1,4 @@ +class ChatRoomParticipation < ApplicationRecord + belongs_to :user + belongs_to :chat_room +end diff --git a/app/models/request.rb b/app/models/request.rb new file mode 100644 index 0000000..2857ced --- /dev/null +++ b/app/models/request.rb @@ -0,0 +1,4 @@ +class Request < ApplicationRecord + belongs_to :user + belongs_to :chat_room +end diff --git a/app/models/user.rb b/app/models/user.rb index dfa148e..a084ec6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,6 +5,9 @@ class User < ApplicationRecord :recoverable, :rememberable, :validatable has_many :connections + has_many :chat_rooms, through: :chat_room_participations + has_many :chat_room_participations + has_many :requests def contacts self.connections.map do |connection| diff --git a/db/migrate/20180829122818_create_chat_rooms.rb b/db/migrate/20180829122818_create_chat_rooms.rb new file mode 100644 index 0000000..a142563 --- /dev/null +++ b/db/migrate/20180829122818_create_chat_rooms.rb @@ -0,0 +1,8 @@ +class CreateChatRooms < ActiveRecord::Migration[5.2] + def change + create_table :chat_rooms do |t| + + t.timestamps + end + end +end diff --git a/db/migrate/20180829122930_create_chat_room_participations.rb b/db/migrate/20180829122930_create_chat_room_participations.rb new file mode 100644 index 0000000..34739cc --- /dev/null +++ b/db/migrate/20180829122930_create_chat_room_participations.rb @@ -0,0 +1,10 @@ +class CreateChatRoomParticipations < ActiveRecord::Migration[5.2] + def change + create_table :chat_room_participations do |t| + t.references :user, foreign_key: true + t.references :chat_room, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/migrate/20180829123049_create_requests.rb b/db/migrate/20180829123049_create_requests.rb new file mode 100644 index 0000000..2417469 --- /dev/null +++ b/db/migrate/20180829123049_create_requests.rb @@ -0,0 +1,11 @@ +class CreateRequests < ActiveRecord::Migration[5.2] + def change + create_table :requests do |t| + t.references :user, foreign_key: true + t.references :chat_room, foreign_key: true + t.boolean :accepted + + t.timestamps + end + end +end diff --git a/db/migrate/20180829123204_change_column_in_requests.rb b/db/migrate/20180829123204_change_column_in_requests.rb new file mode 100644 index 0000000..87166c7 --- /dev/null +++ b/db/migrate/20180829123204_change_column_in_requests.rb @@ -0,0 +1,5 @@ +class ChangeColumnInRequests < ActiveRecord::Migration[5.2] + def change + change_column :requests, :accepted, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index cc819c1..e915a1a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,11 +10,25 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2018_08_27_151325) do +ActiveRecord::Schema.define(version: 2018_08_29_123204) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "chat_room_participations", force: :cascade do |t| + t.bigint "user_id" + t.bigint "chat_room_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["chat_room_id"], name: "index_chat_room_participations_on_chat_room_id" + t.index ["user_id"], name: "index_chat_room_participations_on_user_id" + end + + create_table "chat_rooms", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "connections", force: :cascade do |t| t.bigint "user_id" t.bigint "contact_id" @@ -22,6 +36,16 @@ ActiveRecord::Schema.define(version: 2018_08_27_151325) do t.index ["user_id"], name: "index_connections_on_user_id" end + create_table "requests", force: :cascade do |t| + t.bigint "user_id" + t.bigint "chat_room_id" + t.boolean "accepted", default: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["chat_room_id"], name: "index_requests_on_chat_room_id" + t.index ["user_id"], name: "index_requests_on_user_id" + end + create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false @@ -34,6 +58,10 @@ ActiveRecord::Schema.define(version: 2018_08_27_151325) do t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end + add_foreign_key "chat_room_participations", "chat_rooms" + add_foreign_key "chat_room_participations", "users" add_foreign_key "connections", "users" add_foreign_key "connections", "users", column: "contact_id" + add_foreign_key "requests", "chat_rooms" + add_foreign_key "requests", "users" end diff --git a/test/models/chat_room_participation_test.rb b/test/models/chat_room_participation_test.rb new file mode 100644 index 0000000..fd70bc7 --- /dev/null +++ b/test/models/chat_room_participation_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ChatRoomParticipationTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/chat_room_test.rb b/test/models/chat_room_test.rb new file mode 100644 index 0000000..c621386 --- /dev/null +++ b/test/models/chat_room_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ChatRoomTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/request_test.rb b/test/models/request_test.rb new file mode 100644 index 0000000..f9f95ba --- /dev/null +++ b/test/models/request_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class RequestTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end From 68d55cbcd8477ee4191102b9abfa4a6e1481732e Mon Sep 17 00:00:00 2001 From: Beatrice Olivera Date: Wed, 29 Aug 2018 14:09:12 +0100 Subject: [PATCH 4/4] added chat room controllers --- app/controllers/chat_rooms_controller.rb | 6 ++++++ app/controllers/requests_controller.rb | 8 ++++++++ config/environments/production.rb | 2 +- test/controllers/chat_rooms_controller_test.rb | 7 +++++++ test/controllers/requests_controller_test.rb | 7 +++++++ 5 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 app/controllers/chat_rooms_controller.rb create mode 100644 app/controllers/requests_controller.rb create mode 100644 test/controllers/chat_rooms_controller_test.rb create mode 100644 test/controllers/requests_controller_test.rb diff --git a/app/controllers/chat_rooms_controller.rb b/app/controllers/chat_rooms_controller.rb new file mode 100644 index 0000000..f011b21 --- /dev/null +++ b/app/controllers/chat_rooms_controller.rb @@ -0,0 +1,6 @@ +class ChatRoomsController < ApplicationController + + def create + + end +end diff --git a/app/controllers/requests_controller.rb b/app/controllers/requests_controller.rb new file mode 100644 index 0000000..d781786 --- /dev/null +++ b/app/controllers/requests_controller.rb @@ -0,0 +1,8 @@ +class RequestsController < ApplicationController + + def update + request.accepted = true + + # Create new Chat Room + end +end diff --git a/config/environments/production.rb b/config/environments/production.rb index c90a719..255da15 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -50,7 +50,7 @@ Rails.application.configure do config.action_cable.allowed_request_origins = [ '*' ] # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. - # config.force_ssl = true + config.force_ssl = true # Use the lowest log level to ensure availability of diagnostic information # when problems arise. diff --git a/test/controllers/chat_rooms_controller_test.rb b/test/controllers/chat_rooms_controller_test.rb new file mode 100644 index 0000000..5f545e2 --- /dev/null +++ b/test/controllers/chat_rooms_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ChatRoomsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/controllers/requests_controller_test.rb b/test/controllers/requests_controller_test.rb new file mode 100644 index 0000000..309c8e5 --- /dev/null +++ b/test/controllers/requests_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class RequestsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end