Skip to content

Commit

Permalink
Fix bug preventing the creation of CFPs
Browse files Browse the repository at this point in the history
continues #14
  • Loading branch information
AndrewKvalheim committed Jun 13, 2021
1 parent e0c5646 commit d44be85
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
28 changes: 9 additions & 19 deletions app/controllers/admin/cfps_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def new
def edit; end

def create
@cfp = @program.build_cfp(cfp_params)
@cfp = Cfp.new(cfp_params)
send_mail_on_cfp_dates_updates = @cfp.notify_on_cfp_date_update?

if @cfp.save
Expand Down Expand Up @@ -54,24 +54,14 @@ def destroy
private

def cfp_params
# Do a bit of param massaging.
# 1. For some reason fields_for @cfp.program in the view produces a slightly
# wrong parameter name that can't be used with assign/update_attributes.
# Fix this by renaming :program to :program_attributes.
# 2. Starting with a CFP model object such as
# @cfp = #<Cfp id: 1, ... , program_id: 1>
# running .update_attributes on that with {... "program_attributes"=>{"speaker_proposals_req_speaker_event_reg"=>"true"}}
# should simply update the program associated with that CFP. Unfortunately,
# it also sets @cfp.program_id to nil. Not clear why as update_attributes
# is supposed to merge rather than replace, but the workaround is to inject
# the @cfp's program_id into the incoming parameters so the link is not lost.

cfp = params.require(:cfp)
cfp[:program_attributes] = cfp.delete(:program) if cfp.has_key? :program
cfp[:program_attributes][:id] = @cfp.program.id

cfp.permit(:start_date, :end_date,
program_attributes: [ :id, :speaker_proposals_req_speaker_event_reg, :speaker_proposals_req_bio, :speaker_proposals_req_subtitle, :speaker_proposals_req_commercial, :speaker_proposals_req_track, :speaker_proposals_req_difficulty_level])
params.require(:cfp).permit(:start_date, :end_date,
program_attributes: %i[id
speaker_proposals_req_bio
speaker_proposals_req_commercial
speaker_proposals_req_difficulty_level
speaker_proposals_req_speaker_event_reg
speaker_proposals_req_subtitle
speaker_proposals_req_track])
end
end
end
9 changes: 8 additions & 1 deletion app/models/cfp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class Cfp < ActiveRecord::Base
has_paper_trail ignore: [:updated_at], meta: { conference_id: :conference_id }
belongs_to :program
accepts_nested_attributes_for :program
accepts_nested_attributes_for :program, update_only: true

validates :program_id, presence: true
validates :start_date, :end_date, presence: true
Expand Down Expand Up @@ -56,6 +56,13 @@ def remaining_days(date = Date.today)
result > 0 ? result : 0
end

# Workaround for https://stackoverflow.com/q/6346134
def program_attributes=(attributes)
self.program = Program.find(attributes[:id]) if attributes[:id].present?

assign_nested_attributes_for_one_to_one_association(:program, attributes) # super
end

private

def before_end_of_conference
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/cfps/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
= semantic_form_for(@cfp, url: admin_conference_program_cfp_path(@conference.short_title), html: {multipart: true}) do |f|
= f.input :start_date, as: :string, input_html: { id: 'registration-period-start-datepicker', start_date: @conference.start_date, end_date: @conference.end_date, readonly: 'readonly' }
= f.input :end_date, as: :string, input_html: { id: 'registration-period-end-datepicker', readonly: 'readonly' }
= f.fields_for @cfp.program do |p|
= f.fields_for :program do |p|
= p.input :speaker_proposals_req_speaker_event_reg, as: :radio, label: 'Speakers todo: remind to register for event', input_html: { id: 'speaker_proposals_req_speaker_event_reg'}
= p.input :speaker_proposals_req_bio, as: :radio, label: 'Speakers todo: remind to fill out bio', input_html: { id: 'speaker_proposals_req_bio'}
= p.input :speaker_proposals_req_subtitle, as: :radio, label: 'Speakers todo: remind to add subtitle', input_html: { id: 'speaker_proposals_req_subtitle'}
Expand Down

0 comments on commit d44be85

Please sign in to comment.