Browse files

if there is an error in the form, then reshow it

  • Loading branch information...
1 parent d80a3ae commit e6c16127ab90a83fc34221d7229d83ece91f255b @drnic drnic committed Feb 23, 2010
View
1 .gitignore
@@ -4,3 +4,4 @@ tmp/**/*
config/database.yml
config/initializers/site_keys.rb
db/*.sqlite3
+capybara-*html
View
46 app/controllers/operate_form_controller.rb
@@ -3,25 +3,7 @@ def show
slug = params[:slug].first
if @google_form = GoogleForm.find_by_slug(slug)
form_html = @google_form.fetch_form_page
-
- doc = Nokogiri::HTML(form_html)
- doc.xpath("//*[@style]").remove_attr('style')
- doc.xpath("//*[@class='ss-legal']").each { |n| n.unlink }
- doc.xpath("//link").each { |n| n.unlink }
- doc.xpath("//style").each { |n| n.unlink }
-
- # add 'clearfix' to wrap
- doc.xpath("//*[@class='ss-form-container']").add_class('clearfix');
-
- google_form = doc.xpath("//form").first
- google_form_action = google_form["action"]
- google_form["action"] = submit_operate_form_url(:id => @google_form.id, :google_form => google_form_action)
-
- css_node = doc.create_element('link')
- css_node["href"] = "/stylesheets/style.css"
- css_node["rel"] = "stylesheet"
- css_node["type"] = "text/css"
- doc.xpath("//head").first.add_child(css_node)
+ doc = clean_up_html(form_html)
render :text => doc.to_html
else
redirect_to 'http://mocra.com'
@@ -37,8 +19,32 @@ def update
if result_html =~ %r{<title>Thanks!<\/title>}
render :text => "Thanks for your answers."
else
- show
+ doc = clean_up_html(result_html)
+ render :text => doc.to_html
end
end
end
+
+ private
+ def clean_up_html(form_html)
+ doc = Nokogiri::HTML(form_html)
+ doc.xpath("//*[@style]").remove_attr('style')
+ doc.xpath("//*[@class='ss-legal']").each { |n| n.unlink }
+ doc.xpath("//link").each { |n| n.unlink }
+ doc.xpath("//style").each { |n| n.unlink }
+
+ # add 'clearfix' to wrap
+ doc.xpath("//*[@class='ss-form-container']").add_class('clearfix');
+
+ google_form = doc.xpath("//form").first
+ google_form_action = google_form["action"]
+ google_form["action"] = submit_operate_form_url(:id => @google_form.id, :google_form => google_form_action)
+
+ css_node = doc.create_element('link')
+ css_node["href"] = "/stylesheets/style.css"
+ css_node["rel"] = "stylesheet"
+ css_node["type"] = "text/css"
+ doc.xpath("//head").first.add_child(css_node)
+ doc
+ end
end
View
20 features/filling_in_form.feature
@@ -7,13 +7,27 @@ Feature: Filling in form
Given I have a Google Form with slug "railsdev"
And I expect Google Form POST to be successful
And I am on "/railsdev"
+ Then I should see no "style" attributes
+ And I should see no "ss-legal" classes
+ And I should see no "style" elements
+ And I should see 1 "link" element
+ And I should not see "Google Docs"
+ When I fill in "Text Question" with "My Answer"
+ And I press "Submit"
+ Then I should see "Thanks for your answers."
+
+ Scenario: Fill in a Google Form with errors
+ Given I have a Google Form with slug "railsdev"
+ And I expect Google Form POST to have errors
+ And I am on "/railsdev"
And I should see no "style" attributes
And I should see no "ss-legal" classes
Then I should see no "style" elements
Then I should see 1 "link" element
+ And I press "Submit"
+ Then I should not see "Thanks for your answers."
+ And I should see "Looks like you have a question or two that still needs to be filled out."
+ And I expect Google Form POST to be successful
When I fill in "Text Question" with "My Answer"
And I press "Submit"
Then I should see "Thanks for your answers."
-
-
-
View
5 features/step_definitions/google_forms_steps.rb
@@ -16,4 +16,9 @@
Given /^I expect Google Form POST to be successful/ do
thankyou = File.read(File.join(Rails.root, "features", "fixtures", "sample_form_thanks.html"))
FakeWeb.register_uri(:post, "http://spreadsheets.google.com/formResponse", :body => thankyou)
+end
+
+Given /^I expect Google Form POST to have errors/ do
+ thankyou = File.read(File.join(Rails.root, "features", "fixtures", "sample_form_with_errors.html"))
+ FakeWeb.register_uri(:post, "http://spreadsheets.google.com/formResponse", :body => thankyou)
end
View
11 features/support/debug.rb
@@ -0,0 +1,11 @@
+After do |scenario|
+ $opened_page_count ||= 0
+ if scenario.status == :failed && ($opened_page_count < 5)
+ save_and_open_page
+ $opened_page_count += 1
+ if ENV['PAUSE']
+ puts "Press any key to continue."
+ STDIN.getc
+ end
+ end
+end

0 comments on commit e6c1612

Please sign in to comment.