Skip to content

Commit

Permalink
Draft picks and fantasy teams modeled and seeded
Browse files Browse the repository at this point in the history
  • Loading branch information
tpadjen committed Aug 28, 2015
1 parent f05425f commit 89d0bb4
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/models/draft_pick.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class DraftPick < ActiveRecord::Base
belongs_to :nfl_player
belongs_to :fantasy_team
end
4 changes: 4 additions & 0 deletions app/models/fantasy_team.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class FantasyTeam < ActiveRecord::Base
has_many :draft_picks
has_many :nfl_players, through: :draft_picks
end
6 changes: 6 additions & 0 deletions app/models/nfl_player.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class NflPlayer < ActiveRecord::Base
belongs_to :nfl_team
has_one :draft_pick
has_one :fantasy_team, through: :draft_pick

VALID_POSITIONS = ['QB', 'WR', 'RB', 'TE', 'DEF', 'K']

Expand All @@ -13,4 +15,8 @@ def self.position_valid?(pos)
VALID_POSITIONS.include?(pos.upcase)
end

def picked?
fantasy_team != nil
end

end
10 changes: 10 additions & 0 deletions db/migrate/20150828035921_create_fantasy_teams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateFantasyTeams < ActiveRecord::Migration
def change
create_table :fantasy_teams do |t|
t.string :owner
t.string :pick_number

t.timestamps null: false
end
end
end
11 changes: 11 additions & 0 deletions db/migrate/20150828040414_create_draft_picks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateDraftPicks < ActiveRecord::Migration
def change
create_table :draft_picks do |t|
t.integer :number
t.references :nfl_player, index: true, foreign_key: true
t.references :fantasy_team, index: true, foreign_key: true

t.timestamps null: false
end
end
end
22 changes: 21 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,29 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150827171048) do
ActiveRecord::Schema.define(version: 20150828040414) do

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

create_table "draft_picks", force: :cascade do |t|
t.integer "number"
t.integer "nfl_player_id"
t.integer "fantasy_team_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "draft_picks", ["fantasy_team_id"], name: "index_draft_picks_on_fantasy_team_id", using: :btree
add_index "draft_picks", ["nfl_player_id"], name: "index_draft_picks_on_nfl_player_id", using: :btree

create_table "fantasy_teams", force: :cascade do |t|
t.string "owner"
t.string "pick_number"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "nfl_players", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
Expand All @@ -38,5 +56,7 @@
t.datetime "updated_at", null: false
end

add_foreign_key "draft_picks", "fantasy_teams"
add_foreign_key "draft_picks", "nfl_players"
add_foreign_key "nfl_players", "nfl_teams"
end
15 changes: 15 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,19 @@
)
puts "Loading #{pl.name} - #{pl.position_rank} - #{pl.projected_points}"
end
end

owners = ['Ian', 'Brandt', 'Lamb', 'Goldson', 'Carlson',
'Chuck', 'Kinder', 'Lucas', 'Rodney', 'TBQ']
owners.each_with_index do |owner, i|
fantasy_team = FantasyTeam.find_or_create_by(owner: owner, pick_number: i + 1)
puts "\nLoading #{owner}'s draft: "
picks = []
(1..16).each do |round|
offset = round % 2 == 1 ? i + 1 : 10 - i
pick = (round-1)*10 + offset
fantasy_team.draft_picks.create(number: pick)
end

puts "He gets picks #{fantasy_team.draft_picks.map(&:number)}\n"
end
11 changes: 11 additions & 0 deletions test/fixtures/draft_picks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
number: MyString
nfl_player_id:
fantasy_team_id:

two:
number: MyString
nfl_player_id:
fantasy_team_id:
9 changes: 9 additions & 0 deletions test/fixtures/fantasy_teams.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
owner: MyString
pick_number: MyString

two:
owner: MyString
pick_number: MyString
7 changes: 7 additions & 0 deletions test/models/draft_pick_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class DraftPickTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
7 changes: 7 additions & 0 deletions test/models/fantasy_team_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class FantasyTeamTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 89d0bb4

Please sign in to comment.