Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

eagerly generate PDF

  • Loading branch information...
commit bd862208d8e82554560d99abf71704226c3a0f65 1 parent 87fa70f
Josh Susser authored
View
6 app/controllers/registrants_controller.rb
@@ -22,13 +22,7 @@ def ineligible
def download
find_registrant
- end
-
- def pdf
- find_registrant
@registrant.generate_pdf!
- # TODO: serve this as a static asset
- send_file(@registrant.pdf_path, :type => :pdf, :filename => "voter_registration_form.pdf")
end
def current_step
View
12 app/models/registrant.rb
@@ -319,7 +319,7 @@ def form_date_of_birth
end
def generate_pdf!
- unless File.exists?(pdf_path)
+ unless File.exists?(pdf_file_path)
Tempfile.open("nvra-#{to_param}") do |f|
f.puts to_xfdf
f.close
@@ -330,7 +330,7 @@ def generate_pdf!
def merge_pdf(tmp)
classpath = ["$CLASSPATH", File.join(Rails.root, "lib/pdf_merge/lib/iText-2.1.7.jar"), File.join(Rails.root, "lib/pdf_merge/out/production/Rocky_pdf")].join(":")
- `java -classpath #{classpath} PdfMerge #{nvra_template_path} #{tmp.path} #{pdf_path}`
+ `java -classpath #{classpath} PdfMerge #{nvra_template_path} #{tmp.path} #{pdf_file_path}`
end
def nvra_template_path
@@ -338,8 +338,12 @@ def nvra_template_path
end
def pdf_path
- FileUtils.mkdir_p(File.join(Rails.root, "public", "pdf"))
- File.join(Rails.root, "public", "pdf", "nvra-#{to_param}.pdf")
+ "/pdf/nvra-#{to_param}.pdf"
+ end
+
+ def pdf_file_path
+ FileUtils.mkdir_p(File.join(Rails.root, "pdf"))
+ File.join(Rails.root, pdf_path)
end
def check_ineligible
View
2  app/views/registrants/download.html.erb
@@ -1,6 +1,6 @@
<h1><%= I18n.t('txt.registration.download_header') %></h1>
<div class="download">
<h2><%= I18n.t('txt.registration.instructions.download') %></h2>
- <p><%= link_to image_tag("#{I18n.locale}/button_printform.gif", :alt => I18n.t('txt.button.download')), pdf_registrant_path(@registrant) %></p>
+ <p><%= link_to image_tag("#{I18n.locale}/button_printform.gif", :alt => I18n.t('txt.button.download')), @registrant.pdf_path %></p>
<p><%= I18n.t('txt.registration.instructions.download_2') %></p>
</div>
View
2  config/routes.rb
@@ -1,5 +1,5 @@
ActionController::Routing::Routes.draw do |map|
- map.resources "registrants", :only => [:new, :create, :show, :update], :member => {:ineligible => :get, :download => :get, :pdf => :get} do |reg|
+ map.resources "registrants", :only => [:new, :create, :show, :update], :member => {:ineligible => :get, :download => :get} do |reg|
reg.resource "step_1", :controller => "step1", :only => [:show, :update]
reg.resource "step_2", :controller => "step2", :only => [:show, :update]
reg.resource "step_3", :controller => "step3", :only => [:show, :update]
View
1  features/enter_step_5_data.feature
@@ -15,6 +15,7 @@ Feature: Step 5
And I press "registrant_submit"
Then I should see "Review"
+ @cleanup_pdf
Scenario: enter data
Given I have completed step 4
When I go to the step 5 page
View
2  features/finish.feature
@@ -3,9 +3,9 @@ Feature: Finish
As a registrant
I want to download reg form PDF
+ @cleanup_pdf
Scenario: download form
Given I have completed step 5
And I have not downloaded the PDF before
When I go to the download page
- And I follow "Download PDF!"
Then I should see a new download
View
9 features/step_definitions/registration_steps.rb
@@ -22,7 +22,7 @@
end
Given /^I have not downloaded the PDF before$/ do
- `rm #{@registrant.pdf_path}`
+ `rm #{@registrant.pdf_file_path}`
end
Given /there is localized state data/ do
@@ -30,8 +30,7 @@
end
Then /^I should see a new download$/ do
- assert File.exists?(@registrant.pdf_path)
- `rm #{@registrant.pdf_path}`
+ assert File.exists?(@registrant.pdf_file_path)
end
Then /^I should see my email$/ do
@@ -58,3 +57,7 @@
zip_prefix = GeoState.zip3map.index(state.abbreviation)
When %Q{I fill in "zip code" with "#{zip_prefix}01"}
end
+
+After('@cleanup_pdf') do
+ `rm #{@registrant.pdf_file_path}`
+end
View
1  pdf/.gitignore
@@ -0,0 +1 @@
+*.pdf
View
1  public/pdf
View
21 spec/controllers/registrants_controller_spec.rb
@@ -89,29 +89,18 @@
end
describe "download" do
- it "provides a link to download the PDF" do
+ before(:each) do
@registrant = Factory.create(:maximal_registrant)
+ `touch #{@registrant.pdf_file_path}`
+ end
+ it "provides a link to download the PDF" do
get :download, :id => @registrant.to_param
assert_not_nil assigns[:registrant]
assert_response :success
assert_template "download"
end
- end
-
- describe "PDF" do
- before(:each) do
- @registrant = Factory.create(:maximal_registrant)
- `touch #{@registrant.pdf_path}`
- end
-
- it "downloads the PDF to the browser" do
- get :pdf, :id => @registrant.to_param
- assert_equal 'application/pdf', response.content_type
- end
-
after(:each) do
- `rm #{@registrant.pdf_path}`
+ `rm #{@registrant.pdf_file_path}`
end
end
-
end
View
12 spec/models/registrant_spec.rb
@@ -371,25 +371,25 @@
describe "merge" do
before(:each) do
@registrant = Factory.build(:maximal_registrant)
- stub(@registrant).merge_pdf { `touch #{@registrant.pdf_path}` }
+ stub(@registrant).merge_pdf { `touch #{@registrant.pdf_file_path}` }
end
it "generates PDF with merged data" do
- `rm -f #{@registrant.pdf_path}`
- assert_difference('Dir[File.join(RAILS_ROOT, "public/pdf/*")].length') do
+ `rm -f #{@registrant.pdf_file_path}`
+ assert_difference('Dir[File.join(RAILS_ROOT, "pdf/*")].length') do
@registrant.generate_pdf!
end
end
it "returns PDF if already exists" do
- `touch #{@registrant.pdf_path}`
- assert_no_difference('Dir[File.join(RAILS_ROOT, "public/pdf/*")].length') do
+ `touch #{@registrant.pdf_file_path}`
+ assert_no_difference('Dir[File.join(RAILS_ROOT, "pdf/*")].length') do
@registrant.generate_pdf!
end
end
after do
- `rm #{@registrant.pdf_path}`
+ `rm #{@registrant.pdf_file_path}`
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.