Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

GoogleForm#title is updated each time the form is downloaded

  • Loading branch information...
1 parent 89ccd77 commit f3db3b80ac98fc65bd1cda41bdbcd343ae5755e8 @drnic drnic committed
4 app/controllers/operate_form_controller.rb
@@ -6,9 +6,9 @@ def index
def show
slug = params[:slug].first
if @google_form = GoogleForm.find_by_slug(slug)
- response = @google_form.fetch_form_page
+ response = @google_form.fetch_form_page!
form_html = response.body
- doc = clean_up_html(form_html)
+ doc = clean_up_html(form_html)
render :text => doc.to_html
redirect_to '/'
13 app/models/google_form.rb
@@ -8,6 +8,16 @@ def fetch_form_page
uri = URI.parse(google_form_url)
req ="#{uri.path}?#{uri.query}")
response = {|h| h.request(req)}
+ if response.is_a?(Net::HTTPSuccess)
+ self.title = extract_form_title(response.body)
+ end
+ response
+ end
+ # If the title is modified, then save the GoogleForm before returning response
+ def fetch_form_page!
+ response = self.fetch_form_page
@@ -35,9 +45,8 @@ def clean_formkey
def validate_formkey_is_valid
- case response = fetch_form_page
+ case fetch_form_page
when Net::HTTPSuccess
- self.title = extract_form_title(response.body)
errors.add(:formkey, "is not a valid Google Forms key or URL or error connecting to Google")
69 features/fixtures/sample_form_with_new_title.html
@@ -0,0 +1,69 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" ""><html><head><link rel="shortcut icon" href="" type="image/x-icon">
+<meta http-equiv="Content-type" content="text/html; charset=utf-8">
+<title>NEW TITLE</title>
+<link href='//' type='text/css' rel='stylesheet'><link href='//' type='text/css' rel='stylesheet'>
+<body class="ss-base-body" dir="ltr"><div class="ss-form-container">
+<div class="ss-form-heading"><h1 class="ss-form-title">NEW TITLE</h1>
+<hr class="ss-email-break" style="display:none;">
+<div class="ss-required-asterisk">* Required</div></div>
+<div class="ss-form"><form action=";ifq" method="POST" id="ss-form">
+<div class="errorbox-good">
+<div class="ss-item ss-item-required ss-text"><div class="ss-form-entry"><label class="ss-q-title" for="entry_0">Text Question
+<span class="ss-required-asterisk">*</span></label>
+<label class="ss-q-help" for="entry_0">Text Question Help Text</label>
+<input type="text" name="entry.0.single" value="" class="ss-q-short" id="entry_0"></div></div></div>
+<br> <div class="ss-item ss-grid"><div class="ss-form-entry"><label class="ss-q-title" for="entry_2">Table of questions
+<label class="ss-q-help" for="entry_2"></label>
+<div class="errorbox-component errorbox-good"><table border="0" cellpadding="5" cellspacing="0"><thead><tr><td class="ss-gridnumbers" style="width: 24%;"></td>
+<td class="ss-gridnumbers" style="width: 6%;"></td>
+<td class="ss-gridnumbers" style="width: 12%;"><label class="ss-gridnumber">1</label></td> <td class="ss-gridnumbers" style="width: 12%;"><label class="ss-gridnumber">2</label></td> <td class="ss-gridnumbers" style="width: 12%;"><label class="ss-gridnumber">3</label></td> <td class="ss-gridnumbers" style="width: 12%;"><label class="ss-gridnumber">4</label></td> <td class="ss-gridnumbers" style="width: 12%;"><label class="ss-gridnumber">5</label></td>
+<td class="ss-gridnumbers" style="width: 6%;"></td></tr></thead>
+<tbody><div class="errorbox-good"><tr class="ss-gridrow ss-grid-row-odd"><td class="ss-gridrow ss-leftlabel ss-gridrow-leftlabel" style="width: 24%;">Item 1</td>
+<td class="ss-gridrow" style="width: 6%;"></td>
+<td class="ss-gridrow" style="width: 12%;"><input type="radio" name="" value="1" class="ss-q-radio" id="group_3_1"></td> <td class="ss-gridrow" style="width: 12%;"><input type="radio" name="" value="2" class="ss-q-radio" id="group_3_2"></td> <td class="ss-gridrow" style="width: 12%;"><input type="radio" name="" value="3" class="ss-q-radio" id="group_3_3"></td> <td class="ss-gridrow" style="width: 12%;"><input type="radio" name="" value="4" class="ss-q-radio" id="group_3_4"></td> <td class="ss-gridrow" style="width: 12%;"><input type="radio" name="" value="5" class="ss-q-radio" id="group_3_5"></td>
+<td class="ss-gridrow" style="width: 6%;"></td></tr></div></tbody></table></div></div></div>
+<input type="hidden" name="pageNumber" value="0">
+<input type="hidden" name="backupCache" value="">
+<div class="ss-item ss-navigate"><div class="ss-form-entry">
+<input type="submit" name="submit" value="Submit"></div></div></form>
+<script type="text/javascript">
+ (function() {
+var error = false;
+var divs = document.getElementById('ss-form').
+for (var j in divs) {
+if (divs[j].className == 'errorbox-bad') {
+error = true;
+if (!error) {
+for (var i in divs) {
+if (divs[i].className == 'ss-form-entry' && divs[i].
+firstChild.className == 'ss-q-title') {
+ </script></div>
+<div class="ss-footer"><div class="ss-attribution"></div>
+<div class="ss-legal"><span class="ss-powered-by">Powered by <a href="">Google Docs</a></span>
+<span class="ss-terms"><small><a href=";">Report Abuse</a>
+<a href="">Terms of Service</a>
+<a href="">Additional Terms</a></small></span></div></div></div></body></html>
14 features/google_forms.feature
@@ -44,3 +44,17 @@ Feature: Add new google forms
Given I am on the home page
Then I should see "Sample form for Testing"
+ Scenario: Form title is auto-updated each time form is fetched
+ Given I expect Google Form "dFRUNHpLZmZHbVRrdlpMRnlJclBLc0E6MA" to be fetched
+ And I am on the new forms page
+ When I fill in "Slug" with "railsdev"
+ And I fill in "Form Key" with "dFRUNHpLZmZHbVRrdlpMRnlJclBLc0E6MA"
+ And I press "Create"
+ Then I should see "Your form 'Sample form for Testing' has been successfully customized."
+ Given I am on the home page
+ And I expect Google Form "dFRUNHpLZmZHbVRrdlpMRnlJclBLc0E6MA" to be fetched with new title
+ When I follow "Sample form for Testing"
+ Then I should not see "Sample form for Testing"
+ Then I should see "NEW TITLE"
5 features/step_definitions/google_forms_steps.rb
@@ -14,6 +14,11 @@
FakeWeb.register_uri(:get, "{formkey}", :body => sample_form)
+Given /^I expect Google Form "([^\"]*)" to be fetched with new title$/ do |formkey|
+ sample_form =, "features", "fixtures", "sample_form_with_new_title.html"))
+ FakeWeb.register_uri(:get, "{formkey}", :body => sample_form)
Given /^I expect Google Form "([^\"]*)" to be invalid$/ do |formkey|
FakeWeb.register_uri(:get, "{formkey}", :body => "Go away!", :status => [404, "Not Found"])
2  rerun.txt
@@ -1 +1 @@

0 comments on commit f3db3b8

Please sign in to comment.
Something went wrong with that request. Please try again.