Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(questionnaire): directors can update questionnaires with invalid information #601

Closed
wants to merge 10 commits into from
8 changes: 8 additions & 0 deletions app/assets/javascripts/manage/lib/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ var setupManageForms = function() {
updateMessageForm();
});
};

document.addEventListener('turbolinks:load', function () {
JeremyRudman marked this conversation as resolved.
Show resolved Hide resolved
$('#validateFormSwitch').val($('#validateSwitch').is(':checked'));
$('#validateSwitch').on('change', function () {
JeremyRudman marked this conversation as resolved.
Show resolved Hide resolved
$('#validateFormSwitch').val($('#validateSwitch').is(':checked'));
})
JeremyRudman marked this conversation as resolved.
Show resolved Hide resolved
});

8 changes: 6 additions & 2 deletions app/controllers/manage/questionnaires_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ def update
@questionnaire.user.update_attributes(email: email) if email.present?
update_params = convert_school_name_to_id(update_params)
update_params = convert_boarded_bus_param(update_params, @questionnaire)
@questionnaire.update_attributes(update_params)
if update_params[:validate_switch] == 'false'
@questionnaire.update_with_invalid_attributes(update_params)
else
@questionnaire.update_attributes(update_params)
end
respond_with(:manage, @questionnaire)
end

Expand Down Expand Up @@ -157,7 +161,7 @@ def questionnaire_params
:portfolio_url, :vcs_url, :bus_captain_interest, :phone, :can_share_info,
:travel_not_from_school, :travel_location,
:graduation_year, :race_ethnicity, :resume, :delete_resume, :why_attend,
:bus_list_id, :is_bus_captain, :boarded_bus
:bus_list_id, :is_bus_captain, :boarded_bus, :country, :validate_switch
)
end

Expand Down
7 changes: 7 additions & 0 deletions app/models/questionnaire.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Questionnaire < ApplicationRecord
audited

attr_accessor :validate_switch

include ActiveModel::Dirty
include DeletableAttachment
before_validation :consolidate_school_names
Expand Down Expand Up @@ -430,6 +432,11 @@ def unaccepted_agreements
Agreement.all - agreements
end

def update_with_invalid_attributes(attributes)
assign_attributes(attributes)
save!(validate: false)
end

def as_json(options = {})
result = super
result['all_agreements_accepted'] = all_agreements_accepted?
Expand Down
36 changes: 20 additions & 16 deletions app/views/manage/questionnaires/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@
%h6.card-subtitle.mb-2
%span.badge.badge-info Provided by MyMLH
= f.simple_fields_for :user, @questionnaire.user do |u|
= u.input :first_name, input_html: { "data-validate" => "presence" }, label: "First Name", autofocus: true
= u.input :last_name, input_html: { "data-validate" => "presence" }, label: "Last Name"
= f.input :email, input_html: { "data-validate" => "presence email", value: @questionnaire.user.try(:email) }, required: true, hint: 'Can be an existing user (without a questionnaire) or a new user. If this is a new user, they will receive a randomly-generated password that they must request a password reset for.'
= f.input :phone, input_html: { "data-validate" => "presence" }
= f.input :date_of_birth, start_year: Date.today.year - 18, end_year: Date.today.year - 90, order: [:month, :day, :year], input_html: { "data-validate" => "presence" }
= f.input :school_id, as: :school_selection, input_html: { "data-validate" => "presence" }
= f.input :level_of_study, input_html: { "data-validate" => "presence" }
= f.input :major, input_html: { "data-validate" => "presence" }
= f.input :gender, input_html: { "data-validate" => "presence" }
= f.input :country, as: :select, collection: Questionnaire::POSSIBLE_COUNTRIES, include_blank: "(select one...)", input_html: { "data-validate" => "presence" }
= u.input :first_name, label: "First Name", autofocus: true
= u.input :last_name, label: "Last Name"
= f.input :email, input_html: { value: @questionnaire.user.try(:email) }, required: true, hint: 'Can be an existing user (without a questionnaire) or a new user. If this is a new user, they will receive a randomly-generated password that they must request a password reset for.'
= f.input :phone
= f.input :date_of_birth, start_year: Date.today.year - 18, end_year: Date.today.year - 90, order: [:month, :day, :year]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the - 18 and - 90 logic? Checking for minor?
We should refactor this logic for validation in 3.0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that set the start year and the end year in the drop down to 1931 and 2003. I assume that is meant as basic minor checking but it is set as -5 for the user facing application so I will set it to that here as well

= f.input :school_id, as: :school_selection
= f.input :level_of_study
= f.input :major
= f.input :gender
= f.input :country, as: :select, collection: Questionnaire::POSSIBLE_COUNTRIES, include_blank: "(select one...)"

- if !HackathonConfig['digital_hackathon']
.card.mb-4
.card-header Travel information
.card-body
- travel_state = [email protected]_record? && @questionnaire.travel_not_from_school
= f.input :travel_not_from_school, label: "I will not be traveling from my school"
= f.input :travel_location, input_html: { "data-validate" => "presence", disabled: !travel_state }, wrapper_html: { style: travel_state ? "" : "display: none" }, label: "Travel Location"
= f.input :travel_location, input_html: { disabled: !travel_state }, wrapper_html: { style: travel_state ? "" : "display: none" }, label: "Travel Location"

= f.association :bus_list, label: "Bus list", include_blank: 'Not travelling on a sponsored bus'
= f.input :boarded_bus, as: :boolean, label: "Boarded bus", input_html: { checked: @questionnaire.boarded_bus_at.present? }
Expand All @@ -40,7 +40,7 @@
.card.mb-4
.card-header Special notices
.card-body
= f.input :shirt_size, as: :select, collection: Questionnaire::POSSIBLE_SHIRT_SIZES, include_blank: "(select one...)", input_html: { "data-validate" => "presence" }
= f.input :shirt_size, as: :select, collection: Questionnaire::POSSIBLE_SHIRT_SIZES, include_blank: "(select one...)"
- if !HackathonConfig['digital_hackathon']
= f.input :dietary_restrictions, label: "Dietary restrictions"
= f.input :special_needs, label: "Special needs"
Expand All @@ -50,11 +50,11 @@
- if HackathonManager.field_enabled?(:why_attend)
= f.input :why_attend, label: "Why #{HackathonConfig['name']}?", placeholder: "In a sentence or two, why would you like to attend #{HackathonConfig['name']}?", input_html: { rows: 3, maxlength: 280 }

= f.input :experience, as: :select, collection: Questionnaire::POSSIBLE_EXPERIENCES.invert, include_blank: "(select one...)", label: "Experience", input_html: { "data-validate" => "presence" }
= f.input :interest, as: :select, collection: Questionnaire::POSSIBLE_INTERESTS.invert, include_blank: "(select one...)", label: "Interest", input_html: { "data-validate" => "presence" }
peterkos marked this conversation as resolved.
Show resolved Hide resolved
= f.input :experience, as: :select, collection: Questionnaire::POSSIBLE_EXPERIENCES.invert, include_blank: "(select one...)", label: "Experience"
= f.input :interest, as: :select, collection: Questionnaire::POSSIBLE_INTERESTS.invert, include_blank: "(select one...)", label: "Interest"

= f.input :graduation_year, as: :select, collection: Questionnaire::POSSIBLE_GRAD_YEARS, include_blank: "(select one...)", label: "Graduation year", input_html: { "data-validate" => "presence" }, wrapper_html: { class: 'input--half' }
= f.input :race_ethnicity, as: :select, collection: Questionnaire::POSSIBLE_RACE_ETHNICITIES, include_blank: "(select one...)", label: "Race/Ethnicity", input_html: { "data-validate" => "presence" }, wrapper_html: { class: 'input--half' }
= f.input :graduation_year, as: :select, collection: Questionnaire::POSSIBLE_GRAD_YEARS, include_blank: "(select one...)", label: "Graduation year", wrapper_html: { class: 'input--half' }
= f.input :race_ethnicity, as: :select, collection: Questionnaire::POSSIBLE_RACE_ETHNICITIES, include_blank: "(select one...)", label: "Race/Ethnicity", wrapper_html: { class: 'input--half' }

= f.input :resume, as: :deletable_attachment, hint: "Must be under 2MB", input_html: { "data-validate" => "file-max-size file-content-type", "data-validate-file-max-size" => "2097152", "data-validate-file-content-type" => "application/pdf" }, label: "Resume (PDF)"

Expand All @@ -64,4 +64,8 @@
= f.input :can_share_info, label: "Share resume with employers?"

.center.mb-4
.custom-control.custom-switch.pb-3
= f.input :validate_switch, as: :hidden, input_html: { id: "validateFormSwitch" }
%input.custom-control-input{ type: "checkbox", id: "validateSwitch", checked: true }
%label.custom-control-label{ for: "validateSwitch" } Enable Validation
= f.button :submit, value: ( @questionnaire.new_record? ? 'Create' : 'Save' ), class: 'btn-primary'