From b20ca0711277bb99efee7cd5722f758b681f173b Mon Sep 17 00:00:00 2001 From: Paul Armer Date: Thu, 14 Apr 2022 23:26:19 -0700 Subject: [PATCH] make registration route --- app/controllers/players_controller.rb | 8 +- app/controllers/tournaments_controller.rb | 28 +++-- app/policies/tournament_policy.rb | 4 + .../tournaments/_overview_card.html.slim | 26 +++++ .../tournaments/_player_counts.html.slim | 24 +++++ app/views/tournaments/registration.html.slim | 20 ++++ app/views/tournaments/show.html.slim | 100 +++++------------- config/routes.rb | 1 + 8 files changed, 127 insertions(+), 84 deletions(-) create mode 100644 app/views/tournaments/_overview_card.html.slim create mode 100644 app/views/tournaments/_player_counts.html.slim create mode 100644 app/views/tournaments/registration.html.slim diff --git a/app/controllers/players_controller.rb b/app/controllers/players_controller.rb index 99470228..9c1c52f3 100644 --- a/app/controllers/players_controller.rb +++ b/app/controllers/players_controller.rb @@ -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 diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index 30b51c10..5ed73cf6 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -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 @@ -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'] = '*' @@ -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 diff --git a/app/policies/tournament_policy.rb b/app/policies/tournament_policy.rb index 06cc435a..b9147a1a 100644 --- a/app/policies/tournament_policy.rb +++ b/app/policies/tournament_policy.rb @@ -19,6 +19,10 @@ def upload_to_abr? update? end + def register? + record.self_registration || update? + end + def save_json? show? end diff --git a/app/views/tournaments/_overview_card.html.slim b/app/views/tournaments/_overview_card.html.slim new file mode 100644 index 00000000..90e9e0b2 --- /dev/null +++ b/app/views/tournaments/_overview_card.html.slim @@ -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 \ No newline at end of file diff --git a/app/views/tournaments/_player_counts.html.slim b/app/views/tournaments/_player_counts.html.slim new file mode 100644 index 00000000..60f8a168 --- /dev/null +++ b/app/views/tournaments/_player_counts.html.slim @@ -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 \ No newline at end of file diff --git a/app/views/tournaments/registration.html.slim b/app/views/tournaments/registration.html.slim new file mode 100644 index 00000000..95384205 --- /dev/null +++ b/app/views/tournaments/registration.html.slim @@ -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' \ No newline at end of file diff --git a/app/views/tournaments/show.html.slim b/app/views/tournaments/show.html.slim index 3b0236d8..4a274d54 100644 --- a/app/views/tournaments/show.html.slim +++ b/app/views/tournaments/show.html.slim @@ -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: @@ -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 @@ -84,28 +62,4 @@ span.icon.icon-link |  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' \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 76a404ca..db54ef8c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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