Add table and seat models

This commit is contained in:
Jesse C. Fisher 2015-11-25 02:15:05 -08:00
parent a4fb8e6613
commit 0de80ca307
12 changed files with 124 additions and 1 deletions

View File

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

4
app/models/seat.rb Normal file
View File

@ -0,0 +1,4 @@
class Seat < ActiveRecord::Base
belongs_to :table
belongs_to :user
end

5
app/models/table.rb Normal file
View File

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

View File

@ -0,0 +1,9 @@
class CreateTables < ActiveRecord::Migration
def change
create_table :tables do |t|
t.string :title
t.timestamps
end
end
end

View File

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

View File

@ -0,0 +1,5 @@
class AddCurrentGameIdToTable < ActiveRecord::Migration
def change
add_column :tables, :current_game_id, :integer
end
end

View File

@ -0,0 +1,5 @@
class AddTableIdToGames < ActiveRecord::Migration
def change
add_column :games, :table_id, :integer
end
end

View File

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

7
spec/factories/seats.rb Normal file
View File

@ -0,0 +1,7 @@
FactoryGirl.define do
factory :seat do
table { FactoryGirl.create(:table) }
user { FactoryGirl.create(:user) }
end
end

7
spec/factories/tables.rb Normal file
View File

@ -0,0 +1,7 @@
FactoryGirl.define do
factory :table do
title "Test Table"
current_game { FactoryGirl.create(:game) }
end
end

21
spec/models/seat_spec.rb Normal file
View File

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

31
spec/models/table_spec.rb Normal file
View File

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