Skip to content

Commit

Permalink
surveyor show page. closes NUBIC#20
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Yoon committed Mar 1, 2012
1 parent 476bb19 commit 71640da
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 20 deletions.
7 changes: 4 additions & 3 deletions app/views/partials/_answer.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
- rg ||= nil
- r = response_for(@response_set, q, a, rg)
- i = response_idx(q.pick != "one") # argument will be false (don't increment i) if we're on radio buttons
- disabled = defined?(disableFlag) ? disableFlag : false
= f.semantic_fields_for i, r do |ff|
= ff.quiet_input :question_id unless q.pick == "one" # don't repeat question_id if we're on radio buttons
= ff.quiet_input :id unless q.pick == "one" or r.new_record?
= ff.quiet_input :response_group, :value => rg if q.pick != "one" && g && g.display_type == "repeater"
- case q.pick
- when "one"
= ff.input :answer_id, :as => :surveyor_radio, :collection => [[a_text(a, nil, @render_context), a.id]], :label => false, :input_html => {:class => a.css_class}, :response_class => a.response_class
= ff.input :answer_id, :as => :surveyor_radio, :collection => [[a_text(a, nil, @render_context), a.id]], :label => false, :input_html => {:class => a.css_class, :disabled => disabled}, :response_class => a.response_class
- when "any"
= ff.input :answer_id, :as => :surveyor_check_boxes, :collection => [[a_text(a, nil, @render_context), a.id]], :label => false, :input_html => {:class => a.css_class}, :response_class => a.response_class
= ff.input :answer_id, :as => :surveyor_check_boxes, :collection => [[a_text(a, nil, @render_context), a.id]], :label => false, :input_html => {:class => a.css_class, :disabled => disabled}, :response_class => a.response_class
- when "none"
- if %w(date datetime time float integer string text).include? a.response_class
= ff.quiet_input :answer_id, :input_html => {:class => a.css_class, :value => a.id}
= ff.input rc_to_attr(a.response_class), :as => rc_to_as(a.response_class), :label => a_text(a, :pre, @render_context).blank? ? false : a_text(a, :pre, @render_context), :hint => a_text(a, :post, @render_context), :input_html => generate_pick_none_input_html(r.as(a.response_class), a.default_value, a.css_class, a.response_class)
= ff.input rc_to_attr(a.response_class), :as => rc_to_as(a.response_class), :label => a_text(a, :pre, @render_context).blank? ? false : a_text(a, :pre, @render_context), :hint => a_text(a, :post, @render_context), :input_html => generate_pick_none_input_html(r.as(a.response_class), a.default_value, a.css_class, a.response_class, disabled)
- else
= a_text(a, nil, @render_context)
6 changes: 4 additions & 2 deletions app/views/partials/_question.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-# TODO: js for slider
- rg ||= nil
- renderer = q.renderer(g ||= nil)
- disabled = defined?(disableFlag) ? disableFlag : false
= f.inputs q_text(q, @render_context), :id => rg ? "q_#{q.id}_#{rg}" : "q_#{q.id}", :class => "q_#{renderer} #{q.css_class(@response_set)}" do
%span.help= render_help_text(q, @render_context)
- case renderer
Expand All @@ -12,7 +13,7 @@
= ff.quiet_input :question_id
= ff.quiet_input :response_group, :value => rg if g && g.display_type == "repeater"
= ff.quiet_input :id unless r.new_record?
= ff.input :answer_id, :as => :select, :collection => q.answers.map{|a| [a.text, a.id]}, :label => q.text
= ff.input :answer_id, :as => :select, :collection => q.answers.map{|a| [a.text, a.id]}, :label => q.text, :input_html => { :disabled => disabled }
- else # :default, :inline, :inline_default
- if q.pick == "one"
- r = response_for(@response_set, q, nil, rg)
Expand All @@ -22,4 +23,5 @@
= ff.quiet_input :response_group, :value => rg if g && g.display_type == "repeater"
= ff.quiet_input :id unless r.new_record?
- q.answers.each do |a|
= render a.custom_renderer || '/partials/answer', :q => q, :a => a, :f => f, :rg => rg, :g => g
- next if (q.pick == "one" or q.pick == "any") and disabled and @response_set.responses.where( :question_id => q.id, :answer_id => a.id).empty?
= render a.custom_renderer || '/partials/answer', :q => q, :a => a, :f => f, :rg => rg, :g => g, :disableFlag => disabled
88 changes: 75 additions & 13 deletions app/views/surveyor/show.html.haml
Original file line number Diff line number Diff line change
@@ -1,15 +1,77 @@
#surveyor
- @survey.sections.each do |section|
- div_for section do
.title= section.title
.questions
- group_questions ||= []
- section.questions.each_with_index do |question, index|
- if question.part_of_group?
- group_questions << question
- if (index + 1 >= section.questions.size) or (question.question_group_id != section.questions[index + 1].question_group_id)
- # skip to the last question of the section, or the last question of the group
= render(:partial => "/partials/question_group", :locals => {:question_group => question.question_group, :response_set => @response_set, :group_questions => group_questions})
- unless (types = flash.keys.select{|k| [:notice, :error, :warning].include?(k)}).blank?
.surveyor_flash
= flash_messages(types)
.close
= semantic_form_for(@response_set, :as => :r, :url => update_my_survey_path, :html => {:id => "survey_form", :class => @survey.custom_class}) do |f|
.survey_title= @survey.title
- @survey.sections.each do |section|
= div_for section do
%span.title= strip_tags(section.title)
- group_questions = []
- answered_questions = []
- label = nil
- answered_count = 0
- (questions = section.questions).each_with_index do |q, i|
- if q.part_of_group?
- group_questions << q # gather up the group questions
- unless @response_set.responses.where( :question_id => q.id).empty?
- answered_questions << q
- answered_count = answered_count + 1
- if (i+1 >= questions.size) or (q.question_group_id != questions[i+1].question_group_id) # this is the last question of the section, or the group
- if answered_questions.length > 0 # there are answered questions in this section or group
- g = q.question_group
- renderer = g.renderer

- unless label.blank?
= render q.custom_renderer || "/partials/question", :q => label, :f => f, :disableFlag => true
- label = nil
= f.inputs q_text(g), :id => "g_#{g.id}", :class => "g_#{renderer} #{g.css_class(@response_set)}" do
%li.help= g.help_text
- case renderer
- when :grid
%li
%table
%col.pre
- group_questions.first.answers.each do |a|
%col{:class => cycle("odd", "even")}
%col.post
%tbody
- group_questions.each_slice(10) do |ten_questions| # header row every 10
%tr
%th &nbsp;
- ten_questions.first.answers.each do |a|
%th= a_text(a)
%th &nbsp;
- ten_questions.each_with_index do |q, j|
%tr{:id => "q_#{q.id}", :class => "q_#{renderer} #{q.css_class(@response_set)}"}
- if q.pick == "one"
- r = response_for(@response_set, q, nil, g)
- j = response_idx # increment the response index since the answer partial skips for q.pick == one
%th= q.split_text(:pre)
- q.answers.each do |a|
%td= render a.custom_renderer || '/partials/answer', :g => g, :q => q, :a => a, :f => f, :disableFlag => true
%th= q.split_text(:post)
- when :repeater
- (@response_set.count_group_responses(group_questions) + 1).times do |rg|
%li
- group_questions.each do |q|
= render q.custom_renderer || "/partials/question", :g => g, :rg => rg, :q => q, :f => f, :disableFlag => true
- else # :inline
- answered_questions.each do |q|
= render q.custom_renderer || "/partials/question", :g => g, :q => q, :f => f, :disableFlag => true

- group_questions = []
- else
= render(:partial => "/partials/question", :locals => {:question => question, :response_set => @response_set})
- answered_questions = []
- else
- if q.display_type == 'label'
- label = q
- unless @response_set.responses.where( :question_id => q.id).empty?
- unless label.blank?
= render q.custom_renderer || "/partials/question", :q => label, :f => f, :disableFlag => true
- label = nil
= render q.custom_renderer || "/partials/question", :q => q, :f => f, :disableFlag => true
- answered_count = answered_count + 1
- if (i+1 >= questions.size) and (answered_count == 0)
#{"--- No data ---"}
42 changes: 42 additions & 0 deletions features/show_survey.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Feature: showing a survey
As a survey administrator
I want to see a survey that a participant has taken
So that I can understand the data

Scenario: Take a survey, then look at it
Given the survey
"""
survey "Favorites" do
section "Colors" do
label "You with the sad eyes don't be discouraged"
question_1 "What is your favorite color?", :pick => :one
answer "red"
answer "blue"
answer "green"
answer :other
q_2b "Choose the colors you don't like", :pick => :any
a_1 "orange"
a_2 "purple"
a_3 "brown"
a :omit
end
end
"""
When I start the "Favorites" survey
Then I should see "You with the sad eyes don't be discouraged"
And I choose "red"
And I choose "blue"
And I check "orange"
And I check "brown"
And I press "Click here to finish"
Then there should be 1 response set with 3 responses with:
| answer |
| blue |
| orange |
| brown |
When I go to the last response set show page
Then the "blue" radiobutton should be checked
And the "orange" checkbox should be checked
And the "brown" checkbox should be checked
3 changes: 2 additions & 1 deletion features/support/paths.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def path_to(page_name)
'/'
when /the surveys page/
'/surveys'

when /the last response set show page/
view_my_survey_path(:survey_code => ResponseSet.last.survey.access_code, :response_set_code => ResponseSet.last.access_code)
# Add more mappings here.
# Here is an example that pulls values out of the Regexp:
#
Expand Down
3 changes: 2 additions & 1 deletion lib/surveyor/helpers/surveyor_helper_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ def rc_to_as(type_sym)
end
end

def generate_pick_none_input_html(value, default_value, css_class, response_class)
def generate_pick_none_input_html(value, default_value, css_class, response_class, disabled)
html = {}
html[:class] = [response_class,css_class].reject{ |c| c.blank? }
html[:value] = default_value if value.blank?
html[:disabled] = disabled unless disabled.blank?
html
end

Expand Down

0 comments on commit 71640da

Please sign in to comment.