Skip to content

Commit

Permalink
Merge pull request #144 from code-mancers/fix-checkbox-answers
Browse files Browse the repository at this point in the history
fix(checkbox-answers):match answer_text to ActionController::Parameters
  • Loading branch information
akshaysasidrn authored Oct 4, 2019
2 parents bffe7c7 + e66c454 commit ecabade
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 38 deletions.
7 changes: 6 additions & 1 deletion app/models/rapidfire/attempt.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
module Rapidfire
class Attempt < ActiveRecord::Base
belongs_to :survey
belongs_to :user, polymorphic: true
has_many :answers, inverse_of: :attempt, autosave: true

if Rails::VERSION::MAJOR >= 5
belongs_to :user, polymorphic: true, optional: true
else
belongs_to :user, polymorphic: true
end
end
end
29 changes: 14 additions & 15 deletions app/services/rapidfire/attempt_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,21 @@ def to_model

def save!(options = {})
params.each do |question_id, answer_attributes|
if answer = @attempt.answers.find { |a| a.question_id.to_s == question_id.to_s }
text = answer_attributes[:answer_text]
answer = @attempt.answers.find { |a| a.question_id.to_s == question_id.to_s }
next unless answer

# in case of checkboxes, values are submitted as an array of
# strings. we will store answers as one big string separated
# by delimiter.
text = text.values if text.is_a?(Hash)
answer.answer_text =
if text.is_a?(Array)
strip_checkbox_answers(text).join(Rapidfire.answers_delimiter)
elsif text.is_a?(ActionController::Parameters)
text.values.join(Rapidfire.answers_delimiter)
else
text
end
end
text = answer_attributes[:answer_text]

# in case of checkboxes, values are submitted as an array of
# strings. we will store answers as one big string separated
# by delimiter.
text = text.values if text.is_a?(ActionController::Parameters)
answer.answer_text =
if text.is_a?(Array)
strip_checkbox_answers(text).join(Rapidfire.answers_delimiter)
else
text
end
end

@attempt.save!(options)
Expand Down
2 changes: 1 addition & 1 deletion app/views/rapidfire/answers/_checkbox.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<%= f.fields_for :answer_text do |af| %>
<%- answer.question.options.each_with_index do |option, index| %>
<%= af.label index do %>
<%= check_box_tag "attempt[#{answer.question.id}][answer_text][#{index}]", option, checkbox_checked?(answer, option) %>
<%= af.check_box index, { checked: checkbox_checked?(answer, option) }, option %>
<%= option %>
<% end %>
<% end %>
Expand Down
71 changes: 50 additions & 21 deletions spec/features/rapidfire/answering_questions_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require 'spec_helper'

describe "Surveys" do
let(:survey) { FactoryGirl.create(:survey, name: "Question Set", introduction: "Some introduction") }
let(:question1) { FactoryGirl.create(:q_long, survey: survey, question_text: "Long Question", validation_rules: { presence: "1" }) }
let(:question2) { FactoryGirl.create(:q_short, survey: survey, question_text: "Short Question") }
let!(:survey) { FactoryGirl.create(:survey, name: "Question Set", introduction: "Some introduction") }
let!(:question1) { FactoryGirl.create(:q_long, survey: survey, question_text: "Long Question", validation_rules: { presence: "1" }) }
let!(:question2) { FactoryGirl.create(:q_short, survey: survey, question_text: "Short Question") }
let!(:question3) { FactoryGirl.create(:q_checkbox, survey: survey, question_text: "Checkbox question") }
let!(:question4) { FactoryGirl.create(:q_checkbox, survey: survey, question_text: "Checkbox question", validation_rules: { presence: "1" }) }

before do
[question1, question2]
visit rapidfire.new_survey_attempt_path(survey)
end

Expand All @@ -18,15 +20,17 @@
before do
fill_in "attempt_#{question1.id}_answer_text", with: "Long Answer"
fill_in "attempt_#{question2.id}_answer_text", with: "Short Answer"
check "attempt_#{question3.id}_answer_text_1"
check "attempt_#{question4.id}_answer_text_0"
click_button "Save"
end

it "persists 2 answers" do
expect(Rapidfire::Answer.count).to eq(2)
it "persists 4 answers" do
expect(Rapidfire::Answer.count).to eq(4)
end

it "persists 2 answers with answer values" do
expected_answers = ["Long Answer", "Short Answer"]
it "persists 4 answers with answer values" do
expected_answers = ["Long Answer", "Short Answer", "telugu", "hindi"]
expect(Rapidfire::Answer.all.map(&:answer_text)).to match(expected_answers)
end

Expand All @@ -36,23 +40,48 @@
end

context "when all questions are not answered" do
before do
fill_in "attempt_#{question1.id}_answer_text", with: ""
fill_in "attempt_#{question2.id}_answer_text", with: "Short Answer"
click_button "Save"
end
context "when validation fails" do
before do
fill_in "attempt_#{question1.id}_answer_text", with: ""
fill_in "attempt_#{question2.id}_answer_text", with: "Short Answer"
check "attempt_#{question3.id}_answer_text_1"
click_button "Save"
end

it "fails to persits answers" do
expect(Rapidfire::Answer.count).to eq(0)
end
it "fails to persits answers" do
expect(Rapidfire::Answer.count).to eq(0)
end

it "shows error for missing answers" do
expect(page).to have_content("can't be blank", count: 2)
end

it "shows error for missing answers" do
expect(page).to have_content "can't be blank"
it "shows already populated answers" do
short_answer = page.find("#attempt_#{question2.id}_answer_text").value
expect(page).to have_checked_field("attempt_#{question3.id}_answer_text_1")
expect(short_answer).to have_content "Short Answer"
end
end

it "shows already populated answers" do
short_answer = page.find("#attempt_#{question2.id}_answer_text").value
expect(short_answer).to have_content "Short Answer"
context "when validation passes" do
before do
fill_in "attempt_#{question1.id}_answer_text", with: "Long Answer"
check "attempt_#{question4.id}_answer_text_0"
click_button "Save"
end

it "persists 4 answers" do
expect(Rapidfire::Answer.count).to eq(4)
end

it "persists 4 answers with 2 empty answers" do
expected_answers = ["Long Answer", "", "", "hindi"]
expect(Rapidfire::Answer.all.map(&:answer_text)).to match(expected_answers)
end

it "redirects to question groups path" do
expect(current_path).to eq(rapidfire.surveys_path)
end
end
end
end
Expand Down

0 comments on commit ecabade

Please sign in to comment.