Skip to content

Commit

Permalink
Validate that each event has at least a round
Browse files Browse the repository at this point in the history
  • Loading branch information
viroulep committed Jan 3, 2019
1 parent 83e9002 commit f8685a5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion WcaOnRails/app/controllers/competitions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class CompetitionsController < ApplicationController

PAST_COMPETITIONS_DAYS = 90
CHECK_SCHEDULE_ASSOCIATIONS = {
rounds: [:competition_event],
competition_events: [:rounds],
competition_venues: {
venue_rooms: {
schedule_activities: [:child_activities],
Expand Down
13 changes: 9 additions & 4 deletions WcaOnRails/app/models/competition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ class Competition < ApplicationRecord

validates :registration_open, :registration_close, presence: { message: I18n.t('simple_form.required.text') }, if: :registration_period_required?

# NOTE: we only validate when confirming, until we have a unified events/rounds editor.
# If we would validate everytime, changing the number of rounds for
# competition wouldn't be possible: adding rounds through the events page
# couldn't be possible because the schedule doesn't contain the round
# just added.
validate :must_have_at_least_one_event, if: :confirmed_or_visible?
private def must_have_at_least_one_event
if no_events?
Expand All @@ -204,13 +209,13 @@ class Competition < ApplicationRecord
# The only exception to this is within tests, in which case we actually don't want to run this validation.
validate :schedule_must_match_rounds, if: :confirmed_at_changed?, on: :update
def schedule_must_match_rounds
unless has_round? && schedule_includes_rounds?
unless has_any_round_per_event? && schedule_includes_rounds?
errors.add(:competition_events, I18n.t('competitions.errors.schedule_must_match_rounds'))
end
end

def has_round?
rounds.any?
def has_any_round_per_event?
competition_events.map(&:rounds).none?(&:empty?)
end

def schedule_includes_rounds?
Expand Down Expand Up @@ -320,7 +325,7 @@ def warnings_for(user)
# NOTE: this will show up on the edit schedule page, and stay even if the
# schedule matches when saved. Should we add some logic to not show this
# message on the edit schedule page?
unless has_round? && schedule_includes_rounds?
unless has_any_round_per_event? && schedule_includes_rounds?
warnings[:schedule] = I18n.t('competitions.messages.schedule_must_match_rounds')
end
end
Expand Down
2 changes: 1 addition & 1 deletion WcaOnRails/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ en:
not_visible: "This competition is not visible to the public."
name_too_long: "The competition name is longer than 32 characters. We prefer shorter ones and we will be glad if you change it."
must_have_events: "Please add at least one event before confirming the competition."
schedule_must_match_rounds: "Please make sure the competition has at least one round and that the schedule you created includes all the rounds you declared before confirming the competition."
schedule_must_match_rounds: "Please make sure the competition has at least one round per event and that the schedule you created includes all the rounds you declared before confirming the competition."
create_success: "Successfully created new competition!"
results_preview_alert: "You are viewing results that have not been posted yet."
upload_results: "This competition is over, we are working to upload the results as soon as possible!"
Expand Down

0 comments on commit f8685a5

Please sign in to comment.