Skip to content

Commit

Permalink
Merge pull request #56 from armerpaul/players-can-update-registration
Browse files Browse the repository at this point in the history
Players can update registration
  • Loading branch information
plural authored May 12, 2022
2 parents c49dd1d + b20ca07 commit b4b183f
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 84 deletions.
8 changes: 6 additions & 2 deletions app/controllers/players_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ def create
end

def update
authorize @tournament, :update?
authorize @tournament, :register?

@player.update(player_params)

redirect_to tournament_players_path(@tournament)
if current_user.id == @tournament.user_id
redirect_to tournament_players_path(@tournament)
else
redirect_to tournament_path(@tournament)
end
end

def destroy
Expand Down
28 changes: 19 additions & 9 deletions app/controllers/tournaments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class TournamentsController < ApplicationController
before_action :set_tournament, only: [
:show, :edit, :update, :destroy,
:upload_to_abr, :save_json, :cut, :qr
:upload_to_abr, :save_json, :cut, :qr, :registration
]

def index
Expand All @@ -23,14 +23,7 @@ def show

respond_to do |format|
format.html do
@players = @tournament.players.active.sort_by { |p| p.name || '' }
@dropped = @tournament.players.dropped.sort_by { |p| p.name || '' }

if current_user
@current_user_is_running_tournament = @tournament.user_id == current_user.id
@current_user_player = @players.find { |p| p.user_id == current_user.id }
@current_user_dropped = @dropped.any? { |p| p.user_id == current_user.id }
end
set_tournament_view_data
end
format.json do
headers['Access-Control-Allow-Origin'] = '*'
Expand All @@ -39,6 +32,23 @@ def show
end
end

def registration
authorize @tournament, :register?

set_tournament_view_data
end

def set_tournament_view_data
@players = @tournament.players.active.sort_by { |p| p.name || '' }
@dropped = @tournament.players.dropped.sort_by { |p| p.name || '' }

if current_user
@current_user_is_running_tournament = @tournament.user_id == current_user.id
@current_user_player = @players.find { |p| p.user_id == current_user.id }
@current_user_dropped = @dropped.any? { |p| p.user_id == current_user.id }
end
end

def new
authorize Tournament

Expand Down
4 changes: 4 additions & 0 deletions app/policies/tournament_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def upload_to_abr?
update?
end

def register?
record.self_registration || update?
end

def save_json?
show?
end
Expand Down
26 changes: 26 additions & 0 deletions app/views/tournaments/_overview_card.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.col-md-6
.card
ul.list-group.list-group-flush
- if @tournament.slug
li.list-group-item
.small.text-secondary Shortcode:
| #{@tournament.slug} (
= link_to tournament_url(@tournament.slug, request), tournament_url(@tournament.slug, request)
| )

li.list-group-item
.small.text-secondary Organiser:
| #{@tournament.user.nrdb_username}
li.list-group-item
.small.text-secondary Players:
=> pluralize(@players.count, 'active player')
| (#{@dropped.count} dropped)
- if policy(@tournament).edit?
li.list-group-item
.small.text-secondary
| QR Code:
.row
.col-sm-6
= link_to qr_tournament_path(@tournament), :target => '_blank' do
=> fa_icon 'qrcode'
| Open Printable QR Code
24 changes: 24 additions & 0 deletions app/views/tournaments/_player_counts.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
- if @tournament.rounds.any? || policy(@tournament).edit?
.col-12.mt-3.row
.col-md-6
table.table
thead
tr
th Corp
th Players
tbody
- @tournament.corp_counts.each do |id, count|
tr
td= render id
td= count
.col-md-6
table.table
thead
tr
th Runner
th Players
tbody
- @tournament.runner_counts.each do |id, count|
tr
td= render id
td= count
20 changes: 20 additions & 0 deletions app/views/tournaments/registration.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
= render 'overview_card'

.col-md-6
.card
.card-header
.d-flex.justify-content-between
h5.mb-0 My Registration Information
= link_to tournament_path(@tournament), title: 'Cancel', class: 'float-right' do
=> fa_icon 'undo'
span.d-none Cancel registration edits
.card-body
= simple_form_for @current_user_player, url: tournament_player_path(@tournament, @current_user_player) do |f|
= render 'players/form', f: f
.text-right
= button_tag type: :submit, class: 'btn btn-primary' do
=> fa_icon 'check'
| Update


= render 'player_counts'
100 changes: 27 additions & 73 deletions app/views/tournaments/show.html.slim
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
.col-md-6
.card
ul.list-group.list-group-flush
- if @tournament.slug
li.list-group-item
.small.text-secondary Shortcode:
| #{@tournament.slug} (
= link_to tournament_url(@tournament.slug, request), tournament_url(@tournament.slug, request)
| )

li.list-group-item
.small.text-secondary Organiser:
| #{@tournament.user.nrdb_username}
li.list-group-item
.small.text-secondary Players:
=> pluralize(@players.count, 'active player')
| (#{@dropped.count} dropped)
- if policy(@tournament).edit?
li.list-group-item
.small.text-secondary
| QR Code:
.row
.col-sm-6
= link_to qr_tournament_path(@tournament), :target => '_blank' do
=> fa_icon 'qrcode'
| Open Printable QR Code
= render 'overview_card'

.col-md-6
- if @current_user_player
.card
.card-header
h5.card-title.mb-0 My Registration Information
.d-flex.justify-content-between
h5.mb-0 My Registration Information
- if policy(@tournament).register?
= link_to registration_tournament_path(@tournament), title: 'Edit', class: 'float-right' do
=> fa_icon 'pencil'
span.d-none Edit my registration information
ul.list-group.list-group-flush
li.list-group-item
.small.text-secondary Name:
Expand All @@ -49,27 +29,25 @@
- else
- if @tournament.self_registration?
- if current_user
- unless @current_user_player
.card.alert.alert-secondary
.card-body
- unless @current_user_dropped
h5.card-title Register for this Event
= simple_form_for :player, url: tournament_players_path(@tournament) do |f|
= render 'players/form', f: f, default_name: current_user ? current_user.nrdb_username : ''
.text-right
= button_tag type: :submit, class: 'btn btn-primary', disabled: (not current_user) do
=> fa_icon 'user-plus'
| Register
- else
h5.card-title Rejoin this Event
- if @current_user_is_running_tournament
p
| You can re-instate yourself on the
=<> link_to tournament_players_path(@tournament) do
| Players
| tab.
- else
p Talk to a Tournmanet Organiser to rejoin the event
.card.alert.alert-secondary
- unless @current_user_dropped
h5.card-title Register for this Event
= simple_form_for :player, url: tournament_players_path(@tournament) do |f|
= render 'players/form', f: f, default_name: current_user ? current_user.nrdb_username : ''
.text-right
= button_tag type: :submit, class: 'btn btn-primary', disabled: (not current_user) do
=> fa_icon 'user-plus'
| Register
- else
h5.card-title Rejoin this Event
- if @current_user_is_running_tournament
p
| You can re-instate yourself on the
=<> link_to tournament_players_path(@tournament) do
| Players
| tab.
- else
p Talk to a Tournmanet Organiser to rejoin the event
- else
.card.alert.alert-warning
.card-body
Expand All @@ -84,28 +62,4 @@
span.icon.icon-link
| &nbsp;Create NRDB account


- if @tournament.rounds.any? || policy(@tournament).edit?
.col-12.mt-3.row
.col-md-6
table.table
thead
tr
th Corp
th Players
tbody
- @tournament.corp_counts.each do |id, count|
tr
td= render id
td= count
.col-md-6
table.table
thead
tr
th Runner
th Players
tbody
- @tournament.runner_counts.each do |id, count|
tr
td= render id
td= count
= render 'player_counts'
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
get :save_json, on: :member
post :cut, on: :member
get :qr, on: :member
get :registration, on: :member
get :shortlink, on: :collection
get :not_found, on: :collection
get :my, on: :collection
Expand Down

0 comments on commit b4b183f

Please sign in to comment.