From 2fc6d8e75b78be96191579bbeb37a4d333210fd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sch=C3=A4dler?= Date: Tue, 13 Nov 2018 16:12:44 +0100 Subject: [PATCH] Start on Interactor vodoo magic --- Gemfile | 4 +++ Gemfile.lock | 2 ++ app/controllers/matches_controller.rb | 21 ++++++++------- app/controllers/teams_controller.rb | 21 ++++++++------- app/controllers/tournaments_controller.rb | 21 ++++++++------- app/interactors/ChangeMatchScore.rb | 10 +++++++ app/interactors/UpdateMatchStatus.rb | 27 +++++++++++++++++++ app/models/match.rb | 2 ++ app/organizers/UpdateMatchScore.rb | 7 +++++ test/controllers/matches_controller_test.rb | 16 ++++++----- test/controllers/teams_controller_test.rb | 16 ++++++----- .../tournaments_controller_test.rb | 16 ++++++----- 12 files changed, 115 insertions(+), 48 deletions(-) create mode 100644 app/interactors/ChangeMatchScore.rb create mode 100644 app/interactors/UpdateMatchStatus.rb create mode 100644 app/organizers/UpdateMatchScore.rb diff --git a/Gemfile b/Gemfile index 92d7a16..4073c54 100644 --- a/Gemfile +++ b/Gemfile @@ -32,8 +32,12 @@ gem 'bootsnap', '>= 1.1.0', require: false gem 'devise' gem 'devise_token_auth' + gem 'rack-cors' +# Interactors +gem 'interactor', '~> 3.1.1' + group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: %i[mri mingw x64_mingw] diff --git a/Gemfile.lock b/Gemfile.lock index 77022e1..151d141 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -88,6 +88,7 @@ GEM domain_name (~> 0.5) i18n (1.1.1) concurrent-ruby (~> 1.0) + interactor (3.1.1) jaro_winkler (1.5.1) json (2.1.0) kramdown (1.17.0) @@ -253,6 +254,7 @@ DEPENDENCIES devise_token_auth factory_bot_rails faker + interactor (~> 3.1.1) listen (>= 3.0.5, < 3.2) puma (~> 3.11) rack-cors diff --git a/app/controllers/matches_controller.rb b/app/controllers/matches_controller.rb index b760ec7..d4a14ad 100644 --- a/app/controllers/matches_controller.rb +++ b/app/controllers/matches_controller.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + class MatchesController < ApplicationController - before_action :set_match, only: [:show, :update, :destroy] + before_action :set_match, only: %i[show update destroy] # GET /matches def index @@ -39,13 +41,14 @@ class MatchesController < ApplicationController end private - # Use callbacks to share common setup or constraints between actions. - def set_match - @match = Match.find(params[:id]) - end - # Only allow a trusted parameter "white list" through. - def match_params - params.fetch(:match, {}) - end + # Use callbacks to share common setup or constraints between actions. + def set_match + @match = Match.find(params[:id]) + end + + # Only allow a trusted parameter "white list" through. + def match_params + params.fetch(:match, {}) + end end diff --git a/app/controllers/teams_controller.rb b/app/controllers/teams_controller.rb index 7e02232..3f9dc8a 100644 --- a/app/controllers/teams_controller.rb +++ b/app/controllers/teams_controller.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + class TeamsController < ApplicationController - before_action :set_team, only: [:show, :update, :destroy] + before_action :set_team, only: %i[show update destroy] # GET /teams def index @@ -39,13 +41,14 @@ class TeamsController < ApplicationController end private - # Use callbacks to share common setup or constraints between actions. - def set_team - @team = Team.find(params[:id]) - end - # Only allow a trusted parameter "white list" through. - def team_params - params.fetch(:team, {}) - end + # Use callbacks to share common setup or constraints between actions. + def set_team + @team = Team.find(params[:id]) + end + + # Only allow a trusted parameter "white list" through. + def team_params + params.fetch(:team, {}) + end end diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index ec0fe1b..f1b954e 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + class TournamentsController < ApplicationController - before_action :set_tournament, only: [:show, :update, :destroy] + before_action :set_tournament, only: %i[show update destroy] # GET /tournaments def index @@ -39,13 +41,14 @@ class TournamentsController < ApplicationController end private - # Use callbacks to share common setup or constraints between actions. - def set_tournament - @tournament = Tournament.find(params[:id]) - end - # Only allow a trusted parameter "white list" through. - def tournament_params - params.fetch(:tournament, {}) - end + # Use callbacks to share common setup or constraints between actions. + def set_tournament + @tournament = Tournament.find(params[:id]) + end + + # Only allow a trusted parameter "white list" through. + def tournament_params + params.fetch(:tournament, {}) + end end diff --git a/app/interactors/ChangeMatchScore.rb b/app/interactors/ChangeMatchScore.rb new file mode 100644 index 0000000..5cd5546 --- /dev/null +++ b/app/interactors/ChangeMatchScore.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class ChangeMatchScore + include Interactor + + def call + context.match.score_team1 = context.score_team1 + context.match.score_team2 = context.score_team2 + end +end diff --git a/app/interactors/UpdateMatchStatus.rb b/app/interactors/UpdateMatchStatus.rb new file mode 100644 index 0000000..ae26f3d --- /dev/null +++ b/app/interactors/UpdateMatchStatus.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require 'match' + +class UpdateMatchStatus + include Interactor + + def call + context.match.status = evaluate_new_match_state(context.match) + end + + def evaluate_new_match_state(match) + score_team1 = match.score_team1 + score_team2 = match.score_team2 + if score_team1 < score_team2 + return Match.team2_won + elsif score_team2 < score_team1 + return Match.team1_won + else + if match.is_group_match + return Match.undecided + else + return Match.in_progress + end + end + end +end diff --git a/app/models/match.rb b/app/models/match.rb index 54c1f66..a0e837b 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class Match < ApplicationRecord + enum status: %i[team1_won team2_won undecided in_progress not_started] + belongs_to :stage belongs_to :group has_many :scores, dependent: :destroy diff --git a/app/organizers/UpdateMatchScore.rb b/app/organizers/UpdateMatchScore.rb new file mode 100644 index 0000000..016b550 --- /dev/null +++ b/app/organizers/UpdateMatchScore.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class UpdateMatchScore + include Interactor::Organizer + + organize ChangeMatchScore, UpdateMatchScore +end diff --git a/test/controllers/matches_controller_test.rb b/test/controllers/matches_controller_test.rb index 5c82bdc..ad34132 100644 --- a/test/controllers/matches_controller_test.rb +++ b/test/controllers/matches_controller_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' class MatchesControllerTest < ActionDispatch::IntegrationTest @@ -5,30 +7,30 @@ class MatchesControllerTest < ActionDispatch::IntegrationTest @match = matches(:one) end - test "should get index" do + test 'should get index' do get matches_url, as: :json assert_response :success end - test "should create match" do + test 'should create match' do assert_difference('Match.count') do - post matches_url, params: { match: { } }, as: :json + post matches_url, params: { match: {} }, as: :json end assert_response 201 end - test "should show match" do + test 'should show match' do get match_url(@match), as: :json assert_response :success end - test "should update match" do - patch match_url(@match), params: { match: { } }, as: :json + test 'should update match' do + patch match_url(@match), params: { match: {} }, as: :json assert_response 200 end - test "should destroy match" do + test 'should destroy match' do assert_difference('Match.count', -1) do delete match_url(@match), as: :json end diff --git a/test/controllers/teams_controller_test.rb b/test/controllers/teams_controller_test.rb index 8f5b105..f782bb9 100644 --- a/test/controllers/teams_controller_test.rb +++ b/test/controllers/teams_controller_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' class TeamsControllerTest < ActionDispatch::IntegrationTest @@ -5,30 +7,30 @@ class TeamsControllerTest < ActionDispatch::IntegrationTest @team = teams(:one) end - test "should get index" do + test 'should get index' do get teams_url, as: :json assert_response :success end - test "should create team" do + test 'should create team' do assert_difference('Team.count') do - post teams_url, params: { team: { } }, as: :json + post teams_url, params: { team: {} }, as: :json end assert_response 201 end - test "should show team" do + test 'should show team' do get team_url(@team), as: :json assert_response :success end - test "should update team" do - patch team_url(@team), params: { team: { } }, as: :json + test 'should update team' do + patch team_url(@team), params: { team: {} }, as: :json assert_response 200 end - test "should destroy team" do + test 'should destroy team' do assert_difference('Team.count', -1) do delete team_url(@team), as: :json end diff --git a/test/controllers/tournaments_controller_test.rb b/test/controllers/tournaments_controller_test.rb index fe57b00..8d8701d 100644 --- a/test/controllers/tournaments_controller_test.rb +++ b/test/controllers/tournaments_controller_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' class TournamentsControllerTest < ActionDispatch::IntegrationTest @@ -5,30 +7,30 @@ class TournamentsControllerTest < ActionDispatch::IntegrationTest @tournament = tournaments(:one) end - test "should get index" do + test 'should get index' do get tournaments_url, as: :json assert_response :success end - test "should create tournament" do + test 'should create tournament' do assert_difference('Tournament.count') do - post tournaments_url, params: { tournament: { } }, as: :json + post tournaments_url, params: { tournament: {} }, as: :json end assert_response 201 end - test "should show tournament" do + test 'should show tournament' do get tournament_url(@tournament), as: :json assert_response :success end - test "should update tournament" do - patch tournament_url(@tournament), params: { tournament: { } }, as: :json + test 'should update tournament' do + patch tournament_url(@tournament), params: { tournament: {} }, as: :json assert_response 200 end - test "should destroy tournament" do + test 'should destroy tournament' do assert_difference('Tournament.count', -1) do delete tournament_url(@tournament), as: :json end