Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 390
- Log:
Initial import of Canvass, a donations-based poll application.
- Author:
- rool
- Date:
- Mon Mar 21 14:58:04 +0000 2011
- Size:
- 2420 Bytes
1 | ######################################################################## |
2 | # File:: invoice_number.rb |
3 | # (C):: Hipposoft 2010, 2011 |
4 | # |
5 | # Purpose:: Keep track of invoice numbers. This is a singleton which |
6 | # records a most recently used invoice number (or zero at the |
7 | # very beginning). The integer invoice number is monotonically |
8 | # increased with total thread-safety so that successive |
9 | # invoices will get successive numbers without fail. The class |
10 | # was originally created due to UK VAT invoice numbering |
11 | # requirements. |
12 | # ---------------------------------------------------------------------- |
13 | # 19-Mar-2010 (ADH): Created. |
14 | # 21-Feb-2011 (ADH): Imported from Artisan. |
15 | ######################################################################## |
16 | |
17 | class InvoiceNumber < ActiveRecord::Base |
18 | |
19 | # =========================================================================== |
20 | # CHARACTERISTICS |
21 | # =========================================================================== |
22 | |
23 | before_create :enforce_singleton |
24 | |
25 | def enforce_singleton |
26 | |
27 | # OK, so this isn't particularly clever, but it's a lot lighter than |
28 | # adding in some plugin like Acts As Singleton just to try and get an |
29 | # analogue of the Ruby core Singleton module. |
30 | # |
31 | # http://ruby-doc.org/stdlib/libdoc/singleton/rdoc/index.html |
32 | |
33 | raise "InvoiceNumber is a singleton" unless InvoiceNumber.count.zero? |
34 | end |
35 | |
36 | # =========================================================================== |
37 | # PERMISSIONS |
38 | # =========================================================================== |
39 | |
40 | # Not applicable. |
41 | |
42 | # =========================================================================== |
43 | # GENERAL |
44 | # =========================================================================== |
45 | |
46 | # Generate the next invoice number. Returns the number to use in the given |
47 | # invoice. Always use an outer transaction so that the invoice number can be |
48 | # assigned to e.g. a purchase and if anything fails, including the creation |
49 | # of the purchase object, the invoice number counter will be rolled back too. |
50 | # |
51 | def self.next! |
52 | return InvoiceNumber.transaction do |
53 | generator = InvoiceNumber.find( :first, :lock => true ) # ...and only! |
54 | number = ( generator.last_number_used += 1 ) |
55 | |
56 | generator.save! |
57 | |
58 | number # Block evaluates to this |
59 | end |
60 | end |
61 | end |