Skip to content

Commit

Permalink
- Introduct ActiveModel::Serializers
Browse files Browse the repository at this point in the history
- Add modeling for:
  - LeaguePlayers
  - Players
  - Teams
  - Roseter Slots
- Create a RosterManager that should end up responsible for all roster
  changes
  • Loading branch information
gmassanek committed Jun 28, 2015
1 parent 04413de commit 3f80aa4
Show file tree
Hide file tree
Showing 34 changed files with 398 additions and 48 deletions.
1 change: 1 addition & 0 deletions Gemfile
@@ -1,5 +1,6 @@
source "https://rubygems.org"

gem "active_model_serializers", "~> 0.9.3"
gem "jbuilder", "~> 2.0"
gem "pg"
gem "puma", "~> 2.11.3"
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Expand Up @@ -20,6 +20,8 @@ GEM
erubis (~> 2.7.0)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.1)
active_model_serializers (0.9.3)
activemodel (>= 3.2)
activejob (4.2.2)
activesupport (= 4.2.2)
globalid (>= 0.3.0)
Expand Down Expand Up @@ -224,6 +226,7 @@ PLATFORMS
ruby

DEPENDENCIES
active_model_serializers (~> 0.9.3)
bundler-audit
byebug
capistrano-rails
Expand Down
15 changes: 15 additions & 0 deletions app/controllers/api/v1/teams_controller.rb
@@ -0,0 +1,15 @@
module Api
module V1
class TeamsController < ApplicationController
def show
team = Team.includes({ roster_slots: [:player, :league_position] }).find_by({ id: params[:id] })

if team
render({ json: team })
else
head :not_found
end
end
end
end
end
25 changes: 25 additions & 0 deletions app/models/league.rb
@@ -1,7 +1,9 @@
class League < ActiveRecord::Base
belongs_to :league_template
has_many :players, { class_name: LeaguePlayer }
has_many :point_categories, { class_name: LeaguePointCategory }
has_many :positions, { class_name: LeaguePosition }
has_many :teams

def create_point_categories_from_league_template!
return unless league_template
Expand Down Expand Up @@ -31,4 +33,27 @@ def create_positions_from_league_template!
})
end
end

def create_players_from_league_template!
return unless league_template

league_template.players.each do |player|
next if players.find_by({ player_id: player.id })

players.create!({
player: player,
league_position: positions.find_by({ title: player.position.title }),
first_name: player.first_name,
last_name: player.last_name
})
end
end

def all_positions
all_positions = []
positions.each do |position|
position.count.times { all_positions << position }
end
all_positions
end
end
5 changes: 5 additions & 0 deletions app/models/league_player.rb
@@ -0,0 +1,5 @@
class LeaguePlayer < ActiveRecord::Base
belongs_to :league
belongs_to :league_position
belongs_to :player
end
1 change: 1 addition & 0 deletions app/models/league_template.rb
@@ -1,5 +1,6 @@
class LeagueTemplate < ActiveRecord::Base
has_many :leagues
has_many :players
has_many :point_categories
has_many :positions
end
4 changes: 4 additions & 0 deletions app/models/player.rb
@@ -0,0 +1,4 @@
class Player < ActiveRecord::Base
belongs_to :league_template
belongs_to :position
end
7 changes: 7 additions & 0 deletions app/models/roster_slot.rb
@@ -0,0 +1,7 @@
class RosterSlot < ActiveRecord::Base
belongs_to :team
belongs_to :league_position
belongs_to :player

enum({ status: [:active, :inactive] })
end
4 changes: 4 additions & 0 deletions app/models/team.rb
@@ -0,0 +1,4 @@
class Team < ActiveRecord::Base
belongs_to :league
has_many :roster_slots
end
3 changes: 3 additions & 0 deletions app/serializers/league_position_serializer.rb
@@ -0,0 +1,3 @@
class LeaguePositionSerializer < ActiveModel::Serializer
attributes :id, :title
end
3 changes: 3 additions & 0 deletions app/serializers/player_serializer.rb
@@ -0,0 +1,3 @@
class PlayerSerializer < ActiveModel::Serializer
attributes :id, :first_name, :last_name
end
6 changes: 6 additions & 0 deletions app/serializers/roster_slot_serializer.rb
@@ -0,0 +1,6 @@
class RosterSlotSerializer < ActiveModel::Serializer
attributes :id, :status

has_one :player
has_one :league_position
end
5 changes: 5 additions & 0 deletions app/serializers/team_serializer.rb
@@ -0,0 +1,5 @@
class TeamSerializer < ActiveModel::Serializer
attributes :id, :title

has_many :roster_slots
end
5 changes: 5 additions & 0 deletions config/initializers/active_model_serializer.rb
@@ -0,0 +1,5 @@
ActiveModel::Serializer.setup do |config|
config.embed = :ids
config.embed_in_root = true
config.include = true
end
2 changes: 2 additions & 0 deletions config/routes.rb
Expand Up @@ -7,6 +7,8 @@
resources :point_categories, { only: [:index] }
resources :positions, { only: [:index] }
end

resources :teams, { only: [:show] }
end
end
# The priority is based upon order of creation: first created -> highest priority.
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20150628033433_create_teams.rb
@@ -0,0 +1,10 @@
class CreateTeams < ActiveRecord::Migration
def change
create_table :teams do |t|
t.belongs_to :league, index: true, foreign_key: true
t.string :title

t.timestamps null: false
end
end
end
12 changes: 12 additions & 0 deletions db/migrate/20150628034437_create_players.rb
@@ -0,0 +1,12 @@
class CreatePlayers < ActiveRecord::Migration
def change
create_table :players do |t|
t.string :first_name
t.string :last_name
t.belongs_to :league_template, index: true, foreign_key: true
t.belongs_to :position, index: true, foreign_key: true

t.timestamps null: false
end
end
end
13 changes: 13 additions & 0 deletions db/migrate/20150628034519_create_league_players.rb
@@ -0,0 +1,13 @@
class CreateLeaguePlayers < ActiveRecord::Migration
def change
create_table :league_players do |t|
t.string :first_name
t.string :last_name
t.belongs_to :league, index: true, foreign_key: true
t.belongs_to :league_position, index: true, foreign_key: true
t.belongs_to :player, index: true, foreign_key: true

t.timestamps null: false
end
end
end
14 changes: 14 additions & 0 deletions db/migrate/20150628104624_create_roster_slots.rb
@@ -0,0 +1,14 @@
class CreateRosterSlots < ActiveRecord::Migration
def change
create_table :roster_slots do |t|
t.belongs_to :team, index: true, foreign_key: true
t.belongs_to :league_position, index: true, foreign_key: true
t.belongs_to :player, index: true, foreign_key: true
t.integer :status, default: 0
t.datetime :active_at
t.datetime :inactive_at

t.timestamps null: false
end
end
end
61 changes: 60 additions & 1 deletion db/schema.rb
Expand Up @@ -11,11 +11,25 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150627192310) do
ActiveRecord::Schema.define(version: 20150628104624) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "league_players", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.integer "league_id"
t.integer "league_position_id"
t.integer "player_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "league_players", ["league_id"], name: "index_league_players_on_league_id", using: :btree
add_index "league_players", ["league_position_id"], name: "index_league_players_on_league_position_id", using: :btree
add_index "league_players", ["player_id"], name: "index_league_players_on_player_id", using: :btree

create_table "league_point_categories", force: :cascade do |t|
t.integer "league_id"
t.integer "point_category_id"
Expand Down Expand Up @@ -56,6 +70,18 @@

add_index "leagues", ["league_template_id"], name: "index_leagues_on_league_template_id", using: :btree

create_table "players", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.integer "league_template_id"
t.integer "position_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "players", ["league_template_id"], name: "index_players_on_league_template_id", using: :btree
add_index "players", ["position_id"], name: "index_players_on_position_id", using: :btree

create_table "point_categories", force: :cascade do |t|
t.integer "league_template_id"
t.string "group"
Expand All @@ -77,11 +103,44 @@

add_index "positions", ["league_template_id"], name: "index_positions_on_league_template_id", using: :btree

create_table "roster_slots", force: :cascade do |t|
t.integer "team_id"
t.integer "league_position_id"
t.integer "player_id"
t.integer "status", default: 0
t.datetime "active_at"
t.datetime "inactive_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "roster_slots", ["league_position_id"], name: "index_roster_slots_on_league_position_id", using: :btree
add_index "roster_slots", ["player_id"], name: "index_roster_slots_on_player_id", using: :btree
add_index "roster_slots", ["team_id"], name: "index_roster_slots_on_team_id", using: :btree

create_table "teams", force: :cascade do |t|
t.integer "league_id"
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "teams", ["league_id"], name: "index_teams_on_league_id", using: :btree

add_foreign_key "league_players", "league_positions"
add_foreign_key "league_players", "leagues"
add_foreign_key "league_players", "players"
add_foreign_key "league_point_categories", "leagues"
add_foreign_key "league_point_categories", "point_categories"
add_foreign_key "league_positions", "leagues"
add_foreign_key "league_positions", "positions"
add_foreign_key "leagues", "league_templates"
add_foreign_key "players", "league_templates"
add_foreign_key "players", "positions"
add_foreign_key "point_categories", "league_templates"
add_foreign_key "positions", "league_templates"
add_foreign_key "roster_slots", "league_positions"
add_foreign_key "roster_slots", "players"
add_foreign_key "roster_slots", "teams"
add_foreign_key "teams", "leagues"
end
53 changes: 46 additions & 7 deletions db/seeds/bad_celebs.rb
@@ -1,21 +1,60 @@
require "roster_manager"

class BadCelebs
def self.seed!
league_template = setup_league_template
league = setup_league(league_template)
setup_team(league)
end

def self.setup_league_template
league_template = LeagueTemplate.find_or_create_by!({ title: "Bad Celebrity" })

[["Legal", "foo", 10]].each do |group, title, value|
unless league_template.point_categories.find_by({ title: title, group: group })
league_template.point_categories.create!({ title: title, group: group, suggested_value: value })
end
next if league_template.point_categories.find_by({ title: title, group: group })

league_template.point_categories.create!({ title: title, group: group, suggested_value: value })
end

[["Actor", 2]].each do |title, count|
unless league_template.positions.find_by({ title: title })
league_template.positions.create!({ title: title, suggested_count: count })
end
[["Actor", 2], ["Bench", 2]].each do |title, count|
next if league_template.positions.find_by({ title: title })

league_template.positions.create!({ title: title, suggested_count: count })
end

[["James", "Franco", "Actor"],
["Leonardo", "DiCaprio", "Actor"],
["Paul", "Giamati", "Actor"],
["Russel", "Crow", "Actor"]].each do |first, last, pos|
next if league_template.players.find_by({ first_name: first, last_name: last })

league_template.players.create!({
first_name: first,
last_name: last,
position: Position.find_by({ title: pos })
})
end

league_template
end

def self.setup_league(league_template)
league = League.find_or_create_by!({ title: "Sample league", league_template: league_template })
league.create_point_categories_from_league_template!
league.create_positions_from_league_template!
league.create_players_from_league_template!

league
end

def self.setup_team(league)
team = league.teams.find_or_create_by!({ title: "New Team" })

roster_manager = RosterManager.new(team)
players = Player.pluck(:id).shuffle
positions = league.all_positions
roster_manager.set_roster(players.zip(positions.map(&:id)))

team
end
end
4 changes: 4 additions & 0 deletions features/step_definitions/common_steps.rb
Expand Up @@ -10,3 +10,7 @@
Given(/^that league has the default positions$/) do
@league.create_positions_from_league_template!
end

Given(/^that league has the default players$/) do
@league.create_players_from_league_template!
end
24 changes: 24 additions & 0 deletions features/step_definitions/teams_steps.rb
@@ -0,0 +1,24 @@
require "roster_manager"

Given(/^I have a team in that league$/) do
@team = @league.teams.create!({ title: "New Team" })
end

Given(/^I have set up that team with players$/) do
roster_manager = RosterManager.new(@team)
players = Player.pluck(:id).shuffle
positions = @league.all_positions
roster_manager.set_roster(players.zip(positions.map(&:id)))
end

When(/^I go to my team$/) do
visit "/leagues/#{@league.id}/teams/#{@team.id}"
end

Then(/^I should see my team's players$/) do
@team.roster_slots.each do |roster_slot|
expect(page).to have_content(roster_slot.player.first_name)
expect(page).to have_content(roster_slot.player.last_name)
expect(page).to have_content(roster_slot.league_position.title)
end
end

0 comments on commit 3f80aa4

Please sign in to comment.