C0 code coverage information
Generated on Tue Dec 04 22:06:27 -0500 2007 with rcov 0.8.0
Code reported as executed by Ruby looks like this...
and this: this line is also marked as covered.
Lines considered as run by rcov, but not reported by Ruby, look like this,
and this: these lines were inferred by rcov (using simple heuristics).
Finally, here's a line marked as not executed.
1 #####
2 #
3 # Copyright 2007 Chris Strom, Robin Strom
4 #
5 # This file is part of EEE Code.
6 #
7 # EEE Code is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # EEE Code is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with EEE Code. If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 class Meal < ActiveRecord::Base
22 has_many :menu_items
23
24 belongs_to :author, :class_name => 'User'
25 require_author
26
27 def after_save
28 if published?
29 new_recipes.reject{|r| r.published?}.each do |r|
30 r.published = true
31 r.current_user = current_user
32 r.save
33 end
34 end
35 end
36
37 has_one :image, :as => :resource
38
39 acts_as_taggable
40
41 alias_method :meal_tags, :tags
42
43 validates_presence_of :date, :title
44
45 def Meal.find_all_from_year(year)
46 Meal.find(:all, :conditions => ["date >= ? and date < ? and published = ?", year.to_s+'-01-01', (year.to_i+1).to_s+'-01-01', true], :order => 'date')
47 end
48
49 def Meal.find_all_from_month(*args)
50 month0 = args.length==1 ? Date.new(args[0].year, args[0].month) : Date.new(*args[0..1])
51 month1 = month0.to_time.next_month.to_date
52 Meal.find(:all, :conditions => ["date >= ? and date < ? and published = ?", month0, month1, true], :order => 'date')
53 end
54
55 def Meal.find_by_date(date)
56 Meal.find(:first, :conditions => { :date => date })
57 end
58
59 def Meal.exists_with_date?(date)
60 Meal.exists?(:date => date)
61 end
62
63 def Meal.find_by_uri(uri)
64 date = Meal.date_from_uri(uri)
65 Meal.find_by_date(date)
66 end
67
68 def Meal.find_by_ymd(o)
69 return nil unless (o.include?(:year) && o.include?(:month) && o.include?(:day))
70 date = Date.new(o[:year].to_i, o[:month].to_i, o[:day].to_i)
71 return Meal.find_by_date(date)
72 end
73
74 def Meal.date_from_uri(uri)
75 parts = uri.split(/\//)[0..2].map(&:to_i)
76 Date.new(*parts)
77 end
78
79 def Meal.exists_with_uri?(uri)
80 date = Meal.date_from_uri(uri)
81 Meal.exists_with_date?(date)
82 end
83
84 def Meal.newest(n=1)
85 meals = Meal.find(:all, :conditions => {:published => true}, :limit=>n, :order => 'date desc')
86 n==1 ? meals[0] : meals
87 end
88
89 def Meal.oldest(n=1)
90 meals = Meal.find(:all, :limit=>n, :order => 'date asc')
91 n==1 ? meals[0] : meals
92 end
93
94 def Meal.unpublished(*attr)
95 attr = [:all] if attr.empty?
96 Meal.with_scope(:find => { :conditions => { :published => false } }) do
97 Meal.find(*attr)
98 end
99 end
100
101 def recipe_links
102 menu_items.map(&:recipe_links).flatten
103 end
104
105 def recipes
106 recipe_links.map{|uri| Recipe.find_by_uri(uri)}.compact
107 end
108
109 def new_recipes
110 recipe_links.find_all{|uri| uri.match(/#{self.uri}/)}.map{|uri| Recipe.find_by_uri(uri)}.compact
111 end
112
113 def uri
114 date.to_formatted_s(:uri).to_s
115 end
116
117 def tags
118 return meal_tags if !meal_tags.empty?
119 recipe_tags = recipes.map(&:tags).flatten.uniq
120 if recipe_tags.any?{|tag| tag == 'meat' || tag == 'chicken'}
121 recipe_tags.reject{|tag| tag == 'vegetarian'}
122 else
123 recipe_tags
124 end
125 end
126
127 def next
128 Meal.find(:first, :conditions => ["date > ? and published = ?", date, true], :order => "date ASC")
129 end
130
131 def prev
132 Meal.find(:first, :conditions => ["date < ? and published = ?", date, true], :order => "date DESC")
133 end
134
135 def to_hash
136 {
137 :title => title,
138 :date => date.to_s,
139 :image => image.filename,
140 :serves => serves
141 }
142 end
143
144 def to_json
145 to_hash.to_json
146 end
147 end
Generated using the rcov code coverage analysis tool for Ruby version 0.8.0.