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
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/acts_as_sweepable.rb', line 13
def sweep(*sources)
options = sources..stringify_keys
options = options.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
time_ago = options.delete(:time)
conditions = options.delete(:conditions)
columns = [options.delete(:columns) || :updated_at].flatten
remove_method = options.delete(:method) || :destroy_all
time_format = options.delete(:format) || :db
time = case time_ago
when /^(\d+)s$/ then Time.now - $1.to_i.second
when /^(\d+)m$/ then Time.now - $1.to_i.minute
when /^(\d+)h$/ then Time.now - $1.to_i.hour
when /^(\d+)d$/ then Time.now - $1.to_i.day
else raise(InvalidTime, time_ago)
end
case time_format
when :db then
time = time.to_s(:db)
when :integer then
time = time.to_i
else
raise(InvalidFormat, time_format)
end
statement = []
columns.each do |column|
statement << "#{column} < '#{time}'"
end
statement = "(#{statement.join(' OR ')})"
conditions = "AND #{conditions} " if conditions
if block_given?
els = self.where("#{statement} #{conditions}")
els.find_each {|el| yield el }
end
res = self.send(remove_method, "#{statement} #{conditions}")
case res
when Integer
res
when nil
0
when Array
res.length
end
end
|