Skip to content

Commit 16fd9de

Browse files
committed
fix replacing asset while copying past data
Attempting to update the existing SponsorshipAssetFile by URL containing target conference slug would result into 404. Luckily it resulted into 404, it could overwrite existing data which is unhappy for organizers if we lack conference_slug in asset files controller. This patch properly handles 4 differenct scenario that sponsorships/_form can see: 1. new: sponsorship and asset_file is new, and not submitted yet. 2. copy: sponsorship is new and asset_file belongs to other conference. Happens when a sponsorship is sourced using past data. Never happens after #create, as it does s3:CopyObject and inserts new unclaimed SponsorshipAssetFile. 3. unclaimed: sponsorship is new. asset_file is persisted but unclaimed. Happens when a sponsorship is not valid on #create. 4. existing: sponsorship and asset_file are both persisted. > Level: Error Non-Error promise rejection captured with value: Uploader getSession failed: status=404 https://rubykaigi.sentry.io/issues/7053573751/events/5966851ed69e42fd9a976f807d7f2a69/
1 parent e042f6e commit 16fd9de

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

app/views/sponsorships/_form.html.haml

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,18 +199,42 @@
199199
%p= t('.logo_help')
200200
%p= t('.logo_zip_help')
201201

202-
.form-group.sponsorships_form_asset_file{data: {session_endpoint: sponsorship.asset_file&.persisted? ? initiate_update_user_conference_sponsorship_asset_file_path(conference, sponsorship.asset_file) : user_conference_sponsorship_asset_files_path, session_endpoint_method: 'POST'}}
203-
= form.hidden_field :asset_file_id
204-
.sponsorships_form_asset_file_form (Loading)
205-
206-
- if sponsorship.asset_file
207202
:ruby
208-
asset_file_url = user_conference_sponsorship_asset_file_path(conference, sponsorship.asset_file)
209-
unless sponsorship.persisted? # possibly copying from another sponsorship
210-
asset_file = SponsorshipAssetFile.includes(:sponsorship).find(sponsorship.asset_file_id)
211-
asset_file_url = user_conference_sponsorship_asset_file_path(asset_file.sponsorship&.conference || conference, asset_file)
203+
asset_file_state = :new
204+
if sponsorship.asset_file&.persisted?
205+
asset_file_state = :existing
206+
asset_file_url = user_conference_sponsorship_asset_file_path(conference, sponsorship.asset_file)
207+
# Copying from another sponsorship (:copy) or failed validation (:unclaimed)
208+
if sponsorship.new_record?
209+
# We need to query it again as foo.assoc=other rewrites other.foo
210+
asset_sponsorship = Sponsorship.includes(:asset_file, :conference).select(:id, :conference_id).where(asset_file: {id: sponsorship.asset_file_id}).first
211+
claimed_asset_file = asset_sponsorship&.asset_file
212+
if claimed_asset_file
213+
asset_file_state = :copy
214+
asset_file_url = user_conference_sponsorship_asset_file_path(asset_sponsorship.conference, claimed_asset_file)
215+
raise "asset_file_state=:copy, assertion failed, conference_id must differ when copy; asset_file_id=#{sponsorship.asset_file.id}" unless asset_sponsorship.conference_id != conference.id
216+
else
217+
asset_file_state = :unclaimed
218+
asset_file_url = user_conference_sponsorship_asset_file_path(conference, sponsorship.asset_file)
219+
end
220+
end
221+
end
222+
223+
form_params = case asset_file_state
224+
when :new, :copy
225+
{session_endpoint: user_conference_sponsorship_asset_files_path, session_endpoint_method: 'POST', asset_file_state: asset_file_state.to_s}
226+
when :existing, :unclaimed
227+
{session_endpoint: initiate_update_user_conference_sponsorship_asset_file_path(conference, sponsorship.asset_file), session_endpoint_method: 'POST', asset_file_state: asset_file_state.to_s}
228+
else
229+
raise "Unknown asset_file_state: #{asset_file_state}, sponsorship_asset_file_id: #{sponsorship.asset_file.id}"
212230
end
213-
%small.form-text= link_to t('.download_logo'), asset_file_url
231+
232+
.form-group.sponsorships_form_asset_file{data: form_params}
233+
= form.hidden_field :asset_file_id
234+
.sponsorships_form_asset_file_form (Loading)
235+
236+
- if sponsorship.asset_file
237+
%small.form-text= link_to t('.download_logo'), asset_file_url
214238

215239
%div.py-3.sponsorships_form_asset_file_form__warning.d-none{data: {warning_kind: 'zip_asset'}}
216240
.card

0 commit comments

Comments
 (0)