Start on Interactor vodoo magic

This commit is contained in:
Daniel Schädler 2018-11-13 16:12:44 +01:00 committed by Malaber
parent 2b94e7f627
commit 2fc6d8e75b
12 changed files with 115 additions and 48 deletions

View File

@ -32,8 +32,12 @@ gem 'bootsnap', '>= 1.1.0', require: false
gem 'devise' gem 'devise'
gem 'devise_token_auth' gem 'devise_token_auth'
gem 'rack-cors' gem 'rack-cors'
# Interactors
gem 'interactor', '~> 3.1.1'
group :development, :test do group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console # Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: %i[mri mingw x64_mingw] gem 'byebug', platforms: %i[mri mingw x64_mingw]

View File

@ -88,6 +88,7 @@ GEM
domain_name (~> 0.5) domain_name (~> 0.5)
i18n (1.1.1) i18n (1.1.1)
concurrent-ruby (~> 1.0) concurrent-ruby (~> 1.0)
interactor (3.1.1)
jaro_winkler (1.5.1) jaro_winkler (1.5.1)
json (2.1.0) json (2.1.0)
kramdown (1.17.0) kramdown (1.17.0)
@ -253,6 +254,7 @@ DEPENDENCIES
devise_token_auth devise_token_auth
factory_bot_rails factory_bot_rails
faker faker
interactor (~> 3.1.1)
listen (>= 3.0.5, < 3.2) listen (>= 3.0.5, < 3.2)
puma (~> 3.11) puma (~> 3.11)
rack-cors rack-cors

View File

@ -1,5 +1,7 @@
# frozen_string_literal: true
class MatchesController < ApplicationController class MatchesController < ApplicationController
before_action :set_match, only: [:show, :update, :destroy] before_action :set_match, only: %i[show update destroy]
# GET /matches # GET /matches
def index def index
@ -39,13 +41,14 @@ class MatchesController < ApplicationController
end end
private 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. # Use callbacks to share common setup or constraints between actions.
def match_params def set_match
params.fetch(:match, {}) @match = Match.find(params[:id])
end end
# Only allow a trusted parameter "white list" through.
def match_params
params.fetch(:match, {})
end
end end

View File

@ -1,5 +1,7 @@
# frozen_string_literal: true
class TeamsController < ApplicationController class TeamsController < ApplicationController
before_action :set_team, only: [:show, :update, :destroy] before_action :set_team, only: %i[show update destroy]
# GET /teams # GET /teams
def index def index
@ -39,13 +41,14 @@ class TeamsController < ApplicationController
end end
private 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. # Use callbacks to share common setup or constraints between actions.
def team_params def set_team
params.fetch(:team, {}) @team = Team.find(params[:id])
end end
# Only allow a trusted parameter "white list" through.
def team_params
params.fetch(:team, {})
end
end end

View File

@ -1,5 +1,7 @@
# frozen_string_literal: true
class TournamentsController < ApplicationController class TournamentsController < ApplicationController
before_action :set_tournament, only: [:show, :update, :destroy] before_action :set_tournament, only: %i[show update destroy]
# GET /tournaments # GET /tournaments
def index def index
@ -39,13 +41,14 @@ class TournamentsController < ApplicationController
end end
private 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. # Use callbacks to share common setup or constraints between actions.
def tournament_params def set_tournament
params.fetch(:tournament, {}) @tournament = Tournament.find(params[:id])
end end
# Only allow a trusted parameter "white list" through.
def tournament_params
params.fetch(:tournament, {})
end
end end

View File

@ -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

View File

@ -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

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class Match < ApplicationRecord class Match < ApplicationRecord
enum status: %i[team1_won team2_won undecided in_progress not_started]
belongs_to :stage belongs_to :stage
belongs_to :group belongs_to :group
has_many :scores, dependent: :destroy has_many :scores, dependent: :destroy

View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
class UpdateMatchScore
include Interactor::Organizer
organize ChangeMatchScore, UpdateMatchScore
end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
class MatchesControllerTest < ActionDispatch::IntegrationTest class MatchesControllerTest < ActionDispatch::IntegrationTest
@ -5,30 +7,30 @@ class MatchesControllerTest < ActionDispatch::IntegrationTest
@match = matches(:one) @match = matches(:one)
end end
test "should get index" do test 'should get index' do
get matches_url, as: :json get matches_url, as: :json
assert_response :success assert_response :success
end end
test "should create match" do test 'should create match' do
assert_difference('Match.count') do assert_difference('Match.count') do
post matches_url, params: { match: { } }, as: :json post matches_url, params: { match: {} }, as: :json
end end
assert_response 201 assert_response 201
end end
test "should show match" do test 'should show match' do
get match_url(@match), as: :json get match_url(@match), as: :json
assert_response :success assert_response :success
end end
test "should update match" do test 'should update match' do
patch match_url(@match), params: { match: { } }, as: :json patch match_url(@match), params: { match: {} }, as: :json
assert_response 200 assert_response 200
end end
test "should destroy match" do test 'should destroy match' do
assert_difference('Match.count', -1) do assert_difference('Match.count', -1) do
delete match_url(@match), as: :json delete match_url(@match), as: :json
end end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
class TeamsControllerTest < ActionDispatch::IntegrationTest class TeamsControllerTest < ActionDispatch::IntegrationTest
@ -5,30 +7,30 @@ class TeamsControllerTest < ActionDispatch::IntegrationTest
@team = teams(:one) @team = teams(:one)
end end
test "should get index" do test 'should get index' do
get teams_url, as: :json get teams_url, as: :json
assert_response :success assert_response :success
end end
test "should create team" do test 'should create team' do
assert_difference('Team.count') do assert_difference('Team.count') do
post teams_url, params: { team: { } }, as: :json post teams_url, params: { team: {} }, as: :json
end end
assert_response 201 assert_response 201
end end
test "should show team" do test 'should show team' do
get team_url(@team), as: :json get team_url(@team), as: :json
assert_response :success assert_response :success
end end
test "should update team" do test 'should update team' do
patch team_url(@team), params: { team: { } }, as: :json patch team_url(@team), params: { team: {} }, as: :json
assert_response 200 assert_response 200
end end
test "should destroy team" do test 'should destroy team' do
assert_difference('Team.count', -1) do assert_difference('Team.count', -1) do
delete team_url(@team), as: :json delete team_url(@team), as: :json
end end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'test_helper' require 'test_helper'
class TournamentsControllerTest < ActionDispatch::IntegrationTest class TournamentsControllerTest < ActionDispatch::IntegrationTest
@ -5,30 +7,30 @@ class TournamentsControllerTest < ActionDispatch::IntegrationTest
@tournament = tournaments(:one) @tournament = tournaments(:one)
end end
test "should get index" do test 'should get index' do
get tournaments_url, as: :json get tournaments_url, as: :json
assert_response :success assert_response :success
end end
test "should create tournament" do test 'should create tournament' do
assert_difference('Tournament.count') do assert_difference('Tournament.count') do
post tournaments_url, params: { tournament: { } }, as: :json post tournaments_url, params: { tournament: {} }, as: :json
end end
assert_response 201 assert_response 201
end end
test "should show tournament" do test 'should show tournament' do
get tournament_url(@tournament), as: :json get tournament_url(@tournament), as: :json
assert_response :success assert_response :success
end end
test "should update tournament" do test 'should update tournament' do
patch tournament_url(@tournament), params: { tournament: { } }, as: :json patch tournament_url(@tournament), params: { tournament: {} }, as: :json
assert_response 200 assert_response 200
end end
test "should destroy tournament" do test 'should destroy tournament' do
assert_difference('Tournament.count', -1) do assert_difference('Tournament.count', -1) do
delete tournament_url(@tournament), as: :json delete tournament_url(@tournament), as: :json
end end