From 77c76b7a3193cb7db67a784117241538e9029150 Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Fri, 25 Oct 2019 23:47:38 +0200 Subject: [PATCH 1/9] Add matches method to tournament The method collects all matches from all stages --- app/models/tournament.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/models/tournament.rb b/app/models/tournament.rb index ec71609..6a52b62 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -16,6 +16,10 @@ class Tournament < ApplicationRecord after_initialize :generate_code + def matches + stages.map(&:matches).flatten + end + private def generate_code From a6b7a905a0d9f20abcad8d1b60d3028ab1156322 Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Fri, 25 Oct 2019 23:49:27 +0200 Subject: [PATCH 2/9] Return matches as children of tournament --- app/controllers/matches_controller.rb | 10 ++++++++++ config/routes.rb | 1 + 2 files changed, 11 insertions(+) diff --git a/app/controllers/matches_controller.rb b/app/controllers/matches_controller.rb index 778fca9..f3918c6 100644 --- a/app/controllers/matches_controller.rb +++ b/app/controllers/matches_controller.rb @@ -4,6 +4,12 @@ class MatchesController < ApplicationController before_action :set_match, only: %i[show update] before_action :validate_params, only: %i[update] before_action -> { require_owner! @match.owner }, only: %i[update] + before_action :set_tournament, only: %i[index] + + # GET/tournaments/1/matches + def index + render json: @tournament.matches, each_serializer: MatchSerializer + end # GET /matches/1 def show @@ -61,6 +67,10 @@ class MatchesController < ApplicationController @match = Match.find(params[:id]) end + def set_tournament + @tournament = Tournament.find(params[:tournament_id]) + end + def match_params params.slice(:state).permit! end diff --git a/config/routes.rb b/config/routes.rb index 61901bd..143d90c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,6 +13,7 @@ Rails.application.routes.draw do resources :teams, only: %i[show update] resources :tournaments do resources :statistics, only: %i[index] + resources :matches, only: %i[index] end resources :match_scores, only: %i[show update] resources :groups, only: %i[show] From 33ccab8988c2067fe3f32e224f675c23d5848cfb Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Sat, 26 Oct 2019 00:32:14 +0200 Subject: [PATCH 3/9] Return all matches also from groups --- app/models/tournament.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 6a52b62..d91ec72 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -17,7 +17,7 @@ class Tournament < ApplicationRecord after_initialize :generate_code def matches - stages.map(&:matches).flatten + [stages.map(&:matches), stages.map { |s| s.groups.map(&:matches) }].flatten end private From b001817bacf4e9f05312ba2cd727a7a1180067ea Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Sat, 26 Oct 2019 00:34:04 +0200 Subject: [PATCH 4/9] Filter matches by state parameter --- app/controllers/matches_controller.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/controllers/matches_controller.rb b/app/controllers/matches_controller.rb index f3918c6..7c0c012 100644 --- a/app/controllers/matches_controller.rb +++ b/app/controllers/matches_controller.rb @@ -8,7 +8,14 @@ class MatchesController < ApplicationController # GET/tournaments/1/matches def index - render json: @tournament.matches, each_serializer: MatchSerializer + matches = if match_params['state'].nil? + @tournament.matches + else + @tournament.matches.select do |m| + m.state == match_params['state'] + end + end + render json: matches, each_serializer: MatchSerializer end # GET /matches/1 From 82e4ff90dc902304a957d8efbc3752d864ec0d23 Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Sat, 26 Oct 2019 01:22:52 +0200 Subject: [PATCH 5/9] Test tournament.matches method --- spec/models/tournament_spec.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/spec/models/tournament_spec.rb b/spec/models/tournament_spec.rb index 20e6f6b..c182909 100644 --- a/spec/models/tournament_spec.rb +++ b/spec/models/tournament_spec.rb @@ -30,4 +30,29 @@ RSpec.describe Tournament, type: :model do it { should have_many :teams } it { should have_many :stages } end + + describe '#matches' do + context 'group stage tournament' do + before do + @tournament = create(:group_stage_tournament) + end + + it 'returns only matches' do + @tournament.matches.each do |m| + expect(m).to be_a Match + end + end + end + + context 'stage tournament' do + before do + @tournament = create(:stage_tournament) + end + it 'returns only matches' do + @tournament.matches.each do |m| + expect(m).to be_a Match + end + end + end + end end From 400a396f00cc4fec0afcb0031131cf173def0a7d Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Sat, 26 Oct 2019 01:23:15 +0200 Subject: [PATCH 6/9] Test match index controller with filter --- spec/controllers/matches_controller_spec.rb | 35 +++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/spec/controllers/matches_controller_spec.rb b/spec/controllers/matches_controller_spec.rb index 061c4e8..38bc536 100644 --- a/spec/controllers/matches_controller_spec.rb +++ b/spec/controllers/matches_controller_spec.rb @@ -2,6 +2,14 @@ require 'rails_helper' +def test_get_index_with_filter(filter_state) + get :index, params: {state: filter_state, tournament_id: @tournament.to_param} + body = deserialize_response response + body.each do |match| + expect(match[:state]).to eq(filter_state) + end +end + RSpec.describe MatchesController, type: :controller do before do @match = create(:match, state: :not_started) @@ -12,6 +20,33 @@ RSpec.describe MatchesController, type: :controller do @match.match_scores = create_pair(:match_score) end + describe 'GET #index' do + context 'on a running group stage' do + before do + @tournament = create(:group_stage_tournament, match_factory: :running_group_match) + @tournament.matches.each_with_index do |m, i| + m.state = :not_started if i.even? + m.save! + end + end + + it 'filters running matches when told to do so' do + test_get_index_with_filter('running') + end + + it 'filters not_started matches when told to do so' do + test_get_index_with_filter('not_started') + end + + it 'doesn\'t break if the filter contains rubbish' do + filter_state = 'saftladen' + get :index, params: { state: filter_state, tournament_id: @tournament.to_param } + body = deserialize_response response + expect(body.empty?).to be true + end + end + end + describe 'GET #show' do it 'should return success' do get :show, params: { id: @match.to_param } From b37f5105341a7a3e900fd30314e8c37b1d53c459 Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Sat, 26 Oct 2019 01:32:17 +0200 Subject: [PATCH 7/9] Unnecessary Variables --- spec/controllers/matches_controller_spec.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/spec/controllers/matches_controller_spec.rb b/spec/controllers/matches_controller_spec.rb index 38bc536..579f341 100644 --- a/spec/controllers/matches_controller_spec.rb +++ b/spec/controllers/matches_controller_spec.rb @@ -4,8 +4,7 @@ require 'rails_helper' def test_get_index_with_filter(filter_state) get :index, params: {state: filter_state, tournament_id: @tournament.to_param} - body = deserialize_response response - body.each do |match| + deserialize_response(response).each do |match| expect(match[:state]).to eq(filter_state) end end @@ -39,8 +38,7 @@ RSpec.describe MatchesController, type: :controller do end it 'doesn\'t break if the filter contains rubbish' do - filter_state = 'saftladen' - get :index, params: { state: filter_state, tournament_id: @tournament.to_param } + get :index, params: { state: 'saftladen', tournament_id: @tournament.to_param } body = deserialize_response response expect(body.empty?).to be true end From b8f8ffb411fa38f286c6b157a72f0cbe225fefe0 Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Sat, 26 Oct 2019 01:35:46 +0200 Subject: [PATCH 8/9] More space for hound to be happy with --- spec/controllers/matches_controller_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/controllers/matches_controller_spec.rb b/spec/controllers/matches_controller_spec.rb index 579f341..d36e9ba 100644 --- a/spec/controllers/matches_controller_spec.rb +++ b/spec/controllers/matches_controller_spec.rb @@ -3,7 +3,7 @@ require 'rails_helper' def test_get_index_with_filter(filter_state) - get :index, params: {state: filter_state, tournament_id: @tournament.to_param} + get :index, params: { state: filter_state, tournament_id: @tournament.to_param } deserialize_response(response).each do |match| expect(match[:state]).to eq(filter_state) end From 777ee95281a5db922ea58cb1f7328cbc6094a680 Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Sat, 26 Oct 2019 01:36:01 +0200 Subject: [PATCH 9/9] Readability +4 --- spec/models/tournament_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/models/tournament_spec.rb b/spec/models/tournament_spec.rb index c182909..a555abe 100644 --- a/spec/models/tournament_spec.rb +++ b/spec/models/tournament_spec.rb @@ -48,6 +48,7 @@ RSpec.describe Tournament, type: :model do before do @tournament = create(:stage_tournament) end + it 'returns only matches' do @tournament.matches.each do |m| expect(m).to be_a Match