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

Add more info to the competition API #8409

Merged
merged 11 commits into from
Oct 10, 2023
1 change: 1 addition & 0 deletions WcaOnRails/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ gem 'sprockets-rails'
gem 'fuzzy-string-match'
gem 'sidekiq'
gem 'sidekiq-cron'
gem 'deep_merge', require: 'deep_merge/rails_compat'

group :development, :test do
gem 'spring'
Expand Down
2 changes: 2 additions & 0 deletions WcaOnRails/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ GEM
date (3.3.3)
debug_inspector (1.0.0)
declarative (0.0.20)
deep_merge (1.2.2)
devise (4.9.2)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
Expand Down Expand Up @@ -775,6 +776,7 @@ DEPENDENCIES
daemons
database_cleaner
datetimepicker-rails!
deep_merge
devise
devise-bootstrap-views
devise-i18n
Expand Down
13 changes: 12 additions & 1 deletion WcaOnRails/app/controllers/api/v0/competitions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,20 @@ def index
paginate json: competitions
end

COMPETITION_INFO_SERIALIZE_OPTIONS = {
only: %w[extra_registration_requirements enable_donations refund_policy_limit_date event_change_deadline_date waiting_list_deadline_date on_the_spot_registration on_the_spot_entry_fee_lowest_denomination qualification_results
event_restrictions base_entry_fee_lowest_denomination currency_code allow_registration_edits allow_registration_self_delete_after_acceptance allow_registration_without_qualification refund_policy_percent
use_wca_registration guests_per_registration_limit venue contact force_comment_in_registration use_wca_registration external_registration_page guests_entry_fee_lowest_denomination guest_entry_status information],
methods: %w[registration_opened? main_event_id number_of_bookmarks using_stripe_payments? uses_qualification? uses_cutoff?],
include: %w[tabs],
}.freeze

def show
competition = competition_from_params
render json: competition

if stale?(competition)
render json: competition.as_json(COMPETITION_INFO_SERIALIZE_OPTIONS)
end
end

def schedule
Expand Down
9 changes: 8 additions & 1 deletion WcaOnRails/app/models/competition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,10 @@ def registration_full?
competitor_limit_enabled? && registrations.accepted_and_paid_pending_count >= competitor_limit
end

def number_of_bookmarks
bookmarked_users.count
end

def country
Country.c_find(self.countryId)
end
Expand Down Expand Up @@ -1911,7 +1915,10 @@ def url
}.freeze

def serializable_hash(options = nil)
json = super(DEFAULT_SERIALIZE_OPTIONS.merge(options || {}))
# This looks weird, but we need the 'deeper_merge' method to handle arrays inside hashes.
# In turn, the 'deeper_merge' library has a quirk that even though it doesn't use the ! naming convention,
# it tries to modify the source array in-place. This is not cool so we need to circumvent by duplicating.
json = super(DEFAULT_SERIALIZE_OPTIONS.deep_dup.deeper_merge(options || {}))
# Fallback to the default 'serializable_hash' method, but always include our
# custom 'class' attribute.
# We can't put that in our DEFAULT_SERIALIZE_OPTIONS because the 'class'
Expand Down