Skip to content

Commit

Permalink
serialization: restore initial behavior (#8418)
Browse files Browse the repository at this point in the history
* serialization: restore initial behavior

This also explicitly lists all expected attributes when "showing" a
competition's json.

* test: Add a test for the posting check in
  • Loading branch information
viroulep authored Oct 13, 2023
1 parent fc4b65d commit 2f12efb
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 16 deletions.
1 change: 0 additions & 1 deletion WcaOnRails/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ 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: 0 additions & 2 deletions WcaOnRails/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ 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 @@ -776,7 +775,6 @@ DEPENDENCIES
daemons
database_cleaner
datetimepicker-rails!
deep_merge
devise
devise-bootstrap-views
devise-i18n
Expand Down
21 changes: 12 additions & 9 deletions WcaOnRails/app/controllers/api/v0/competitions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@ 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

if stale?(competition)
render json: competition.as_json(COMPETITION_INFO_SERIALIZE_OPTIONS)
options = {
only: %w[id name website start_date registration_open registration_close
announced_at cancelled_at end_date competitor_limit
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[url website short_name city venue_address venue_details latitude_degrees
longitude_degrees country_iso2 event_ids
registration_opened? main_event_id number_of_bookmarks using_stripe_payments? uses_qualification? uses_cutoff?],
include: %w[delegates organizers tabs],
}
render json: competition.as_json(options)
end
end

Expand Down
10 changes: 6 additions & 4 deletions WcaOnRails/app/models/competition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1915,10 +1915,12 @@ def url
}.freeze

def serializable_hash(options = nil)
# 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 || {}))
# The intent behind this is to have a "good" default setup for serialization.
# We also want the caller to be able to be picky about the attributes included
# in the json (eg: specify an empty 'methods' to remove these attributes,
# or set a custom array in 'only' without getting the default ones), therefore
# we only use 'merge' here, which doesn't "deeply" merge into the default options.
json = super(DEFAULT_SERIALIZE_OPTIONS.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
26 changes: 26 additions & 0 deletions WcaOnRails/spec/requests/admin_results_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe "Admin Results" do
describe "Posting Check In" do
let!(:competition) { FactoryBot.create :competition, :with_valid_submitted_results }
let!(:wrt_member) { FactoryBot.create :user, :wrt_member }

it "locks a competition and returns the correct attributes" do
sign_in wrt_member
post start_posting_path, params: {
'competition_ids' => [competition.id],
}
expect(response).to be_successful
response_json = JSON.parse(response.body)
expect(response_json["message"]).to eq "Competitions successfully locked, go on posting!"
get results_posting_dashboard_path(format: :json)
expect(response).to be_successful
competitions = JSON.parse(response.body)["competitions"]
expect(competitions.size).to eq 1
expect(competitions[0]["id"]).to eq competition.id
expect(competitions[0]["posting_user"]["id"]).to eq wrt_member.id
end
end
end

0 comments on commit 2f12efb

Please sign in to comment.