Skip to content
This repository was archived by the owner on Feb 12, 2018. It is now read-only.

Commit 546131f

Browse files
author
Mark Yoon
committed
first shot at using formtastic
1 parent c18935b commit 546131f

17 files changed

+222
-211
lines changed

Rakefile

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ begin
1111
gem.authors = ["Brian Chamberlain", "Mark Yoon"]
1212
gem.add_dependency 'haml'
1313
gem.add_dependency 'fastercsv'
14+
gem.add_dependency 'formtastic'
1415
gem.add_development_dependency "yard", ">= 0"
1516
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
1617
end

app/helpers/surveyor_helper.rb

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
module SurveyorHelper
2-
32
# Layout: stylsheets and javascripts
43
def surveyor_includes
54
surveyor_stylsheets + surveyor_javascripts
@@ -34,7 +33,28 @@ def next_section
3433
# refactored to use copy in memory instead of making extra db calls
3534
@sections.last == @section ? submit_tag(t('surveyor.click_here_to_finish'), :name => "finish") : submit_tag(t('surveyor.next_section'), :name => "section[#{@sections[@sections.index(@section)+1].id}]")
3635
end
37-
36+
37+
# new methods
38+
def q_text(obj)
39+
@n ||= 0
40+
return image_tag(obj.text) if obj.is_a?(Question) and obj.display_type == "image"
41+
return obj.text if obj.is_a?(Question) and (obj.dependent? or obj.display_type == "label" or obj.part_of_group?)
42+
"#{@n += 1}) #{obj.text}"
43+
end
44+
def response_for(response_set, question, answer = nil)
45+
return nil unless response_set && question && question.id
46+
if answer.nil?
47+
result = response_set.responses.detect{|r| r.question_id == question.id}
48+
result.blank? ? response_set.responses.build(:question => question) : result
49+
elsif
50+
result = response_set.responses.detect{|r| r.question_id == question.id && r.answer_id == answer.id}
51+
result.blank? ? response_set.responses.build(:question => question, :answer => answer) : result
52+
end
53+
end
54+
def response_idx(increment = true)
55+
@rc ||= 0
56+
(increment ? @rc += 1 : @rc).to_s
57+
end
3858
# Questions
3959
def next_number
4060
@n ||= 0

app/views/partials/_answer.html.haml

+19-58
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,19 @@
1-
- renderer = answer.renderer(question)
2-
- response_group ||= nil
3-
- hide_label ||= false
4-
- disabled ||= false
5-
- fields_for_response(response_obj, response_group) do |response_form|
6-
- case renderer
7-
- when :any_answer
8-
= response_form.survey_check_box(:selected, :class => (answer.is_exclusive? ? "exclusive" : ""))
9-
= response_form.label("selected", answer.text) unless hide_label # This is a temp fix til I figure out why the id of the checkbox is all jacked up
10-
- when :any_other_and_string
11-
= response_form.survey_check_box(:selected, :class => (answer.is_exclusive? ? "exclusive" : ""))
12-
%label Other
13-
= response_form.text_field(:other_value)
14-
= response_form.text_field(:string_value)
15-
- when :any_string
16-
= response_form.survey_check_box(:selected, :class => (answer.is_exclusive? ? "exclusive" : ""))
17-
= response_form.label(:answer_id, answer.text)
18-
= response_form.text_field(:string_value, :size => 25, :maxlength => 250)
19-
- when :one_answer, :one_string, :one_integer
20-
- fields_for_radio(response_obj) do |radio_form|
21-
= radio_form.radio_button(:answer_id, answer.id, :checked => response_obj.selected?, :title => answer.text)
22-
= radio_form.label("answer_id_#{answer.id}", split_text(answer.text)[:prefix]) unless hide_label
23-
- if renderer == :one_string
24-
= response_form.text_field(:string_value, :size => 25, :maxlength => 250, :autocomplete => "off")
25-
- if renderer == :one_integer
26-
= response_form.text_field(:integer_value, :size => 5, :maxlength => 10, :autocomplete => "off")
27-
= radio_form.label("answer_id_#{answer.id}", split_text(answer.text)[:postfix]) unless hide_label
28-
- when :none_date
29-
= response_form.label(:datetime_value, split_text(answer.text)[:prefix], :class => "#{(disabled)? 'disabled' : 'enabled'}") unless hide_label
30-
= response_form.text_field(:datetime_value)
31-
= response_form.label(:datetime_value, split_text(answer.text)[:postfix], :class => "#{(disabled)? 'disabled' : 'enabled'}") unless hide_label
32-
- when :none_datetime
33-
= response_form.label(:datetime_value, split_text(answer.text)[:prefix], :class => "#{(disabled)? 'disabled' : 'enabled'}") unless hide_label
34-
= response_form.datetime_select(:datetime_value, {:include_blank => true})
35-
= response_form.label(:datetime_value, split_text(answer.text)[:postfix], :class => "#{(disabled)? 'disabled' : 'enabled'}") unless hide_label
36-
- when :none_float
37-
= response_form.label(:float_value, split_text(answer.text)[:prefix]) unless hide_label
38-
= response_form.text_field(:float_value, :size => 5, :maxlength => 10, :autocomplete => "off")
39-
= response_form.label(:float_value, split_text(answer.text)[:postfix]) unless hide_label
40-
- when :none_integer
41-
= response_form.label(:integer_value, split_text(answer.text)[:prefix], :class => "#{(disabled)? 'disabled' : 'enabled'}") unless hide_label
42-
= response_form.text_field(:integer_value, :size => 5, :maxlength => 10, :autocomplete => "off", :disabled => disabled)
43-
= response_form.label(:integer_value, split_text(answer.text)[:postfix], :class => "#{(disabled)? 'disabled' : 'enabled'}") unless hide_label
44-
- when :none_string
45-
= response_form.label(:string_value, split_text(answer.text)[:prefix], :class => "#{(disabled)? 'disabled' : 'enabled'}") unless hide_label
46-
= response_form.text_field(:string_value, :size => 25, :maxlength => 250, :autocomplete => "off", :disabled => disabled)
47-
= response_form.label(:string_value, split_text(answer.text)[:postfix], :class => "#{(disabled)? 'disabled' : 'enabled'}") unless hide_label
48-
- when :none_text
49-
= response_form.label(:text_value, split_text(answer.text)[:prefix]) unless hide_label
50-
= response_form.text_area(:text_value, :cols => 80, :rows => 10)
51-
= response_form.label(:text_value, split_text(answer.text)[:postfix]) unless hide_label
52-
- when :none_time
53-
= response_form.label(:datetime_value, split_text(answer.text)[:prefix], :class => "#{(disabled)? 'disabled' : 'enabled'}") unless hide_label
54-
= response_form.time_select(:datetime_value,{ :include_blank => true })
55-
= response_form.label(:datetime_value, split_text(answer.text)[:postfix], :class => "#{(disabled)? 'disabled' : 'enabled'}") unless hide_label
56-
- else
57-
= answer.text
58-
1+
-# TODO: class for a.exclusive, disabled, hide_label, prefix/post labels
2+
- r = response_for(@response_set, q, a)
3+
- i = response_idx(q.pick != "one") # argument will be false (don't increment i) if we're on radio buttons
4+
- f.semantic_fields_for i, r do |g|
5+
= g.input :question_id, :as => :hidden unless q.pick == "one" # don't repeat question_id if we're on radio buttons
6+
- case q.pick
7+
- when "one"
8+
= g.input :answer_id, :as => :radio, :collection => [[a.text, a.id]], :label => false
9+
= g.input :string if a.response_class == 'string'
10+
= g.input :integer if a.response_class == 'integer'
11+
- when "any"
12+
= g.input :answer_id, :as => :check_boxes, :collection => [[a.text, a.id]], :label => false
13+
= g.input :other_value, :label => false if a.response_class == "other_and_string"
14+
= g.input :string_value, :label => false if %w(string other_and_string).include? a.response_class
15+
- when "none"
16+
- if %w(date datetime time float integer string text time).include? a.response_class
17+
= g.input a.response_class.to_sym, :label => a.text
18+
- else
19+
= a.text
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#dependents
2+
.title Follow-up questions from your answers on the previous page
3+
-# @dependents.each_with_index do |question, index|
4+
= dependency_explanation_helper(question, @response_set)
5+
= render question.custom_renderer || "/partials/question", :question => question, :response_set => @response_set, :number => "D#{index+1}"
+20-72
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,20 @@
1-
- renderer = question.renderer(question_group ||= nil)
2-
- case renderer
3-
- when :label
4-
- div_for question, :class => "label #{question.css_class(response_set)}" do
5-
%span.text= question.text
6-
%span.help= question.help_text
7-
- when :image
8-
.image= image_tag(question.text)
9-
- when :dropdown, :slider
10-
- if renderer == :slider
11-
%script{:type => "text/javascript"}= "$(function(){$('#slider_#{question.id}').accessibleUISlider({width: 400, labels:#{question.answers.size} }).hide();});"
12-
- div_for question, :class => question.css_class(response_set) do
13-
.number= next_number unless question.dependent?
14-
%fieldset
15-
%legend{:style => "display:inline;"}
16-
%span.text= question.text
17-
%span.help= question.help_text
18-
= hidden_field_tag("responses[#{question.id}][question_id]", question.id)
19-
%ol.answers
20-
%li.answer
21-
- options = question.answers.collect{|a| "<option #{ (response_set.response_for(question.id, a.id).selected?)? "selected='selected'" : nil } value ='#{a.id}'>#{a.text}</option>" }
22-
= renderer == :slider ? select_tag("responses[#{question.id}][answer_id]", options, {:id => "slider_#{question.id}"}) : select_tag("responses[#{question.id}][answer_id]", ["<option value=''>#{t('surveyor.Select_one')}</option>"].concat(options))
23-
- when :grid_default
24-
- reset_cycle("col")
25-
- content_tag_for :tr, question, :class => question.css_class(response_set) do
26-
%th.question_prefix
27-
%span.text= split_text(question.text)[:prefix]
28-
%span.help= question.help_text
29-
= hidden_field_tag("responses[#{question.id}][question_id]", question.id)
30-
- question.answers.each do |answer|
31-
%td{:class => "#{cycle("column_highlight", "", :name => "col")}"}
32-
.answer{:class => answer.custom_class}= render(:partial => answer.custom_renderer || "/partials/answer", :locals => {:answer => answer, :question => question, :response_obj => @response_set.response_for(question.id, answer.id), :hide_label => true})
33-
%th.question_postfix
34-
%span.text= split_text(question.text)[:postfix]
35-
- when :repeater_default, :repeater_dropdown
36-
- disabled ||= false
37-
- div_for question, :class => "#{disabled ? 'dis' : 'en'}abled #{question.css_class(response_set)}" do
38-
%span.text= question.text
39-
%span.help= question.help_text
40-
= hidden_field_tag("response_groups[#{question.id}][#{response_group}][response_group]", response_group)
41-
= hidden_field_tag("response_groups[#{question.id}][#{response_group}][question_id]", question.id)
42-
%br
43-
%ol.answers
44-
- if renderer == :repeater_default
45-
- question.answers.each do |answer|
46-
- content_tag_for :li, answer, :class => answer.custom_class do
47-
= render(:partial => answer.custom_renderer || "/partials/answer", :locals => {:answer => answer, :question => question, :response_obj => @response_set.response_for(question.id, answer.id, response_group), :response_group => response_group, :hide_label => true, :disabled => disabled})
48-
- else
49-
%li.answer
50-
- options = question.answers.collect{|a| "<option #{ (response_set.response_for(question.id, a.id, response_group).selected?)? "selected='selected'" : nil } value ='#{a.id}'>#{a.text}</option>" }
51-
= select_tag("response_groups[#{question.id}][#{response_group}][answer_id]", ["<option value=''>Select one...</option>"].concat(options), :disabled => disabled)
52-
- when :inline_default, :inline_dropdown
53-
- div_for question, :class => "inline #{question.css_class(response_set)}" do
54-
= hidden_field_tag("responses[#{question.id}][question_id]", question.id)
55-
- if renderer == :inline_default
56-
- question.answers.each do |answer|
57-
.answer{:class => answer.custom_class}= render(:partial => answer.custom_renderer || "/partials/answer", :locals => {:answer => answer, :question => question, :response_obj => @response_set.response_for(question.id, answer.id), :hide_label => answer.hide_label})
58-
- else
59-
- options = question.answers.collect{|a| "<option #{ (response_set.response_for(question.id, a.id).selected?)? "selected='selected'" : nil } value ='#{a.id}'>#{a.text}</option>" }
60-
= select_tag("responses[#{question.id}][answer_id]", ["<option value=''>Select one...</option>"].concat(options))
61-
- else # :default, :inline
62-
- div_for question, :class => question.css_class(response_set) do
63-
.number= next_number unless question.dependent?
64-
%fieldset
65-
%legend
66-
%span.text= question.text
67-
%span.help= question.help_text
68-
= hidden_field_tag("responses[#{question.id}][question_id]", question.id)
69-
%ol.answers{:class => renderer == :inline ? "inline" : nil}
70-
- question.answers.each do |answer|
71-
- content_tag_for :li, answer, :class => answer.custom_class do
72-
= render(:partial => answer.custom_renderer || "/partials/answer", :locals => {:answer => answer, :question => question, :response_obj => @response_set.response_for(question.id, answer.id), :hide_label => answer.hide_label})
1+
-# TODO: js for slider pre/post text for question in a grid
2+
- renderer = q.renderer(g ||= nil)
3+
- f.inputs q_text(q), :id => "q_#{q.id}", :class => "q_#{renderer} #{q.css_class(@response_set)}" do
4+
%span.help= q.help_text
5+
- case renderer
6+
- when :image, :label
7+
- when :dropdown, :inline_dropdown, :slider
8+
- r = response_for(@response_set, q)
9+
- i = response_idx
10+
- f.semantic_fields_for i, r do |g|
11+
= g.input :question_id, :as => :hidden
12+
= g.input :answer_id, :collection => q.answers.map{|a| [a.text, a.id]}, :label => false
13+
- else # :default, :inline, :inline_default, :inline_dropdown
14+
- if q.pick == "one"
15+
- r = response_for(@response_set, q)
16+
- i = response_idx
17+
- f.semantic_fields_for i, r do |g|
18+
= g.input :question_id, :as => :hidden
19+
- q.answers.each do |a|
20+
= render a.custom_renderer || '/partials/answer', :q => q, :a => a, :f => f
+30-39
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,30 @@
1-
- renderer = question_group.renderer
2-
- locals = {:question_group => question_group, :response_set => response_set, :hide_label => true}
3-
- div_for question_group, :class => question_group.css_class(response_set) do
4-
.head
5-
.number= next_number
6-
%span.text= question_group.text
7-
%span.help= question_group.help_text
8-
9-
- case question_group.renderer
10-
- when :grid
11-
%table.grid
12-
%thead
13-
%tr
14-
%th= "&nbsp;"
15-
- reset_cycle("column_highlight")
16-
- group_questions.first.answers.each do |answer|
17-
%th{:class => "#{cycle("column_highlight", "", :name => "column_highlight")}"}= answer.text
18-
%tbody
19-
- group_questions.each_with_index do |question, i|
20-
= render(:partial => question.custom_renderer || "/partials/question", :locals => locals.merge({:question => question}))
21-
- if i%10 == 9
22-
%th= "&nbsp;"
23-
- reset_cycle("column_highlight")
24-
- group_questions.first.answers.each do |answer|
25-
%th{:class => "#{cycle("column_highlight", "", :name => "column_highlight")}"}= answer.text
26-
- when :repeater
27-
%ul.repeater
28-
- (response_set.count_group_responses(group_questions) + 1).times do |group_number|
29-
%li.repeater
30-
- group_questions.each do |question|
31-
= render(:partial => question.custom_renderer || "/partials/question", :locals => locals.merge({:question => question, :response_group => group_number, :disabled => false}))
32-
%li.repeater
33-
- group_questions.each do |question|
34-
= render(:partial => question.custom_renderer || "/partials/question", :locals => locals.merge({:question => question, :response_group => 9999, :disabled => true}))
35-
= submit_tag("&lt;= add row", :name => "section[#{group_questions.first.survey_section_id}]question_group_#{question_group.id}", :class => "add_row")
36-
- else # :inline
37-
%ul
38-
- group_questions.each do |question|
39-
%li{:class => question_group.renderer == :inline ? "inline" : nil}= render(:partial => question.custom_renderer || "/partials/question", :locals => locals.merge({:question => question}))
1+
-# TODO: inline, disabled
2+
- renderer = g.renderer
3+
- f.inputs q_text(g), :id => "g_#{g.id}", :class => "g_#{renderer} #{g.css_class(@response_set)}" do
4+
%li.help= g.help_text
5+
- case renderer
6+
- when :grid
7+
%li
8+
%table
9+
%tbody
10+
- qs.each_slice(10) do |ten_questions|
11+
-# header row every 10
12+
%tr
13+
%th &nbsp;
14+
- ten_questions.first.answers.each do |a|
15+
%th= a.text
16+
- ten_questions.each_with_index do |q, i|
17+
%tr{:id => "q_#{q.id}", :class => "q_#{renderer} #{q.css_class(@response_set)}"}
18+
%th= q.text
19+
- q.answers.each do |a|
20+
%td= render a.custom_renderer || '/partials/answer', :g => g, :q => q, :a => a, :f => f
21+
- when :repeater
22+
- (@response_set.count_group_responses(qs) + 1).times do |rg|
23+
- qs.each do |q|
24+
= render q.custom_renderer || "/partials/question", :g => g, :rg => rg, :q => q, :f => f
25+
- qs.each do |q|
26+
= render q.custom_renderer || "/partials/question", :g => g, :q => q, :f => f, :disabled => true
27+
= submit_tag("&lt;= add row", :name => "s[#{@section.id}]g_#{g.id}", :class => "add_row")
28+
- else # :inline
29+
- qs.each do |q|
30+
= render q.custom_renderer || "/partials/question", :g => g, :q => q, :f => f

app/views/partials/_section.html.haml

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
- div_for @section, :class => @section.custom_class do
22
%span.title= @section.title
3-
.questions
4-
- group_questions ||= []
5-
- @questions.each_with_index do |question, index|
6-
- unless question.part_of_group?
7-
= render(:partial => question.custom_renderer || "/partials/question", :locals => {:question => question, :response_set => @response_set})
8-
- else # gather up the group questions
9-
- group_questions << question
10-
- if (index + 1 >= @questions.size) or (question.question_group_id != @questions[index + 1].question_group_id)
11-
- # this is the last question of the section, or the group
12-
= render(:partial => question.question_group.custom_renderer || "/partials/question_group", :locals => {:question_group => question.question_group, :response_set => @response_set, :group_questions => group_questions})
13-
- group_questions = []
3+
= hidden_field_tag :current_section_id, @section.id
4+
- qs ||= []
5+
- (questions = @section.questions).each_with_index do |q, i|
6+
- if q.part_of_group?
7+
- qs << q # gather up the group questions
8+
- if (i+1 >= questions.size) or (q.question_group_id != questions[i+1].question_group_id)
9+
- # this is the last question of the section, or the group
10+
= render q.question_group.custom_renderer || "/partials/question_group", :g => q.question_group, :qs => qs, :f => f
11+
- qs = []
12+
- else # gather up the group questions
13+
= render q.custom_renderer || "/partials/question", :q => q, :f => f
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#menu
2+
= t('surveyor.sections')
3+
%ul
4+
- @sections.each do |s|
5+
%li{:class => ("active" if s == @section)}= menu_button_for(s)

0 commit comments

Comments
 (0)