Skip to content

Commit

Permalink
adding date and time fields to pick one and pick any. closes NUBIC#207
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Yoon committed May 22, 2012
1 parent 70f9910 commit db28f52
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 10 deletions.
6 changes: 2 additions & 4 deletions app/inputs/surveyor_check_boxes_input.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class SurveyorCheckBoxesInput < Formtastic::Inputs::CheckBoxesInput
include Surveyor::Helpers::FormtasticCustomInput
def to_html
super
end
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions app/inputs/surveyor_radio_input.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class SurveyorRadioInput < Formtastic::Inputs::RadioInput
include Surveyor::Helpers::FormtasticCustomInput
def to_html
super
end
Expand All @@ -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
3 changes: 3 additions & 0 deletions features/step_definitions/surveyor_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 28 additions & 2 deletions features/surveyor.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
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
17 changes: 17 additions & 0 deletions lib/surveyor/helpers/formtastic_custom_input.rb
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions spec/helpers/formtastic_custom_input_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit db28f52

Please sign in to comment.