2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
|
# File 'lib/cache_by_page/page_extensions.rb', line 2
def self.included(base)
base.class_eval do
extend ClassMethods
validates_numericality_of :cache_expire_minutes, :allow_nil => false, :only_integer => true,
:message => 'must be a whole number'
def validate
self.cache_expire_minutes = -1 if self.cache_expire_minutes.to_i < -1
if self.cache_expire_minutes.to_i != 0 && self.changes.include?("cache_expire_minutes")
self.cache_expire_time = nil
end
if self.cache_expire_time != nil && self.changes.include?("cache_expire_time")
self.cache_expire_minutes = 0
end
super
end
def cache?
self.cache_expire_minutes.to_i >= 0
end
def cache_override?
self.cache_expire_minutes.to_i > 0 || self.cache_expire_time != nil
end
def cache_duration
@page = Page.find(self.id)
case true
when @page.cache_expire_minutes == 0 && @page.cache_expire_time == nil
if SiteController.respond_to?('cache_timeout')
SiteController.cache_timeout
else
ResponseCache.defaults[:expire_time]
end
when @page.cache_expire_minutes == -1 then nil
when @page.cache_expire_minutes >= 1 then @page.cache_expire_minutes.minutes
when @page.cache_expire_time != nil && @page.cache_expire_time.is_a?(Time) then
next_expire_time = @page.cache_expire_time < Time.now ? @page.cache_expire_time.tomorrow : @page.cache_expire_time
(next_expire_time - Time.now).round
else nil
end
end
def cache_setting
@page = Page.find(self.id)
case true
when @page.cache_expire_minutes == 0 && @page.cache_expire_time == nil then Page.default_caching
when @page.cache_expire_minutes == -1 then "No Caching"
when @page.cache_expire_minutes == 1 then "1 minute"
when @page.cache_expire_minutes > 1 then "#{@page.cache_expire_minutes} minutes"
when @page.cache_expire_time != nil && @page.cache_expire_time.is_a?(Time) then
"Daily at #{@page.cache_expire_time.strftime("%H")}:#{@page.cache_expire_time.strftime("%M")}"
else "Not set"
end
end
alias_method_chain :process, :expire_time
end
end
|