Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added barcode generation

  • Loading branch information...
commit 0741bf2a08ecca4a80b4cf6b5e5ab3d8ec6ede7a 1 parent 5e0d3fa
@aslakhellesoy authored
View
50 app/models/attendance.rb
@@ -1,3 +1,5 @@
+require 'tempfile'
+
class Attendance < ActiveRecord::Base
belongs_to :happening_page
belongs_to :site_user
@@ -8,12 +10,13 @@ class Attendance < ActiveRecord::Base
validates_presence_of :happening_page_id
validates_uniqueness_of :site_user_id, :scope => :happening_page_id, :message => "already signed up"
+ validates_uniqueness_of :ticket_code
validate :site_user_valid
validate :price_code_valid
before_save :update_price
- after_create :activate_user, :send_signup_confirmation_email
+ after_create :create_ticket_code, :activate_user, :send_signup_confirmation_email
def price_code=(pc)
@price_code = pc
@@ -69,5 +72,50 @@ def save_presentation(presentation_page)
def send_signup_confirmation_email
happening_page.send_signup_confirmation_email(site_user)
end
+
+ TICKET_SALT = "f00k teH bark0de-hax0rz"
+ def create_ticket_code
+ self.ticket_code = Digest::MD5.hexdigest("#{TICKET_SALT}#{id}")
+ save
+ end
+
+ # Returns a ticket as a Prawn document
+ def ticket
+ u = site_user
+ barcode_file = write_barcode_image
+
+ Prawn::Document.new(:page_size => "A5") do
+ font "#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf"
+
+ bounding_box [0,500], :width => 300, :height => 140 do
+ text "Billett til Smidig 2008", :at => [15,115], :size => 20
+ text "Navn", :at => [15,100]
+ text u.name, :at => [65,100]
+ text "E-post", :at => [15,85]
+ text u.email, :at => [65,85]
+
+ text "Firma", :at => [15,70]
+ text u.company, :at => [65,70]
+
+ stroke do
+ line bounds.top_left, bounds.top_right
+ line bounds.top_left, bounds.bottom_left
+ line bounds.bottom_left, bounds.bottom_right
+ line bounds.top_right, bounds.bottom_right
+ end
+
+ image barcode_file, :at => [15,60], :scale => 0.7
+ text u.id.to_s, :at => [15,10], :size => 10
+ end
+ end
+ end
+ def write_barcode_image
+ barcode = Barby::Code128B.new(ticket_code)
+ path = "/tmp/barcode-#{id}.png"
+ File.open(path, 'w') do |f|
+ f.write barcode.to_png(:height => 50, :margin => 0)
+ end
+ path
+ end
end
View
20 app/models/attendance_page.rb
@@ -1,3 +1,8 @@
+require 'barby'
+require 'barby/outputter/png_outputter'
+require 'prawn'
+require 'digest/md5'
+
class AttendancePage < Page
before_validation_on_create :create_default_content
@@ -12,6 +17,15 @@ def create_default_content
parts << PagePart.new(:name => 'body', :content => read_file('default_attendance_part.html'))
end
+ def find_by_url(url, live = true, clean = false)
+ if url =~ %r{^#{ self.url }(.+)/$}
+ @special_slug = $1
+ self
+ else
+ super
+ end
+ end
+
def process(request, response)
@site_user = controller.current_site_user
@attendance = happening_page.attendance(@site_user)
@@ -24,7 +38,11 @@ def process(request, response)
super
end
else
- super
+ if @special_slug == 'ticket'
+ controller.send :send_data, @attendance.ticket.render, :type => 'application/pdf', :filename => 'smidig2008-billett.pdf'
+ else
+ super
+ end
end
else
# Nothing allowed here unless we're signed up
View
8 app/models/ba_tags.rb
@@ -196,12 +196,10 @@ def ba_input_tag(tag)
to the happening.
*Usage:*
- <pre><code><r:ba:attendance:price [free="free_text"]/></code></pre>
+ <pre><code><r:ba:attendance:price/></code></pre>
}
tag "ba:attendance:price" do |tag|
- price = tag.locals.attendance.actual_price
- free = tag.attr['free'] || '0'
- price ? "#{price.currency} #{price.amount}" : free
+ "#{tag.locals.attendance.currency} #{tag.locals.attendance.amount}"
end
desc %{
@@ -362,7 +360,7 @@ def ba_input_tag(tag)
tag.expand
end
- [:name, :company, :email].each do |field|
+ [:name, :company, :email, :telephone].each do |field|
tag "ba:presenter:#{field}" do |tag|
tag.locals.site_user.__send__(field)
end
View
4 app/models/default_attendance_part.html
@@ -14,6 +14,10 @@
</p>
</form>
+<p>
+ <a href="<r:page:url/>ticket">Print my ticket</a>
+</p>
+
Your proposals:
<ul>
<r:ba:attendance:presentations:each>
View
15 db/migrate/015_add_attendance_ticket_code.rb
@@ -0,0 +1,15 @@
+class AddAttendanceTicketCode < ActiveRecord::Migration
+ def self.up
+ add_column :attendances, :ticket_code, :string
+ attendances = Attendance.find(:all)
+ Attendance.transaction do
+ attendances.each do |attendance|
+ attendance.create_ticket_code
+ end
+ end
+ end
+
+ def self.down
+ remove_column :attendances, :ticket_code
+ end
+end
View
4 features/create_program.feature
@@ -10,9 +10,7 @@ Feature: Create Program
Then I should see "TBA"
Scenario: Two presentations
- Given an "active" site_user named "John" exists
- And there is a "Beerfest" happening page with parts
- And "John" is signed up for "Beerfest"
+ Given "John" is signed up for "Beerfest"
And there is a simple "Day 1" program for "Beerfest"
And "John" has a "Cats" presentation in "Beerfest" slot "1"
And "John" has a "Dogs" presentation in "Beerfest" slot "2"
View
28 features/sign_up.feature
@@ -4,19 +4,11 @@ Feature: Sign up
I want to sign up for a conference
Scenario: New user
- Given I am logged out
- And there is a "Beerfest" happening page with parts
- When I view the "Beerfest" signup page
- And I fill in personal info for "Johannes"
- And I press "Sign up"
+ Given "Johannes" is signed up for "Beerfest"
Then I should see "You are registered, Johannes"
Scenario: Receive confirmation email
- Given I am logged out
- And there is a "Beerfest" happening page with parts
- When I view the "Beerfest" signup page
- And I fill in personal info for "Johannes"
- And I press "Sign up"
+ Given "Johannes" is signed up for "Beerfest"
Then "Johannes" should receive an email with "Hi, Johannes"
Scenario: From email link, good password
@@ -40,23 +32,25 @@ Feature: Sign up
And the site_user named "Aslak" should be "pending"
Scenario: Existing attendance, logged out, correct password
- Given an "active" site_user named "Johannes" exists
+ Given "Johannes" is signed up for "Beerfest"
And I am logged out
- And there is a "Beerfest" happening page with parts
- And "Johannes" is signed up for "Beerfest"
When I view the "Beerfest" signup page
And I fill in personal info for "Johannes"
And I press "Sign up"
Then I should see "The Email address has already been taken"
Scenario: Existing attendance, logged in
- Given an "active" site_user named "Aslak" exists
- And I am logged in as "Aslak"
- And there is a "Beerfest" happening page with parts
- And "Aslak" is signed up for "Beerfest"
+ Given "Aslak" is signed up for "Beerfest"
When I view the "Beerfest" signup page
Then I should see "You are registered, Aslak"
+ # tags: barcode_reg
+ Scenario: Existing attendance, logged in
+ Given "Aslak" is signed up for "Beerfest"
+ When I view the "Beerfest" signup page
+ And I follow "Print my ticket"
+ Then I should receive a application/pdf representation
+
Scenario: New site user, bad password confirmation
Given I am logged out
And there is a "Beerfest" happening page with parts
View
7 features/steps/aggregate_steps.rb
@@ -0,0 +1,7 @@
+Given /"(\w+)" is signed up for "(\w+)"/ do |name, happening|
+ Given %{I am logged out}
+ And %{there is a "#{happening}" happening page with parts}
+ When %{I view the "#{happening}" signup page}
+ And %{I fill in personal info for "#{name}"}
+ And %{I press "Sign up"}
+end
View
6 features/steps/attendance.rb
@@ -1,9 +1,3 @@
-Given /"(\w+)" is signed up for "(\w+)"/ do |site_user_name, title|
- site_user = SiteUser.find_by_name(site_user_name)
- page = Page.find_by_title(title)
- attendance = Attendance.create! :site_user => site_user, :happening_page => page
-end
-
When /I view the "(\w+)" signup page/ do |title|
happening_page = Page.find_by_title(title)
visits happening_page.signup_page.url
View
3  features/steps/rails_steps.rb
@@ -0,0 +1,3 @@
+Then /I should receive a (.*) representation/ do |mime_type|
+ response.headers['type'].should == mime_type
+end
View
25 features/submit_proposal.feature
@@ -4,10 +4,7 @@ Feature: Submit presentation
I want to submit presentation proposals for a conference
Scenario: Successful presentation submission
- Given an "active" site_user named "Aslak" exists
- And I am logged in as "Aslak"
- And there is a "Beerfest" happening page with parts
- And "Aslak" is signed up for "Beerfest"
+ Given "Aslak" is signed up for "Beerfest"
When I visit the "Beerfest" my-page
And I follow "Register new talk"
And I fill in "How to make Bearnaise" for "Title"
@@ -16,10 +13,7 @@ Feature: Submit presentation
Then I should see "How to make Bearnaise"
Scenario: Failed presentation submission
- Given an "active" site_user named "Aslak" exists
- And I am logged in as "Aslak"
- And there is a "Beerfest" happening page with parts
- And "Aslak" is signed up for "Beerfest"
+ Given "Aslak" is signed up for "Beerfest"
When I visit the "Beerfest" my-page
And I follow "Register new talk"
And I fill in "How to make Bearnaise" for "Title"
@@ -27,10 +21,7 @@ Feature: Submit presentation
Then I should see "Body can't be blank"
Scenario: Change existing presentation
- Given an "active" site_user named "Aslak" exists
- And I am logged in as "Aslak"
- And there is a "Beerfest" happening page with parts
- And "Aslak" is signed up for "Beerfest"
+ Given "Aslak" is signed up for "Beerfest"
When I visit the "Beerfest" my-page
And I follow "Register new talk"
And I fill in "How to make Bearnaise" for "Title"
@@ -43,10 +34,7 @@ Feature: Submit presentation
And I should not see "How to make Bearnaise"
Scenario: Viewing published presentation
- Given an "active" site_user named "Aslak" exists
- And I am logged in as "Aslak"
- And there is a "Beerfest" happening page with parts
- And "Aslak" is signed up for "Beerfest"
+ Given "Aslak" is signed up for "Beerfest"
When I visit the "Beerfest" my-page
And I follow "Register new talk"
And I fill in "How to make Bearnaise" for "Title"
@@ -57,10 +45,7 @@ Feature: Submit presentation
Then I should see "Best sauce in the world"
Scenario: Add Web 2.0 tags to presentation
- Given an "active" site_user named "Aslak" exists
- And I am logged in as "Aslak"
- And there is a "Beerfest" happening page with parts
- And "Aslak" is signed up for "Beerfest"
+ Given "Aslak" is signed up for "Beerfest"
When I visit the "Beerfest" my-page
And I follow "Register new talk"
And I fill in "How to make Bearnaise" for "Title"
View
11 spec/models/attendance_spec.rb
@@ -80,7 +80,16 @@
lambda do
Attendance.create! :site_user => @site_user, :happening_page => @happening, :price_code => 'CHEAP'
- end.should raise_error(ActiveRecord::RecordInvalid, %{Validation failed: Price code no longer available, used by <a href="mailto:oldsite_user@gmail.com">Oldie</a>})
+ end.should raise_error(ActiveRecord::RecordInvalid, %r{Validation failed: Price code no longer available, used by <a href="mailto:oldsite_user@gmail.com">Oldie</a>})
+ end
+
+
+ it "should have a ticket as PDF" do
+ attendance = Attendance.new
+ attendance.site_user = @site_user
+ attendance.happening_page = @happening
+ attendance.save!
+ attendance.ticket.should_not be_nil
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.