Skip to content
This repository has been archived by the owner on Feb 6, 2022. It is now read-only.

Commit

Permalink
Added barcode generation
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Oct 6, 2008
1 parent 5e0d3fa commit 0741bf2
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 54 deletions.
50 changes: 49 additions & 1 deletion app/models/attendance.rb
@@ -1,3 +1,5 @@
require 'tempfile'

class Attendance < ActiveRecord::Base
belongs_to :happening_page
belongs_to :site_user
Expand All @@ -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
Expand Down Expand Up @@ -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
20 changes: 19 additions & 1 deletion 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

Expand All @@ -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)
Expand All @@ -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
Expand Down
8 changes: 3 additions & 5 deletions app/models/ba_tags.rb
Expand Up @@ -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 %{
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/models/default_attendance_part.html
Expand Up @@ -14,6 +14,10 @@ <h3>You are registered, <r:ba:site_user_name /></h3>
</p>
</form>

<p>
<a href="<r:page:url/>ticket">Print my ticket</a>
</p>

Your proposals:
<ul>
<r:ba:attendance:presentations:each>
Expand Down
15 changes: 15 additions & 0 deletions 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
4 changes: 1 addition & 3 deletions features/create_program.feature
Expand Up @@ -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"
Expand Down
28 changes: 11 additions & 17 deletions features/sign_up.feature
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions 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
6 changes: 0 additions & 6 deletions 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
Expand Down
3 changes: 3 additions & 0 deletions 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
25 changes: 5 additions & 20 deletions features/submit_proposal.feature
Expand Up @@ -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"
Expand All @@ -16,21 +13,15 @@ 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"
And I press "Save"
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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down
11 changes: 10 additions & 1 deletion spec/models/attendance_spec.rb
Expand Up @@ -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

0 comments on commit 0741bf2

Please sign in to comment.