22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/acts_as_expirable/expirable.rb', line 22
def acts_as_expirable(options = {})
ActsAsExpirable.register_expirable(self)
configuration = { :column => "expires_at" }
configuration.update(options) if options.is_a?(Hash)
class_eval %{
include ActsAsExpirable::InstanceMethods
scope :expired, where('#{configuration[:column]} <= UTC_TIMESTAMP()')
scope :unexpired, where('#{configuration[:column]} IS NULL OR #{configuration[:column]} > UTC_TIMESTAMP()')
class << self
def expiry_column
'#{configuration[:column]}'
end
def destroy_expired
expired.destroy_all
end
def delete_expired
expired.delete_all
end
def default_scope
unexpired
end
end
}
case configuration[:default]
when Proc
before_validation configuration[:default], :on => :create do
write_attribute(configuration[:column], configuration[:default].call(self)) if read_attribute(configuration[:column]).nil?
true
end
when NilClass
else
before_validation :on => :create do
write_attribute(configuration[:column], configuration[:default]) if read_attribute(configuration[:column]).nil?
true
end
end
end
|