From db28f52cd4e0d5e4bae08ba71457360a520bae6c Mon Sep 17 00:00:00 2001 From: Mark Yoon Date: Mon, 21 May 2012 21:46:23 -0500 Subject: [PATCH] adding date and time fields to pick one and pick any. closes #207 --- app/inputs/surveyor_check_boxes_input.rb | 6 ++-- app/inputs/surveyor_radio_input.rb | 6 ++-- features/step_definitions/surveyor_steps.rb | 3 ++ features/surveyor.feature | 30 +++++++++++++++++-- .../helpers/formtastic_custom_input.rb | 17 +++++++++++ spec/helpers/formtastic_custom_input_spec.rb | 16 ++++++++++ 6 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 lib/surveyor/helpers/formtastic_custom_input.rb create mode 100644 spec/helpers/formtastic_custom_input_spec.rb diff --git a/app/inputs/surveyor_check_boxes_input.rb b/app/inputs/surveyor_check_boxes_input.rb index 5ad2c63e..9e51463f 100644 --- a/app/inputs/surveyor_check_boxes_input.rb +++ b/app/inputs/surveyor_check_boxes_input.rb @@ -1,4 +1,5 @@ class SurveyorCheckBoxesInput < Formtastic::Inputs::CheckBoxesInput + include Surveyor::Helpers::FormtasticCustomInput def to_html super end @@ -12,13 +13,10 @@ def choice_html(choice) label_html_options.merge(:for => choice_input_dom_id(choice), :class => nil) ) output << builder.text_field(:response_other, input_html_options_with(choice, :response_other)) if options[:response_class] == "other_and_string" - output << builder.text_field(:string_value, input_html_options_with(choice, :string_value)) if options[:response_class] == "other_and_string" or options[:response_class] == "string" or options[:response_class] == "integer" or options[:response_class] == "float" + output << builder.text_field(response_class_to_method(options[:response_class]), input_html_options_with(choice, options[:response_class])) if %w(date datetime time float integer string other_and_string).include? options[:response_class] output << builder.text_area(:text_value, input_html_options_with(choice, :text_value)) if options[:response_class] == "text" output.html_safe end - def input_html_options_with(choice, sym) - input_html_options.merge(choice_html_options(choice)).merge({:id => (input_html_options[:id] || "answer_id").gsub("answer_id", sym.to_s)}) - end def checked?(value) selected_values.include?(value.to_s) end diff --git a/app/inputs/surveyor_radio_input.rb b/app/inputs/surveyor_radio_input.rb index 80223e59..8390f3ba 100644 --- a/app/inputs/surveyor_radio_input.rb +++ b/app/inputs/surveyor_radio_input.rb @@ -1,4 +1,5 @@ class SurveyorRadioInput < Formtastic::Inputs::RadioInput + include Surveyor::Helpers::FormtasticCustomInput def to_html super end @@ -10,11 +11,8 @@ def choice_html(choice) label_html_options.merge(:for => choice_input_dom_id(choice), :class => nil) ) output << builder.text_field(:response_other, input_html_options_with(choice, :response_other)) if options[:response_class] == "other_and_string" - output << builder.text_field(:string_value, input_html_options_with(choice, :string_value)) if options[:response_class] == "other_and_string" or options[:response_class] == "string" or options[:response_class] == "integer" or options[:response_class] == "float" + output << builder.text_field(response_class_to_method(options[:response_class]), input_html_options_with(choice, options[:response_class])) if %w(date datetime time float integer string other_and_string).include? options[:response_class] output << builder.text_area(:text_value, input_html_options_with(choice, :text_value)) if options[:response_class] == "text" output.html_safe end - def input_html_options_with(choice, sym) - input_html_options.merge(choice_html_options(choice)).merge({:id => (input_html_options[:id] || "answer_id").gsub("answer_id", sym.to_s)}) - end end \ No newline at end of file diff --git a/features/step_definitions/surveyor_steps.rb b/features/step_definitions/surveyor_steps.rb index a182038a..f5eb1ccf 100644 --- a/features/step_definitions/surveyor_steps.rb +++ b/features/step_definitions/surveyor_steps.rb @@ -158,3 +158,6 @@ def render_context page.has_css?('textarea', :count => i) end +Then /^I should see (\d+) "(.*?)" input on the page$/ do |i, css_class| + page.has_css?("input.#{css_class}", :count => i) +end diff --git a/features/surveyor.feature b/features/surveyor.feature index 76b39b3d..482ba1d0 100644 --- a/features/surveyor.feature +++ b/features/surveyor.feature @@ -568,7 +568,7 @@ Feature: Survey creation And I should not see "Keniya" # Issue 236 - ":text"- field doesn't show up in the multi-select questions - Scenario: Pick one and pick many with text areas + Scenario: Pick one and pick any with text areas Given the survey """ survey "Pick plus text" do @@ -587,4 +587,30 @@ Feature: Survey creation When I go to the surveys page And I wait 1 seconds And I press "Take it" - Then I should see 3 textareas on the page \ No newline at end of file + Then I should see 3 textareas on the page + + # Issue 207 - Create separate fields for date and time + Scenario: Pick one and pick any with dates + Given the survey + """ + survey "Complex date survey" do + section "Date questions with pick one and pick any" do + q "What is your birth date?", :pick => :one + a "I was born on", :date + a "Refused" + + q "At what time were you born?", :pick => :any + a "I was born at", :time + a "This time is approximate" + + q "When would you like to schedule your next appointment?" + a :datetime + end + end + """ + When I go to the surveys page + And I wait 1 seconds + And I press "Take it" + Then I should see 1 "date" input on the page + And I should see 1 "time" input on the page + And I should see 1 "datetime" input on the page \ No newline at end of file diff --git a/lib/surveyor/helpers/formtastic_custom_input.rb b/lib/surveyor/helpers/formtastic_custom_input.rb new file mode 100644 index 00000000..606a3d81 --- /dev/null +++ b/lib/surveyor/helpers/formtastic_custom_input.rb @@ -0,0 +1,17 @@ +module Surveyor + module Helpers + module FormtasticCustomInput + def input_html_options_with(choice, response_class) + input_html_options.merge(choice_html_options(choice)).merge({:id => (input_html_options[:id] || "answer_id").gsub("answer_id", response_class_to_method(response_class).to_s), :class => [input_html_options[:class], response_class].join(" ")}) + end + def response_class_to_method(response_class) + # doesn't handle response_class == answer, and doesn't have to + case response_class.to_s + when /^other_and_string$/ then :string_value + when /^date|time$/ then :datetime_value + else "#{response_class}_value".to_sym + end + end + end + end +end diff --git a/spec/helpers/formtastic_custom_input_spec.rb b/spec/helpers/formtastic_custom_input_spec.rb new file mode 100644 index 00000000..eca34525 --- /dev/null +++ b/spec/helpers/formtastic_custom_input_spec.rb @@ -0,0 +1,16 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +require File.expand_path(File.dirname(__FILE__) + '/../../lib/surveyor/helpers/formtastic_custom_input') + +describe Surveyor::Helpers::FormtasticCustomInput do + context "input helpers" do + it "should translate response class into attribute" do + helper.response_class_to_method(:string).should == :string_value + helper.response_class_to_method(:other_and_string).should == :string_value + helper.response_class_to_method(:integer).should == :integer_value + helper.response_class_to_method(:float).should == :float_value + helper.response_class_to_method(:datetime).should == :datetime_value + helper.response_class_to_method(:date).should == :datetime_value + helper.response_class_to_method(:time).should == :datetime_value + end + end +end \ No newline at end of file