Skip to content

Commit

Permalink
added rendering/creating/editing Cups::Pair
Browse files Browse the repository at this point in the history
  • Loading branch information
kortirso committed Feb 29, 2024
1 parent b46f776 commit 277825f
Show file tree
Hide file tree
Showing 16 changed files with 448 additions and 2 deletions.
14 changes: 14 additions & 0 deletions app/contracts/cups/pair_contract.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Cups
class PairContract < ApplicationContract
config.messages.namespace = :cups_pair

params do
optional(:home_name).filled(:string)
optional(:visitor_name).filled(:string)
optional(:start_at)
optional(:points)
end
end
end
82 changes: 82 additions & 0 deletions app/controllers/admin/cups/pairs_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# frozen_string_literal: true

module Admin
module Cups
class PairsController < Admin::BaseController
include Deps[
create_form: 'forms.cups.pairs.create',
update_form: 'forms.cups.pairs.update'
]

before_action :find_cups_pairs, only: %i[index]
before_action :find_cups_round, only: %i[new edit create update]
before_action :find_cups_pair, only: %i[edit update]

def index; end

def new
@cups_pair = ::Cups::Pair.new
end

def edit; end

def create
case create_form.call(cups_round: @cups_round, params: cups_pair_params)
in { errors: errors }
redirect_to new_admin_cups_round_pair_path(cups_round_id: params[:cups_round_id]), alert: errors
else
redirect_to(
admin_cups_round_pairs_path(cups_round_id: params[:cups_round_id]),
notice: t('controllers.admin.cups.pairs.create.success')
)
end
end

def update
case update_form.call(cups_pair: @cups_pair, params: cups_pair_params)
in { errors: errors }
redirect_to(
edit_admin_cups_round_pair_path(cups_round_id: params[:cups_round_id], id: @cups_pair.id),
alert: errors
)
else
redirect_to(
admin_cups_round_pairs_path(cups_round_id: params[:cups_round_id]),
notice: t('controllers.admin.cups.pairs.update.success')
)
end
end

private

def find_cups_pairs
@cups_pairs =
::Cups::Pair
.where(cups_round_id: params[:cups_round_id])
.order(start_at: :asc)
.hashable_pluck(:id, :home_name, :visitor_name, :start_at, :points)
end

def find_cups_round
@cups_round = ::Cups::Round.find(params[:cups_round_id])
end

def find_cups_pair
@cups_pair = @cups_round.cups_pairs.find(params[:id])
end

# rubocop: disable Metrics/AbcSize
def cups_pair_params
params
.require(:cups_pair)
.permit(:home_name, :visitor_name)
.to_h
.merge(
start_at: params[:cups_pair][:start_at].present? ? DateTime.parse(params[:cups_pair][:start_at]) : nil,
points: params[:cups_pair][:points].present? ? params[:cups_pair][:points].split('-') : []
)
end
# rubocop: enable Metrics/AbcSize
end
end
end
6 changes: 5 additions & 1 deletion app/controllers/admin/cups/rounds_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ def create
private

def find_cups_rounds
@cups_rounds = ::Cups::Round.where(cup_id: params[:cup_id]).order(position: :asc)
@cups_rounds =
::Cups::Round
.where(cup_id: params[:cup_id])
.order(position: :asc)
.hashable_pluck(:id, :name, :position)
end

def find_cup
Expand Down
18 changes: 18 additions & 0 deletions app/forms/cups/pairs/create_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module Cups
module Pairs
class CreateForm
include Deps[validator: 'validators.cups.pair']

def call(cups_round:, params:)
errors = validator.call(params: params)
return { errors: errors } if errors.any?

result = cups_round.cups_pairs.create!(params)

{ result: result }
end
end
end
end
18 changes: 18 additions & 0 deletions app/forms/cups/pairs/update_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module Cups
module Pairs
class UpdateForm
include Deps[validator: 'validators.cups.pair']

def call(cups_pair:, params:)
errors = validator.call(params: params)
return { errors: errors } if errors.any?

cups_pair.update!(params)

{ result: cups_pair.reload }
end
end
end
end
7 changes: 7 additions & 0 deletions app/validators/cups/pair_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module Cups
class PairValidator < ApplicationValidator
include Deps[contract: 'contracts.cups.pair']
end
end
16 changes: 16 additions & 0 deletions app/views/controllers/admin/cups/pairs/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div class="form-field">
<%= form.label :home_name, t('views.admin.cups.pairs.index.home_name'), class: 'form-label' %>
<%= form.text_field :home_name, value: @cups_pair.home_name, class: 'form-value' %>
</div>
<div class="form-field">
<%= form.label :visitor_name, t('views.admin.cups.pairs.index.visitor_name'), class: 'form-label' %>
<%= form.text_field :visitor_name, value: @cups_pair.visitor_name, class: 'form-value' %>
</div>
<div class="form-field">
<%= form.label :start_at, t('views.admin.cups.pairs.index.start_at'), class: 'form-label' %>
<%= form.text_field :start_at, value: @cups_pair.start_at&.strftime('%Y-%m-%d %H:%M'), placeholder: '2024-01-31 12:13', class: 'form-value' %>
</div>
<div class="form-field">
<%= form.label :points, t('views.admin.cups.pairs.index.points'), class: 'form-label' %>
<%= form.text_field :points, value: @cups_pair.points&.join('-'), placeholder: '2-1', class: 'form-value' %>
</div>
7 changes: 7 additions & 0 deletions app/views/controllers/admin/cups/pairs/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1><%= t('views.admin.cups.pairs.edit.title') %></h1>
<section>
<%= form_with model: @cups_pair, url: admin_cups_round_pair_path(cups_round_id: params[:cups_round_id], id: params[:id]), method: :patch do |form| %>
<%= render 'form', form: form %>
<%= form.submit t('views.admin.cups.pairs.edit.update'), class: 'btn-primary' %>
<% end %>
</section>
30 changes: 30 additions & 0 deletions app/views/controllers/admin/cups/pairs/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<div class="flex flex-row justify-between items-center">
<h1><%= t('views.admin.cups.pairs.index.header') %></h1>
<%= link_to t('views.admin.cups.pairs.index.new_cups_pair'), new_admin_cups_round_pair_path(cups_round_id: params[:cups_round_id]), class: 'btn-primary' %>
</div>
<table class="table mb-4" cellSpacing="0">
<thead>
<tr>
<th>ID</th>
<th><%= t('views.admin.cups.pairs.index.home_name') %></th>
<th><%= t('views.admin.cups.pairs.index.visitor_name') %></th>
<th><%= t('views.admin.cups.pairs.index.start_at') %></th>
<th><%= t('views.admin.cups.pairs.index.points') %></th>
<th></th>
</tr>
</thead>
<tbody>
<% @cups_pairs.each do |cups_pair| %>
<tr>
<td><%= cups_pair[:id] %></td>
<td><%= cups_pair[:home_name] %></td>
<td><%= cups_pair[:visitor_name] %></td>
<td><%= cups_pair[:start_at] %></td>
<td><%= cups_pair[:points]&.join('-') %></td>
<td>
<%= link_to t('views.admin.cups.pairs.index.edit'), edit_admin_cups_round_pair_path(cups_round_id: params[:cups_round_id], id: cups_pair[:id]), class: 'btn-primary btn-small' %>
</td>
</tr>
<% end %>
</tbody>
</table>
7 changes: 7 additions & 0 deletions app/views/controllers/admin/cups/pairs/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1><%= t('views.admin.cups.pairs.index.new_cups_pair') %></h1>
<section>
<%= form_with model: @cups_pair, url: admin_cups_round_pairs_path(cups_round_id: params[:cups_round_id]), method: :post do |form| %>
<%= render 'form', form: form %>
<%= form.submit t('views.admin.cups.pairs.new.create'), class: 'btn-primary' %>
<% end %>
</section>
2 changes: 1 addition & 1 deletion app/views/controllers/admin/cups/rounds/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<td><%= cups_round[:name] %></td>
<td><%= cups_round[:position] %></td>
<td>

<%= link_to t('views.admin.cups.rounds.index.pairs'), admin_cups_round_pairs_path(cups_round_id: cups_round[:id]), class: 'btn-primary btn-small' %>
</td>
</tr>
<% end %>
Expand Down
4 changes: 4 additions & 0 deletions config/initializers/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def register(key)
register('contracts.oracul') { OraculContract.new }
register('contracts.cup') { CupContract.new }
register('contracts.cups.round') { Cups::RoundContract.new }
register('contracts.cups.pair') { Cups::PairContract.new }

# validators
register('validators.games.create') { Games::CreateValidator.new }
Expand All @@ -68,6 +69,7 @@ def register(key)
register('validators.oracul') { OraculValidator.new }
register('validators.cup') { CupValidator.new }
register('validators.cups.round') { Cups::RoundValidator.new }
register('validators.cups.pair') { Cups::PairValidator.new }

# forms
register('forms.teams.players.create') { Teams::Players::CreateForm.new }
Expand All @@ -93,6 +95,8 @@ def register(key)
register('forms.oraculs.forecasts.update') { Oraculs::Forecasts::UpdateForm.new }
register('forms.cups.create') { Cups::CreateForm.new }
register('forms.cups.rounds.create') { Cups::Rounds::CreateForm.new }
register('forms.cups.pairs.create') { Cups::Pairs::CreateForm.new }
register('forms.cups.pairs.update') { Cups::Pairs::UpdateForm.new }

# notifiers
register('notifiers.telegram.user.deadline_report_payload') { Telegram::User::DeadlineReportPayload.new }
Expand Down
23 changes: 23 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ en:
rounds:
create:
success: Cups round is created
pairs:
create:
success: Cups pair is created
update:
success: Cups pair is updated
seasons:
create:
success: Season is created
Expand Down Expand Up @@ -126,6 +131,9 @@ en:
permission: Email is not confirmed yet
ban: Account is banned
services:
cups:
create:
league_does_not_exist: League does not exist
oraculs:
create:
not_unique: Oracul already exists
Expand Down Expand Up @@ -186,8 +194,23 @@ en:
new_cups_round: New cups round
name: Name
position: Position
pairs: Games
new:
create: Create new cups round
pairs:
index:
header: Round games
new_cups_pair: New round game
home_name: Home name
visitor_name: Visitor name
start_at: Start at
points: Points
edit: Edit
new:
create: Create new pair
edit:
title: Editing pair
update: Update pair
weeks:
index:
header: Weeks
Expand Down
23 changes: 23 additions & 0 deletions config/locales/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ ru:
rounds:
create:
success: Кубковый раунд создан
pairs:
create:
success: Кубковая игры создана
update:
success: Кубковая игры обновлена
seasons:
create:
success: Сезон создан
Expand Down Expand Up @@ -126,6 +131,9 @@ ru:
permission: Необходимо подтвердить электронную почту
ban: Аккаунт заблокирован
services:
cups:
create:
league_does_not_exist: Лига не существует
oraculs:
create:
not_unique: Оракл уже создан
Expand Down Expand Up @@ -186,8 +194,23 @@ ru:
new_cups_round: Новый кубковый раунд
name: Название
position: Положение
pairs: Игры
new:
create: Создать новый раунд
pairs:
index:
header: Игры раунда
new_cups_pair: Новая игры
home_name: Первое название
visitor_name: Второе название
start_at: Время начала
points: Результат
edit: Изменить
new:
create: Создать новую игру
edit:
title: Редактирование игры
update: Обновить игру
weeks:
index:
header: Игровые недели
Expand Down
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
resources :rounds, only: %i[index new create]
end
end
resources :cups_rounds, only: %i[] do
scope module: :cups do
resources :pairs, only: %i[index new edit create update]
end
end
resources :weeks, only: %i[index edit update]
end

Expand Down
Loading

0 comments on commit 277825f

Please sign in to comment.