-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added rendering/creating/editing Cups::Pair
- Loading branch information
Showing
16 changed files
with
448 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.