diff --git a/app/controllers/matches_controller.rb b/app/controllers/matches_controller.rb index 6dea146..5687b2f 100644 --- a/app/controllers/matches_controller.rb +++ b/app/controllers/matches_controller.rb @@ -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 diff --git a/app/models/match.rb b/app/models/match.rb index f8ede0a..0eb0c14 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -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 diff --git a/app/models/score.rb b/app/models/match_score.rb similarity index 66% rename from app/models/score.rb rename to app/models/match_score.rb index c24c0a9..4d7f9bf 100644 --- a/app/models/score.rb +++ b/app/models/match_score.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class Score < ApplicationRecord +class MatchScore < ApplicationRecord belongs_to :match belongs_to :team end diff --git a/app/models/team.rb b/app/models/team.rb index 925ce9f..f266f9c 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -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 diff --git a/app/serializers/match_score_serializer.rb b/app/serializers/match_score_serializer.rb new file mode 100644 index 0000000..f4f9987 --- /dev/null +++ b/app/serializers/match_score_serializer.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class MatchScoreSerializer < ApplicationSerializer + attributes :points + + has_one :team +end diff --git a/app/serializers/match_serializer.rb b/app/serializers/match_serializer.rb index d608775..c89e7a8 100644 --- a/app/serializers/match_serializer.rb +++ b/app/serializers/match_serializer.rb @@ -3,5 +3,5 @@ class MatchSerializer < ApplicationSerializer attributes :state - has_many :scores + has_many :match_scores end diff --git a/app/serializers/score_serializer.rb b/app/serializers/score_serializer.rb deleted file mode 100644 index 08ef59d..0000000 --- a/app/serializers/score_serializer.rb +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true - -class ScoreSerializer < ApplicationSerializer - attributes :score - - has_one :team -end diff --git a/app/services/match_service.rb b/app/services/match_service.rb index ca4bce9..25b590a 100644 --- a/app/services/match_service.rb +++ b/app/services/match_service.rb @@ -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 diff --git a/db/migrate/0000_create_schema.rb b/db/migrate/0000_create_schema.rb index 95b8b6b..600a2a2 100644 --- a/db/migrate/0000_create_schema.rb +++ b/db/migrate/0000_create_schema.rb @@ -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 } diff --git a/db/seeds.rb b/db/seeds.rb index decba22..853470c 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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 diff --git a/spec/controllers/matches_controller_spec.rb b/spec/controllers/matches_controller_spec.rb index 84f4c2c..d05e1b9 100644 --- a/spec/controllers/matches_controller_spec.rb +++ b/spec/controllers/matches_controller_spec.rb @@ -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 diff --git a/spec/factories/scores.rb b/spec/factories/match_scores.rb similarity index 60% rename from spec/factories/scores.rb rename to spec/factories/match_scores.rb index 7de51f0..34e1320 100644 --- a/spec/factories/scores.rb +++ b/spec/factories/match_scores.rb @@ -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 diff --git a/spec/factories/matches.rb b/spec/factories/matches.rb index b35703d..773151f 100644 --- a/spec/factories/matches.rb +++ b/spec/factories/matches.rb @@ -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 diff --git a/spec/models/score_spec.rb b/spec/models/match_score_spec.rb similarity index 69% rename from spec/models/score_spec.rb rename to spec/models/match_score_spec.rb index 4d68364..a48734f 100644 --- a/spec/models/score_spec.rb +++ b/spec/models/match_score_spec.rb @@ -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 diff --git a/spec/models/match_spec.rb b/spec/models/match_spec.rb index 7eb6c67..74fc582 100644 --- a/spec/models/match_spec.rb +++ b/spec/models/match_spec.rb @@ -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 diff --git a/spec/models/team_spec.rb b/spec/models/team_spec.rb index 23b38bf..b930c5c 100644 --- a/spec/models/team_spec.rb +++ b/spec/models/team_spec.rb @@ -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 diff --git a/spec/services/match_service_spec.rb b/spec/services/match_service_spec.rb index 7706e1f..09fc450 100644 --- a/spec/services/match_service_spec.rb +++ b/spec/services/match_service_spec.rb @@ -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