diff --git a/WcaOnRails/Gemfile b/WcaOnRails/Gemfile index 8a67aac42b..06d9ceabfe 100644 --- a/WcaOnRails/Gemfile +++ b/WcaOnRails/Gemfile @@ -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' diff --git a/WcaOnRails/Gemfile.lock b/WcaOnRails/Gemfile.lock index 1a9fdcf75a..b2eb761b5c 100644 --- a/WcaOnRails/Gemfile.lock +++ b/WcaOnRails/Gemfile.lock @@ -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) @@ -775,6 +776,7 @@ DEPENDENCIES daemons database_cleaner datetimepicker-rails! + deep_merge devise devise-bootstrap-views devise-i18n diff --git a/WcaOnRails/app/controllers/api/v0/competitions_controller.rb b/WcaOnRails/app/controllers/api/v0/competitions_controller.rb index 8719a6b8ee..87466e9e74 100644 --- a/WcaOnRails/app/controllers/api/v0/competitions_controller.rb +++ b/WcaOnRails/app/controllers/api/v0/competitions_controller.rb @@ -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 diff --git a/WcaOnRails/app/models/competition.rb b/WcaOnRails/app/models/competition.rb index 63be0101e2..dac3f01b5b 100644 --- a/WcaOnRails/app/models/competition.rb +++ b/WcaOnRails/app/models/competition.rb @@ -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 @@ -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'