From 0de80ca307b47e05f708dad54c4104e473c79a20 Mon Sep 17 00:00:00 2001 From: "Jesse C. Fisher" Date: Wed, 25 Nov 2015 02:15:05 -0800 Subject: [PATCH] Add table and seat models --- app/models/game.rb | 1 + app/models/seat.rb | 4 +++ app/models/table.rb | 5 +++ db/migrate/20151125090607_create_tables.rb | 9 ++++++ db/migrate/20151125092802_create_seats.rb | 10 ++++++ ...1125095642_add_current_game_id_to_table.rb | 5 +++ .../20151125100745_add_table_id_to_games.rb | 5 +++ db/schema.rb | 20 +++++++++++- spec/factories/seats.rb | 7 +++++ spec/factories/tables.rb | 7 +++++ spec/models/seat_spec.rb | 21 +++++++++++++ spec/models/table_spec.rb | 31 +++++++++++++++++++ 12 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 app/models/seat.rb create mode 100644 app/models/table.rb create mode 100644 db/migrate/20151125090607_create_tables.rb create mode 100644 db/migrate/20151125092802_create_seats.rb create mode 100644 db/migrate/20151125095642_add_current_game_id_to_table.rb create mode 100644 db/migrate/20151125100745_add_table_id_to_games.rb create mode 100644 spec/factories/seats.rb create mode 100644 spec/factories/tables.rb create mode 100644 spec/models/seat_spec.rb create mode 100644 spec/models/table_spec.rb diff --git a/app/models/game.rb b/app/models/game.rb index a99664c0..c534d1dd 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -1,5 +1,6 @@ # The game class class Game < ActiveRecord::Base + belongs_to :table has_many :players, dependent: :destroy has_many :users, through: :players has_many :player_cards, through: :players diff --git a/app/models/seat.rb b/app/models/seat.rb new file mode 100644 index 00000000..dc8427c7 --- /dev/null +++ b/app/models/seat.rb @@ -0,0 +1,4 @@ +class Seat < ActiveRecord::Base + belongs_to :table + belongs_to :user +end diff --git a/app/models/table.rb b/app/models/table.rb new file mode 100644 index 00000000..2933c983 --- /dev/null +++ b/app/models/table.rb @@ -0,0 +1,5 @@ +class Table < ActiveRecord::Base + has_many :seats + has_many :games + has_one :current_game, :class_name => "Game", :primary_key => "current_game_id" +end diff --git a/db/migrate/20151125090607_create_tables.rb b/db/migrate/20151125090607_create_tables.rb new file mode 100644 index 00000000..daef9021 --- /dev/null +++ b/db/migrate/20151125090607_create_tables.rb @@ -0,0 +1,9 @@ +class CreateTables < ActiveRecord::Migration + def change + create_table :tables do |t| + t.string :title + + t.timestamps + end + end +end diff --git a/db/migrate/20151125092802_create_seats.rb b/db/migrate/20151125092802_create_seats.rb new file mode 100644 index 00000000..d96bd59d --- /dev/null +++ b/db/migrate/20151125092802_create_seats.rb @@ -0,0 +1,10 @@ +class CreateSeats < ActiveRecord::Migration + def change + create_table :seats do |t| + t.references :table, index: true + t.references :user, index: true + + t.timestamps + end + end +end diff --git a/db/migrate/20151125095642_add_current_game_id_to_table.rb b/db/migrate/20151125095642_add_current_game_id_to_table.rb new file mode 100644 index 00000000..435a412e --- /dev/null +++ b/db/migrate/20151125095642_add_current_game_id_to_table.rb @@ -0,0 +1,5 @@ +class AddCurrentGameIdToTable < ActiveRecord::Migration + def change + add_column :tables, :current_game_id, :integer + end +end diff --git a/db/migrate/20151125100745_add_table_id_to_games.rb b/db/migrate/20151125100745_add_table_id_to_games.rb new file mode 100644 index 00000000..4873d95d --- /dev/null +++ b/db/migrate/20151125100745_add_table_id_to_games.rb @@ -0,0 +1,5 @@ +class AddTableIdToGames < ActiveRecord::Migration + def change + add_column :games, :table_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 9ae5c11f..9e1cce56 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20151108232044) do +ActiveRecord::Schema.define(version: 20151125100745) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -24,6 +24,7 @@ ActiveRecord::Schema.define(version: 20151108232044) do t.integer "controlling_player_id" t.integer "play_to_beat_id" t.integer "winner_player_id" + t.integer "table_id" end add_index "games", ["play_to_beat_id"], name: "index_games_on_play_to_beat_id", using: :btree @@ -69,6 +70,23 @@ ActiveRecord::Schema.define(version: 20151108232044) do add_index "plays", ["game_id"], name: "index_plays_on_game_id", using: :btree add_index "plays", ["player_id"], name: "index_plays_on_player_id", using: :btree + create_table "seats", force: true do |t| + t.integer "table_id" + t.integer "user_id" + t.datetime "created_at" + t.datetime "updated_at" + end + + add_index "seats", ["table_id"], name: "index_seats_on_table_id", using: :btree + add_index "seats", ["user_id"], name: "index_seats_on_user_id", using: :btree + + create_table "tables", force: true do |t| + t.string "title" + t.datetime "created_at" + t.datetime "updated_at" + t.integer "current_game_id" + end + create_table "users", force: true do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "" diff --git a/spec/factories/seats.rb b/spec/factories/seats.rb new file mode 100644 index 00000000..8dace51f --- /dev/null +++ b/spec/factories/seats.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :seat do + table { FactoryGirl.create(:table) } + user { FactoryGirl.create(:user) } + end + +end diff --git a/spec/factories/tables.rb b/spec/factories/tables.rb new file mode 100644 index 00000000..1a757675 --- /dev/null +++ b/spec/factories/tables.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :table do + title "Test Table" + current_game { FactoryGirl.create(:game) } + end + +end diff --git a/spec/models/seat_spec.rb b/spec/models/seat_spec.rb new file mode 100644 index 00000000..e9b20e78 --- /dev/null +++ b/spec/models/seat_spec.rb @@ -0,0 +1,21 @@ +require 'rails_helper' + +RSpec.describe Seat, type: :model do + before :all do + @seat = FactoryGirl.create(:seat) + end + + subject { @seat } + + it { should respond_to :table } + it { should respond_to :user } + + it '#table returns a Table' do + expect(@seat.table.class).to match Table + end + + it '#user returns a User' do + expect(@seat.user.class).to match User + end + +end diff --git a/spec/models/table_spec.rb b/spec/models/table_spec.rb new file mode 100644 index 00000000..7cdfc128 --- /dev/null +++ b/spec/models/table_spec.rb @@ -0,0 +1,31 @@ +require 'rails_helper' + +RSpec.describe Table, type: :model do + before :all do + @table = FactoryGirl.create(:table) + end + + subject { @table } + + it { should respond_to :title } + it { should respond_to :seats } + it { should respond_to :games } + it { should respond_to :current_game } + + it '#title returns a string' do + expect(@table.title).to match 'Test Table' + end + + it '#seats returns Seats collection' do + expect(@table.seats.class).to match Seat::ActiveRecord_Associations_CollectionProxy + end + + it '#games returns Games collection' do + expect(@table.games.class).to match Game::ActiveRecord_Associations_CollectionProxy + end + + it '#current_game returns a Game' do + expect(@table.current_game.class).to match Game + end + +end