Change Score to MatchScore & MatchScore score attribute to points
This is done to improve the naming and therefore make things easier to understand.
This commit is contained in:
parent
4afa32542e
commit
68f9b3b2b3
|
|
@ -3,6 +3,6 @@
|
|||
class MatchesController < ApplicationController
|
||||
# GET /matches/1
|
||||
def show
|
||||
render json: Match.find(params[:id]), include: ['scores.score', 'scores.team'], status: status
|
||||
render json: Match.find(params[:id]), include: ['match_scores.points', 'match_scores.team'], status: status
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -5,9 +5,9 @@ class Match < ApplicationRecord
|
|||
|
||||
belongs_to :stage, optional: true
|
||||
belongs_to :group, optional: true
|
||||
has_many :scores, dependent: :destroy
|
||||
has_many :match_scores, dependent: :destroy
|
||||
|
||||
validates :scores, length: { maximum: 2 }
|
||||
validates :match_scores, length: { maximum: 2 }
|
||||
|
||||
validate :stage_xor_group
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Score < ApplicationRecord
|
||||
class MatchScore < ApplicationRecord
|
||||
belongs_to :match
|
||||
belongs_to :team
|
||||
end
|
||||
|
|
@ -3,7 +3,7 @@
|
|||
class Team < ApplicationRecord
|
||||
belongs_to :tournament, optional: true
|
||||
has_many :group_scores, dependent: :destroy
|
||||
has_many :scores, dependent: :destroy
|
||||
has_many :match_scores, dependent: :destroy
|
||||
|
||||
validates :name, presence: true
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class MatchScoreSerializer < ApplicationSerializer
|
||||
attributes :points
|
||||
|
||||
has_one :team
|
||||
end
|
||||
|
|
@ -3,5 +3,5 @@
|
|||
class MatchSerializer < ApplicationSerializer
|
||||
attributes :state
|
||||
|
||||
has_many :scores
|
||||
has_many :match_scores
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,7 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class ScoreSerializer < ApplicationSerializer
|
||||
attributes :score
|
||||
|
||||
has_one :team
|
||||
end
|
||||
|
|
@ -20,16 +20,16 @@ class MatchService
|
|||
i = matches.size
|
||||
match = Match.new state: :not_started,
|
||||
position: i,
|
||||
scores: [
|
||||
Score.create(team: teams[2 * i]),
|
||||
Score.create(team: teams[(2 * i) + 1])
|
||||
match_scores: [
|
||||
MatchScore.create(team: teams[2 * i]),
|
||||
MatchScore.create(team: teams[(2 * i) + 1])
|
||||
]
|
||||
matches << match
|
||||
end
|
||||
|
||||
until matches.size >= needed_games
|
||||
i = matches.size
|
||||
match = Match.new state: :single_team, position: i, scores: [Score.create(team: teams[i])]
|
||||
match = Match.new state: :single_team, position: i, match_scores: [MatchScore.create(team: teams[i])]
|
||||
matches << match
|
||||
end
|
||||
matches
|
||||
|
|
|
|||
|
|
@ -92,8 +92,8 @@ class CreateSchema < ActiveRecord::Migration[5.2]
|
|||
t.timestamps
|
||||
end
|
||||
|
||||
create_table :scores do |t|
|
||||
t.integer :score, default: 0
|
||||
create_table :match_scores do |t|
|
||||
t.integer :points, default: 0
|
||||
|
||||
t.belongs_to :match, index: true, null: false, foreign_key: { on_delete: :cascade }
|
||||
t.belongs_to :team, index: true, null: false, foreign_key: { on_delete: :cascade }
|
||||
|
|
|
|||
|
|
@ -8,5 +8,5 @@ team1 = tournament.teams.create name: Faker::Dog.name
|
|||
team2 = tournament.teams.create name: Faker::Dog.name
|
||||
stage = tournament.stages.create!
|
||||
match = stage.matches.create!
|
||||
match.scores.create! team: team1, score: 0
|
||||
match.scores.create! team: team2, score: 1
|
||||
match.match_scores.create! team: team1, points: 0
|
||||
match.match_scores.create! team: team2, points: 1
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ require 'rails_helper'
|
|||
RSpec.describe MatchesController, type: :controller do
|
||||
before do
|
||||
@match = create(:match)
|
||||
@match.scores = create_pair(:score)
|
||||
@match.match_scores = create_pair(:match_score)
|
||||
end
|
||||
|
||||
describe 'GET #show' do
|
||||
|
|
@ -19,7 +19,7 @@ RSpec.describe MatchesController, type: :controller do
|
|||
get :show, params: { id: @match.to_param }
|
||||
body = ActiveModelSerializers::Deserialization.jsonapi_parse(JSON.parse(response.body))
|
||||
expect(body[:state]).to eq(@match.state)
|
||||
expect(body[:score_ids]).to eq(@match.scores.map { |score| score.id.to_s })
|
||||
expect(body[:match_score_ids]).to eq(@match.match_scores.map { |match_score| match_score.id.to_s })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
FactoryBot.define do
|
||||
factory :score do
|
||||
score { rand(0..10) }
|
||||
factory :match_score do
|
||||
points { rand(0..10) }
|
||||
match
|
||||
team
|
||||
end
|
||||
|
|
@ -5,10 +5,10 @@ FactoryBot.define do
|
|||
stage
|
||||
factory :running_playoff_match do
|
||||
transient do
|
||||
scores_count { 2 }
|
||||
match_scores_count { 2 }
|
||||
end
|
||||
after(:create) do |match, evaluator|
|
||||
match.scores = create_list(:score, evaluator.scores_count)
|
||||
match.match_scores = create_list(:match_score, evaluator.match_scores_count)
|
||||
end
|
||||
state { 3 }
|
||||
end
|
||||
|
|
|
|||
|
|
@ -2,13 +2,13 @@
|
|||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Score, type: :model do
|
||||
RSpec.describe MatchScore, type: :model do
|
||||
describe 'association' do
|
||||
it { should belong_to :match }
|
||||
it { should belong_to :team }
|
||||
end
|
||||
|
||||
it 'has a valid factory' do
|
||||
expect(build(:score)).to be_valid
|
||||
expect(build(:match_score)).to be_valid
|
||||
end
|
||||
end
|
||||
|
|
@ -4,7 +4,7 @@ require 'rails_helper'
|
|||
|
||||
RSpec.describe Match, type: :model do
|
||||
context 'association' do
|
||||
it { should have_many :scores }
|
||||
it { should have_many :match_scores }
|
||||
it { should belong_to :stage }
|
||||
it { should belong_to :group }
|
||||
end
|
||||
|
|
@ -26,20 +26,20 @@ RSpec.describe Match, type: :model do
|
|||
end
|
||||
end
|
||||
|
||||
context 'scores' do
|
||||
context 'match_scores' do
|
||||
before do
|
||||
@match = create(:match)
|
||||
@match.scores << build_pair(:score)
|
||||
@match.match_scores << build_pair(:match_score)
|
||||
end
|
||||
|
||||
it 'can only have two scores' do
|
||||
@match.scores << build(:score)
|
||||
it 'can only have two match_scores' do
|
||||
@match.match_scores << build(:match_score)
|
||||
expect(@match).to be_invalid
|
||||
end
|
||||
|
||||
it 'can access its scores' do
|
||||
@match.scores[0].score = 0
|
||||
@match.scores[1].score = 0
|
||||
it 'can access its match_scores' do
|
||||
@match.match_scores[0].points = 0
|
||||
@match.match_scores[1].points = 0
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ RSpec.describe Team, type: :model do
|
|||
describe 'association' do
|
||||
it { should belong_to :tournament }
|
||||
it { should have_many :group_scores }
|
||||
it { should have_many :scores }
|
||||
it { should have_many :match_scores }
|
||||
end
|
||||
|
||||
it 'has a valid factory' do
|
||||
|
|
|
|||
|
|
@ -59,8 +59,8 @@ RSpec.describe MatchService do
|
|||
generated_matches = MatchService.generate_matches teams
|
||||
generated_matches.each_index do |index|
|
||||
match = generated_matches[index]
|
||||
first_team = match.scores.first.team.name
|
||||
second_team = match.scores.second.team.name
|
||||
first_team = match.match_scores.first.team.name
|
||||
second_team = match.match_scores.second.team.name
|
||||
expect(first_team).to eq(teams[2 * index].name)
|
||||
expect(second_team).to eq(teams[2 * index + 1].name)
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in New Issue