Add table and seat models
This commit is contained in:
parent
a4fb8e6613
commit
0de80ca307
@ -1,5 +1,6 @@
|
|||||||
# The game class
|
# The game class
|
||||||
class Game < ActiveRecord::Base
|
class Game < ActiveRecord::Base
|
||||||
|
belongs_to :table
|
||||||
has_many :players, dependent: :destroy
|
has_many :players, dependent: :destroy
|
||||||
has_many :users, through: :players
|
has_many :users, through: :players
|
||||||
has_many :player_cards, through: :players
|
has_many :player_cards, through: :players
|
||||||
|
|||||||
4
app/models/seat.rb
Normal file
4
app/models/seat.rb
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
class Seat < ActiveRecord::Base
|
||||||
|
belongs_to :table
|
||||||
|
belongs_to :user
|
||||||
|
end
|
||||||
5
app/models/table.rb
Normal file
5
app/models/table.rb
Normal 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
|
||||||
9
db/migrate/20151125090607_create_tables.rb
Normal file
9
db/migrate/20151125090607_create_tables.rb
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
class CreateTables < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
create_table :tables do |t|
|
||||||
|
t.string :title
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
10
db/migrate/20151125092802_create_seats.rb
Normal file
10
db/migrate/20151125092802_create_seats.rb
Normal 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
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
class AddCurrentGameIdToTable < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
add_column :tables, :current_game_id, :integer
|
||||||
|
end
|
||||||
|
end
|
||||||
5
db/migrate/20151125100745_add_table_id_to_games.rb
Normal file
5
db/migrate/20151125100745_add_table_id_to_games.rb
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
class AddTableIdToGames < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
add_column :games, :table_id, :integer
|
||||||
|
end
|
||||||
|
end
|
||||||
20
db/schema.rb
20
db/schema.rb
@ -11,7 +11,7 @@
|
|||||||
#
|
#
|
||||||
# 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: 20151108232044) do
|
ActiveRecord::Schema.define(version: 20151125100745) 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"
|
||||||
@ -24,6 +24,7 @@ ActiveRecord::Schema.define(version: 20151108232044) do
|
|||||||
t.integer "controlling_player_id"
|
t.integer "controlling_player_id"
|
||||||
t.integer "play_to_beat_id"
|
t.integer "play_to_beat_id"
|
||||||
t.integer "winner_player_id"
|
t.integer "winner_player_id"
|
||||||
|
t.integer "table_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
add_index "games", ["play_to_beat_id"], name: "index_games_on_play_to_beat_id", using: :btree
|
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", ["game_id"], name: "index_plays_on_game_id", using: :btree
|
||||||
add_index "plays", ["player_id"], name: "index_plays_on_player_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|
|
create_table "users", force: true do |t|
|
||||||
t.string "email", default: "", null: false
|
t.string "email", default: "", null: false
|
||||||
t.string "encrypted_password", default: ""
|
t.string "encrypted_password", default: ""
|
||||||
|
|||||||
7
spec/factories/seats.rb
Normal file
7
spec/factories/seats.rb
Normal 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
7
spec/factories/tables.rb
Normal 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
21
spec/models/seat_spec.rb
Normal 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
31
spec/models/table_spec.rb
Normal 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
|
||||||
Loading…
x
Reference in New Issue
Block a user