diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2015-10-04 14:42:15 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2015-10-04 14:42:50 +0200 |
commit | d1c061f1aee62c83e21fb5bf7b26901ab8c12af4 (patch) | |
tree | aa223229555efd4225e097dd901f61762af39c98 | |
parent | 448380ae81e3dfe182b9092880d4a314b39d034a (diff) | |
download | norsk-urskog-registrations-d1c061f1aee62c83e21fb5bf7b26901ab8c12af4.tar.gz norsk-urskog-registrations-d1c061f1aee62c83e21fb5bf7b26901ab8c12af4.tar.bz2 norsk-urskog-registrations-d1c061f1aee62c83e21fb5bf7b26901ab8c12af4.zip |
Flesh out PDF generation.
-rw-r--r-- | lib/member.rb | 4 | ||||
-rw-r--r-- | lib/pdf_form.rb | 68 | ||||
-rw-r--r-- | lib/registration.rb | 7 | ||||
-rw-r--r-- | lib/song.rb | 4 | ||||
-rw-r--r-- | spec/registration_spec.rb | 8 | ||||
-rw-r--r-- | test_pdf.rb | 45 |
6 files changed, 130 insertions, 6 deletions
diff --git a/lib/member.rb b/lib/member.rb index c67915a..0fb69b5 100644 --- a/lib/member.rb +++ b/lib/member.rb @@ -6,4 +6,8 @@ class Member @instrument = params['instrument'] @age = params['age'].to_i end + + def to_s + [name, instrument, age].join(', ') + end end diff --git a/lib/pdf_form.rb b/lib/pdf_form.rb new file mode 100644 index 0000000..7d0aa93 --- /dev/null +++ b/lib/pdf_form.rb @@ -0,0 +1,68 @@ +require 'prawn' +require 'prawn/measurement_extensions' + +class PDFForm + def initialize(band) + @band = band + @document = Prawn::Document.new(:page_size => "A4") + end + + def render + heading + + field("Bandnavn:", @band.name) + field("Hjemsted:", @band.city) + field("Webside:", @band.website) + field("Plateselskap:", @band.label) + field("Kort bio:", @band.short_bio) + + next_line + field("Kontaktperson:", @band.contact.name) + field("Postadresse:", @band.contact.addr) + next_line + field("Telefon:", @band.contact.phone) + field("Epost:", @band.contact.email) + + next_line + field("Medlemmer:", @band.members) + + field("Låter:", @band.songs) + + @document.render_file "#{@band.name}.pdf" + end + + private + + def one_line + @one_line ||= @document.font_size.inspect.to_f * 1.4 + end + + def label_width + 3.2.cm + end + + def heading + @document.text "Påmelding til Norsk Urskog 2016", :size => 20 + end + + def next_line + @document.move_down one_line + end + + def field(title, value) + next_line + y_pos = @document.cursor + @document.text_box title, :at => [@document.bounds.left, y_pos], :width => label_width, :style => :bold + + if value.class == String + @document.text_box value, :at => [label_width, y_pos], :width => @document.bounds.right - label_width + elsif value.class == Array + value.each do |v| + @document.text_box v.to_s, :at => [label_width, y_pos], :width => @document.bounds.right - label_width + y_pos -= one_line + next_line + end + end + + end +end diff --git a/lib/registration.rb b/lib/registration.rb index 51c7663..9826f91 100644 --- a/lib/registration.rb +++ b/lib/registration.rb @@ -1,4 +1,5 @@ -require 'band' +require_relative 'band' +require_relative 'pdf_form' require 'mail' def send_registration_emails_for(band) @@ -13,6 +14,6 @@ def send_registration_emails_for(band) end def generate_pdf_for(band) - Prawn::Document.generate("#{band.name}.pdf") do - end + pdf = PDFForm.new(band) + pdf.render end diff --git a/lib/song.rb b/lib/song.rb index c045ed2..e20b5b8 100644 --- a/lib/song.rb +++ b/lib/song.rb @@ -8,4 +8,8 @@ class Song @performers = params['performers'] @notes = params['notes'] end + + def to_s + "#{title} (#{time})" + end end diff --git a/spec/registration_spec.rb b/spec/registration_spec.rb index c8858cc..2911f6a 100644 --- a/spec/registration_spec.rb +++ b/spec/registration_spec.rb @@ -79,8 +79,9 @@ describe RegistrationApp do } } - class_spy("Prawn::Document") - allow(Prawn::Document).to receive('generate') + @doc_spy = spy("Prawn::Document") + allow(Prawn::Document).to receive('new') { @doc_spy } + allow(@doc_spy).to receive('render_file') post '/submit', 'band' => params end @@ -113,7 +114,8 @@ describe RegistrationApp do end it "generates a PDF file" do - expect(Prawn::Document).to have_received('generate').with("Imbalance.pdf") + expect(Prawn::Document).to have_received('new').with({ :page_size => "A4" }) + expect(@doc_spy).to have_received('render_file').with("Imbalance.pdf") end it 'sends an email to Norsk Urskog' do diff --git a/test_pdf.rb b/test_pdf.rb new file mode 100644 index 0000000..625926a --- /dev/null +++ b/test_pdf.rb @@ -0,0 +1,45 @@ +require_relative 'lib/registration' +require 'prawn' + +params = { + 'name' => 'Imbalance', + 'city' => 'Oslo', + 'website' => 'http://imbalance.no', + 'label' => 'Calculated Imperfection', + 'shortbio' => 'Thrash metal from Norway', + 'contact' => { + 'name' => 'Harald Eilertsen', + 'addr' => "Gamleveien 13\n1289 Snufstad", + 'phone' => '98765432', + 'email' => 'mail@imbalance.no' + }, + 'members' => { + '1' => { + 'name' => 'Harald Eilertsen', + 'instrument' => 'Bass/Vocals', + 'age' => '' + }, + '2' => { + 'name' => 'Welle', + 'instrument' => 'Drums', + 'age' => '' + }, + '3' => { + 'name' => 'Thormodr', + 'instrument' => 'Guitar', + 'age' => '' + } + }, + 'songs' => { + '1' => { + 'title' => 'Bestial by Nature', + 'time' => '02:80', + 'isrc' => '', + 'performers' => "Harald Eilertsen\nThormod Steinert\nLars Welle", + 'composers' => "Harald Eilertsen\nThormod Steinert", + 'notes' => 'Rævrukkje rum kjurr!' + } + } +} + +generate_pdf_for Band.new(params) |