Skip to content

Commit

Permalink
Merge pull request #299 from bmic-development/kg-survey_question_vali…
Browse files Browse the repository at this point in the history
…dation

KG - Fix Survey Question Validation When Dependent on Option Selection
  • Loading branch information
Stuart-Johnson committed May 30, 2017
2 parents 71e22b9 + 750d398 commit 8ea55ef
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
9 changes: 7 additions & 2 deletions app/controllers/surveyor/responses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,16 @@ def create
@response = Response.new(response_params)
@review = params[:review] == 'true'

if @response.save
if @response.save && @response.question_responses.none? { |qr| qr.errors.any? }
# Delete responses to questions that didn't show anyways to avoid confusion in the data
@response.question_responses.where(required: true, content: [nil, '']).destroy_all
SurveyNotification.system_satisfaction_survey(@response) if @response.survey.access_code == 'system-satisfaction-survey'

flash[:success] = t(:surveyor)[:responses][:create]
else
@errors = @response.errors
@response.destroy

@errors = true
end
end

Expand Down
16 changes: 15 additions & 1 deletion app/models/question_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ class QuestionResponse < ActiveRecord::Base
belongs_to :question
belongs_to :response

validates :content, presence: true, if: :required?
delegate :depender, to: :question

validate :phone_number_format, if: Proc.new{ |qr| !qr.content.blank? && qr.question_id && qr.question.question_type == 'phone' }
validate :email_format, if: Proc.new{ |qr| !qr.content.blank? && qr.question_id && qr.question.question_type == 'email' }
validate :zipcode_format, if: Proc.new{ |qr| !qr.content.blank? && qr.question_id && qr.question.question_type == 'zipcode' }

validates_numericality_of :content, only_integer: true, if: Proc.new{ |qr| !qr.content.blank? && qr.question_id && qr.question.question_type == 'number' }

after_save :check_content_requirements

def phone_number_format
if content.match(/\d{10}/).nil?
errors.add(:base, I18n.t(:errors)[:question_responses][:phone_invalid])
Expand Down Expand Up @@ -74,4 +76,16 @@ def report_content
""
end
end

private

def check_content_requirements
if self.required? && self.content.blank? && (self.depender.nil? || self.depender.present? && depender_selected?)
errors.add(:content, :blank)
end
end

def depender_selected?
self.depender && self.response.question_responses.where(question_id: self.depender.question_id).first.content == self.depender.content
end
end
1 change: 1 addition & 0 deletions app/views/surveyor/responses/create.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ $(".question-<%=qr.question_id%>").removeClass('has-error')
$(".question-<%=qr.question_id%> .help-block").remove()
<% end %>
<% end %>
<% @response.question_responses.destroy_all %>
<% else %>
<% if @review %>
$('#modal_place').modal('hide')
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/surveyor/responses/post_create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
}
}

expect(assigns(:errors).count).to eq(1)
expect(assigns(:errors)).to eq(true)
end
end

Expand Down
9 changes: 3 additions & 6 deletions spec/models/question_response/question_response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@
it { is_expected.to belong_to(:response) }

# Validations
it 'should validate presence of content if required' do
qr = build(:question_response, required: true, content: nil)

expect(qr).to_not be_valid
end

context 'question_type == phone' do
it 'should return true if blank' do
q = create(:question, question_type: 'phone')
Expand Down Expand Up @@ -128,4 +122,7 @@
expect(qr2).to be_valid
end
end

# Callbacks
it { is_expected.to callback(:check_content_requirements).after(:save) }
end

0 comments on commit 8ea55ef

Please sign in to comment.