Skip to content

Commit

Permalink
Add more info to the competition API (#8409)
Browse files Browse the repository at this point in the history
* Add more info to the competition API

* Added stale? check

* review changes

* run rubocop

* make serialization options three lines

* remove schedule_wcif and evens_with_rounds because we will get it out of wcif instead

* trying with deep_merge

* add block to merge

* correctly put the block into the super

* Use deep_merge gem for merging serialization options

* change bookmarked_users to count

---------

Co-authored-by: Gregor Billing <[email protected]>
  • Loading branch information
FinnIckler and gregorbg authored Oct 10, 2023
1 parent 979f0bb commit 116dcd1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
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

0 comments on commit 116dcd1

Please sign in to comment.