Start on Interactor vodoo magic
This commit is contained in:
parent
2b94e7f627
commit
2fc6d8e75b
4
Gemfile
4
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]
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class UpdateMatchScore
|
||||
include Interactor::Organizer
|
||||
|
||||
organize ChangeMatchScore, UpdateMatchScore
|
||||
end
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue