class RsvpsController < ApplicationController before_action :logged_in before_action :check_user_has_questionnaire before_action :find_questionnaire before_action :require_accepted_questionnaire layout 'hackathon_manager/application' def logged_in authenticate_user! end # GET /rsvp def show end # GET /rsvp/accept def accept @questionnaire.acc_status = "rsvp_confirmed" @questionnaire.acc_status_author_id = current_user.id @questionnaire.acc_status_date = Time.now if @questionnaire.save flash[:notice] = "Thank you for confirming your attendance! You're all set to attend." flash[:notice] += " See below for additional bus information." if BusList.any? else flash[:notice] = rsvp_error_notice end redirect_to rsvp_path end # GET /rsvp/deny def deny @questionnaire.acc_status = "rsvp_denied" @questionnaire.acc_status_author_id = current_user.id @questionnaire.acc_status_date = Time.now unless @questionnaire.save flash[:notice] = rsvp_error_notice end flash[:notice] = "Your RSVP has been updated." if flash[:notice].blank? redirect_to rsvp_path end # PUT /rsvp # rubocop:disable CyclomaticComplexity # rubocop:disable PerceivedComplexity def update unless @questionnaire.update_attributes(params.require(:questionnaire).permit(:agreement_accepted, :phone)) flash[:notice] = @questionnaire.errors.full_messages.join(", ") redirect_to rsvp_path return end unless ["rsvp_confirmed", "rsvp_denied"].include? params[:questionnaire][:acc_status] flash[:notice] = "Please select a RSVP status." redirect_to rsvp_path return end @questionnaire.acc_status_date = Time.now if @questionnaire.acc_status != params[:questionnaire][:acc_status] @questionnaire.acc_status = params[:questionnaire][:acc_status] @questionnaire.acc_status_author_id = current_user.id bus_list_id = params[:questionnaire][:bus_list_id].presence bus_list = bus_list_id && BusList.find(bus_list_id) is_joining_bus = @questionnaire.bus_list != bus_list if is_joining_bus && bus_list.full? if @questionnaire.bus_list_id? flash[:notice] = "Sorry, that bus is full. You are still signed up for the '#{@questionnaire.bus_list.name}' bus." else flash[:notice] = "Sorry, that bus is full. You may need to arrange other plans for transportation." end else @questionnaire.bus_list = bus_list @questionnaire.bus_captain_interest = params[:questionnaire][:bus_captain_interest] end unless @questionnaire.save flash[:notice] = @questionnaire.errors.full_message.join(", ") redirect_to rsvp_path return end flash[:notice] = "Your RSVP has been updated." if flash[:notice].blank? flash[:notice] += " See below for additional bus information!" if @questionnaire.bus_list_id? redirect_to rsvp_path end # rubocop:enable CyclomaticComplexity # rubocop:enable PerceivedComplexity private def rsvp_error_notice hackathon_name = Rails.configuration.hackathon['name'] "There was an error submitting your response, please check over your application and try again. Did you accept the #{hackathon_name} Agreement?" end def find_questionnaire @questionnaire = current_user.questionnaire end def check_user_has_questionnaire redirect_to new_questionnaires_path if current_user.questionnaire.nil? end def require_accepted_questionnaire return if @questionnaire.can_rsvp? || @questionnaire.checked_in? redirect_to new_questionnaires_path end end