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:
- 2526 Bytes
1 | ######################################################################## |
2 | # File:: collectable.rb |
3 | # (C):: Hipposoft 2010, 2011 |
4 | # |
5 | # Purpose:: A database object which can be garbage collected based on |
6 | # its "updated_at" column's value. |
7 | # ---------------------------------------------------------------------- |
8 | # 12-Feb-2010 (ADH): Created. |
9 | # 30-Jan-2011 (ADH): Imported from Artisan. |
10 | ######################################################################## |
11 | |
12 | class Collectable < ActiveRecord::Base |
13 | |
14 | # =========================================================================== |
15 | # CHARACTERISTICS |
16 | # =========================================================================== |
17 | |
18 | self.abstract_class = true |
19 | |
20 | # Timeout period in seconds and garbage collection interval in seconds. |
21 | |
22 | TIMEOUT = 2.hours |
23 | SWEEP_INTERVAL = 1.day |
24 | |
25 | # =========================================================================== |
26 | # PERMISSIONS |
27 | # =========================================================================== |
28 | |
29 | # Anyone can do anything to collectable objects by default. |
30 | |
31 | def self.can_modify?( ignore1, ignore2 ) |
32 | true |
33 | end |
34 | |
35 | # =========================================================================== |
36 | # GENERAL |
37 | # =========================================================================== |
38 | |
39 | # Get rid of expired objects. They time out after TIMEOUT. Pass session |
40 | # details from the current request; this is used to see if this session has |
41 | # already caused a garbage collection sweep recently. If it has, nothing will |
42 | # be done. The minimum interval between sweeps is SWEEP_INTERVAL. Pass also |
43 | # the model subclass to operate upon. By default the method simply destroys |
44 | # objects with a sufficiently old updated_at value, but the caller can |
45 | # optionally override this by passing their own destroy conditions in a 3rd |
46 | # input parameter. |
47 | # |
48 | # Call only from subclasses. |
49 | # |
50 | def self.garbage_collect( session, model, conditions = [ 'updated_at < ?', TIMEOUT.seconds.ago ] ) |
51 | key = "#{ model.table_name }_sweep".to_sym |
52 | |
53 | if ( session[ key ].nil? || |
54 | session[ key ] < SWEEP_INTERVAL.seconds.ago ) |
55 | |
56 | # Although destroy_all may be slow, we must use this because the join |
57 | # tables related to the HABTM relationships need updating too. |
58 | |
59 | begin |
60 | model.transaction do |
61 | model.destroy_all( conditions ) |
62 | end |
63 | rescue |
64 | # Consume all exceptions |
65 | end |
66 | |
67 | session[ key ] = Time.now |
68 | end |
69 | end |
70 | end |