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
|
# File 'lib/todd/model.rb', line 16
def self.find query, category_ids, archived = false
found = []
query ||= 'last'
case query
when 'all'
found = Task.find(:all, :conditions => {:category_id => category_ids, :archived => archived})
when 'last'
found << Task.find(:first, :conditions => {:category_id => category_ids, :archived => archived}, :order => "updated_at DESC")
when 'active', 'running', 'started'
found = Task.find(:all, :conditions => {:category_id => category_ids, :running => true, :archived => archived})
when 'inactive', 'stopped'
found = Task.find(:all, :conditions => {:category_id => category_ids, :running => false, :archived => archived})
when /(\d+)\.\.(\d+)/
found = Task.find(:all, :conditions => {:category_id => category_ids, :id => $1.to_i..$2.to_i, :archived => archived})
when /(\d+)/
found << Task.find($1.to_i, :conditions => {:category_id => category_ids, :archived => archived})
else
warn "Invalid query: #{query}"
exit
end
raise ActiveRecord::RecordNotFound if found == [nil]
if block_given?
found.each do |f|
yield f
end
end
found
rescue ActiveRecord::RecordNotFound => e
puts "Not task matched query"
end
|