Skip to content

Commit ecabade

Browse files
Merge pull request #144 from code-mancers/fix-checkbox-answers
fix(checkbox-answers):match answer_text to ActionController::Parameters
2 parents bffe7c7 + e66c454 commit ecabade

File tree

4 files changed

+71
-38
lines changed

4 files changed

+71
-38
lines changed

app/models/rapidfire/attempt.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
module Rapidfire
22
class Attempt < ActiveRecord::Base
33
belongs_to :survey
4-
belongs_to :user, polymorphic: true
54
has_many :answers, inverse_of: :attempt, autosave: true
5+
6+
if Rails::VERSION::MAJOR >= 5
7+
belongs_to :user, polymorphic: true, optional: true
8+
else
9+
belongs_to :user, polymorphic: true
10+
end
611
end
712
end

app/services/rapidfire/attempt_builder.rb

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,21 @@ def to_model
1313

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

19-
# in case of checkboxes, values are submitted as an array of
20-
# strings. we will store answers as one big string separated
21-
# by delimiter.
22-
text = text.values if text.is_a?(Hash)
23-
answer.answer_text =
24-
if text.is_a?(Array)
25-
strip_checkbox_answers(text).join(Rapidfire.answers_delimiter)
26-
elsif text.is_a?(ActionController::Parameters)
27-
text.values.join(Rapidfire.answers_delimiter)
28-
else
29-
text
30-
end
31-
end
19+
text = answer_attributes[:answer_text]
20+
21+
# in case of checkboxes, values are submitted as an array of
22+
# strings. we will store answers as one big string separated
23+
# by delimiter.
24+
text = text.values if text.is_a?(ActionController::Parameters)
25+
answer.answer_text =
26+
if text.is_a?(Array)
27+
strip_checkbox_answers(text).join(Rapidfire.answers_delimiter)
28+
else
29+
text
30+
end
3231
end
3332

3433
@attempt.save!(options)

app/views/rapidfire/answers/_checkbox.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<%= f.fields_for :answer_text do |af| %>
66
<%- answer.question.options.each_with_index do |option, index| %>
77
<%= af.label index do %>
8-
<%= check_box_tag "attempt[#{answer.question.id}][answer_text][#{index}]", option, checkbox_checked?(answer, option) %>
8+
<%= af.check_box index, { checked: checkbox_checked?(answer, option) }, option %>
99
<%= option %>
1010
<% end %>
1111
<% end %>

spec/features/rapidfire/answering_questions_spec.rb

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
require 'spec_helper'
22

33
describe "Surveys" do
4-
let(:survey) { FactoryGirl.create(:survey, name: "Question Set", introduction: "Some introduction") }
5-
let(:question1) { FactoryGirl.create(:q_long, survey: survey, question_text: "Long Question", validation_rules: { presence: "1" }) }
6-
let(:question2) { FactoryGirl.create(:q_short, survey: survey, question_text: "Short Question") }
4+
let!(:survey) { FactoryGirl.create(:survey, name: "Question Set", introduction: "Some introduction") }
5+
let!(:question1) { FactoryGirl.create(:q_long, survey: survey, question_text: "Long Question", validation_rules: { presence: "1" }) }
6+
let!(:question2) { FactoryGirl.create(:q_short, survey: survey, question_text: "Short Question") }
7+
let!(:question3) { FactoryGirl.create(:q_checkbox, survey: survey, question_text: "Checkbox question") }
8+
let!(:question4) { FactoryGirl.create(:q_checkbox, survey: survey, question_text: "Checkbox question", validation_rules: { presence: "1" }) }
9+
710
before do
8-
[question1, question2]
911
visit rapidfire.new_survey_attempt_path(survey)
1012
end
1113

@@ -18,15 +20,17 @@
1820
before do
1921
fill_in "attempt_#{question1.id}_answer_text", with: "Long Answer"
2022
fill_in "attempt_#{question2.id}_answer_text", with: "Short Answer"
23+
check "attempt_#{question3.id}_answer_text_1"
24+
check "attempt_#{question4.id}_answer_text_0"
2125
click_button "Save"
2226
end
2327

24-
it "persists 2 answers" do
25-
expect(Rapidfire::Answer.count).to eq(2)
28+
it "persists 4 answers" do
29+
expect(Rapidfire::Answer.count).to eq(4)
2630
end
2731

28-
it "persists 2 answers with answer values" do
29-
expected_answers = ["Long Answer", "Short Answer"]
32+
it "persists 4 answers with answer values" do
33+
expected_answers = ["Long Answer", "Short Answer", "telugu", "hindi"]
3034
expect(Rapidfire::Answer.all.map(&:answer_text)).to match(expected_answers)
3135
end
3236

@@ -36,23 +40,48 @@
3640
end
3741

3842
context "when all questions are not answered" do
39-
before do
40-
fill_in "attempt_#{question1.id}_answer_text", with: ""
41-
fill_in "attempt_#{question2.id}_answer_text", with: "Short Answer"
42-
click_button "Save"
43-
end
43+
context "when validation fails" do
44+
before do
45+
fill_in "attempt_#{question1.id}_answer_text", with: ""
46+
fill_in "attempt_#{question2.id}_answer_text", with: "Short Answer"
47+
check "attempt_#{question3.id}_answer_text_1"
48+
click_button "Save"
49+
end
4450

45-
it "fails to persits answers" do
46-
expect(Rapidfire::Answer.count).to eq(0)
47-
end
51+
it "fails to persits answers" do
52+
expect(Rapidfire::Answer.count).to eq(0)
53+
end
54+
55+
it "shows error for missing answers" do
56+
expect(page).to have_content("can't be blank", count: 2)
57+
end
4858

49-
it "shows error for missing answers" do
50-
expect(page).to have_content "can't be blank"
59+
it "shows already populated answers" do
60+
short_answer = page.find("#attempt_#{question2.id}_answer_text").value
61+
expect(page).to have_checked_field("attempt_#{question3.id}_answer_text_1")
62+
expect(short_answer).to have_content "Short Answer"
63+
end
5164
end
5265

53-
it "shows already populated answers" do
54-
short_answer = page.find("#attempt_#{question2.id}_answer_text").value
55-
expect(short_answer).to have_content "Short Answer"
66+
context "when validation passes" do
67+
before do
68+
fill_in "attempt_#{question1.id}_answer_text", with: "Long Answer"
69+
check "attempt_#{question4.id}_answer_text_0"
70+
click_button "Save"
71+
end
72+
73+
it "persists 4 answers" do
74+
expect(Rapidfire::Answer.count).to eq(4)
75+
end
76+
77+
it "persists 4 answers with 2 empty answers" do
78+
expected_answers = ["Long Answer", "", "", "hindi"]
79+
expect(Rapidfire::Answer.all.map(&:answer_text)).to match(expected_answers)
80+
end
81+
82+
it "redirects to question groups path" do
83+
expect(current_path).to eq(rapidfire.surveys_path)
84+
end
5685
end
5786
end
5887
end

0 commit comments

Comments
 (0)