Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement adding registrations via form #4392

Merged
merged 2 commits into from Aug 4, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -58,3 +58,13 @@ function compareHtmlContent(a, b) {
var second = $('<p>' + b + '</p>').text().trim();
return first.localeCompare(second);
}

onPage('registrations#add, registrations#do_add', function() {
// Bind all/clear cubing event buttons
$('#clear-all-events').on('click', function() {
$('#events input[type="checkbox"]').prop('checked', false);
});
$('#select-all-events').on('click', function() {
$('#events input[type="checkbox"]').prop('checked', true);
});
});
@@ -17,7 +17,10 @@ class RegistrationsController < ApplicationController
competition
end

before_action :competition_must_be_using_wca_registration!, except: [:import, :do_import, :index, :psych_sheet, :psych_sheet_event]
before_action -> { redirect_to_root_unless_user(:can_manage_competition?, competition_from_params) },
only: [:edit_registrations, :do_actions_for_selected, :edit, :refund_payment, :import, :do_import, :add, :do_add]

before_action :competition_must_be_using_wca_registration!, except: [:import, :do_import, :add, :do_add, :index, :psych_sheet, :psych_sheet_event]
private def competition_must_be_using_wca_registration!
if !competition_from_params.use_wca_registration?
flash[:danger] = I18n.t('registrations.flash.not_using_wca')
@@ -32,8 +35,6 @@ class RegistrationsController < ApplicationController
end
end

before_action -> { redirect_to_root_unless_user(:can_manage_competition?, competition_from_params) }, only: [:edit_registrations, :do_actions_for_selected, :edit, :refund_payment]

def edit_registrations
@show_events = params[:show_events] == "true"
@show_full_mail = params[:show_email] == "true"
@@ -166,6 +167,38 @@ def do_import
redirect_to competition_registrations_import_url(competition)
end

def add
@competition = competition_from_params
end

def do_add
@competition = competition_from_params
if @competition.registration_full?

This comment has been minimized.

Copy link
@jfly

jfly Aug 4, 2019

Member

Is there a race condition here if there are two registrations happening simultaneously where only 1 should happen, but they both do? I'm not too familiar with the competition limit enforcement code, so this problem very well may exist in other places. If the problem does exist in other places, I'm ok with waiting to take care of this another time (maybe as part of #3451?)

This comment has been minimized.

Copy link
@jonatanklosko

jonatanklosko Aug 4, 2019

Author Member

There most likely is, although in this case I believe it's unlikely enough (definitely less likely than in case of a registration process with multiple people registering). Absolutely something we can work on separately.

flash[:danger] = "The competitor limit has been reached."
redirect_to competition_path(@competition)
return
end
ActiveRecord::Base.transaction do
user, locked_account_created = user_for_registration!(params[:registration_data])
registration = @competition.registrations.find_or_initialize_by(user_id: user.id)
raise "This person already has a registration." unless registration.new_record?
registration.assign_attributes(accepted_at: Time.now, accepted_by: current_user.id)
params[:registration_data][:event_ids]&.each do |event_id|
competition_event = @competition.competition_events.find { |ce| ce.event_id == event_id }
registration.registration_competition_events.build(competition_event_id: competition_event.id)
end
registration.save!
if locked_account_created
RegistrationsMailer.notify_registrant_of_locked_account_creation(user, @competition).deliver_later
end
end
flash[:success] = "Successfully added registration!"
redirect_to competition_registrations_add_url(@competition)
rescue StandardError => e
flash.now[:danger] = e.to_s
render :add
end

private def user_for_registration!(registration_row)
registration_row[:wca_id]&.upcase!
registration_row[:email]&.downcase!
@@ -0,0 +1,52 @@
<% provide(:title, t('registrations.add.title', comp: @competition.name)) %>

<%= render layout: "nav" do %>
<% if @competition.registration_full? %>
<%= alert :warning, t('registrations.mailer.deleted.causes.registrations_full'), note: true %>
<% else %>
<%= alert :info, t('registrations.add.info'), note: true %>
<%= simple_form_for :registration_data, url: competition_registrations_do_add_path(@competition) do |f| %>
<%= f.input :name, input_html: { value: params.dig(:registration_data, :name) },
label: t('activerecord.attributes.user.name') %>
<%= f.input :country, selected: params.dig(:registration_data, :country),
include_blank: true,
collection: Country.real,
value_method: lambda { |c| c.id },
label_method: lambda { |c| c.name },
label: t('activerecord.attributes.user.country_iso2') %>
<%= f.input :birth_date, as: :date_picker,
input_html: { value: params.dig(:registration_data, :birth_date) },
label: t('activerecord.attributes.user.dob') %>
<%= f.input :gender, selected: params.dig(:registration_data, :gender), #
include_blank: true,
collection: User::ALLOWABLE_GENDERS,
label_method: User::GENDER_LABEL_METHOD,
label: t('activerecord.attributes.user.gender') %>
<%= f.input :email, input_html: { value: params.dig(:registration_data, :email) },
label: t('activerecord.attributes.user.email') %>
<%= f.input :wca_id, as: :wca_id,
input_html: { value: params.dig(:registration_data, :wca_id) },
label: t('activerecord.attributes.user.wca_id') %>

<div class="form-group">
<%= label_tag "events" do %>
<%= t 'competitions.competition_form.events' %>
<button type="button" id="select-all-events" class="btn btn-primary btn-xs"><%= t 'competitions.index.all_events' %></button>
<button type="button" id="clear-all-events" class="btn btn-default btn-xs"><%= t 'competitions.index.clear' %></button>
<% end %>
<div id="events">
<% @competition.events.each do |event| %>
<span class="event-checkbox">
<%= label_tag "checkbox-#{event.id}" do %>
<%= check_box_tag "registration_data[event_ids][]", event.id, params.dig(:registration_data, :event_ids)&.include?(event.id), id: "checkbox-#{event.id}" %>
<%= cubing_icon event.id, data: { toggle: "tooltip", placement: "top" }, title: event.name %>
<% end %>
</span>
<% end %>
</div>
</div>

<%= f.submit t('registrations.add.add'), class: "btn btn-primary" %>
<% end %>
<% end %>
<% end %>
@@ -1016,6 +1016,10 @@ en:
registrations_file_label: "Registrations file"
registrations_file_hint: "CSV file with columns: Status, Name, Country, WCA ID, Birth Date, Gender, Email, and one column for each event."
import: "Import"
add:
title: "Add registration for %{comp}"
info: "This will create an approved registration. Use this form to add walk-ins during the competition."
add: "Add registration"
#context: Key used when managing competitions
competitions:
#context: generic message
@@ -44,6 +44,8 @@
post 'registrations/export' => 'registrations#export', as: :registrations_export
get 'registrations/import' => 'registrations#import', as: :registrations_import
post 'registrations/import' => 'registrations#do_import', as: :registrations_do_import
get 'registrations/add' => 'registrations#add', as: :registrations_add
post 'registrations/add' => 'registrations#do_add', as: :registrations_do_add
get 'registrations/psych-sheet' => 'registrations#psych_sheet', as: :psych_sheet
get 'registrations/psych-sheet/:event_id' => 'registrations#psych_sheet_event', as: :psych_sheet_event
resources :registrations, only: [:index, :update, :create, :edit, :destroy], shallow: true
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.