C0 code coverage information
Generated on Tue Dec 04 22:06:26 -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 class RecipesController < ApplicationController
21 before_filter :login_required, :only => [ :edit, :update, :new, :create ]
22
23 after_filter :destroy_cache_with_flash, :only => [:show]
24
25 layout 'master'
26
27 caches_action :show
28
29 cache_sweeper :recipe_sweeper, :only => [ :create, :update, :delete ]
30
31 def initialize
32 @title = 'Recipes'
33 super
34 end
35
36 def index
37 list
38 end
39
40 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
41 verify :method => :post, :only => [ :destroy, :create, :update ],
42 :redirect_to => { :action => :list }
43
44 def show
45 @show_recipe_search = true
46 @recipe = params.include?(:id) ? Recipe.find(params[:id]) : Recipe.find_by_ymdl(params)
47 if @recipe.nil?
48 flash[:notice] = 'Unable to locate the requested recipe.'
49 redirect_to :controller => 'home', :action => 'interstitial'
50 return
51 end
52
53 @title = @recipe.title + ' (Recipe)'
54 @tags = @recipe.tags
55 if (params[:format] == 'json')
56 render :json => @recipe.to_json
57 else
58 render
59 end
60 end
61
62 def new
63 @recipe = Recipe.new
64 vars_for_selection_lists
65 end
66
67 def create
68 o = params[:recipe].dup
69 tool_ids = o.delete(:tool_ids)
70 book_ids = o.delete(:book_ids)
71 @recipe = Recipe.new(o)
72 @recipe.current_user = current_user
73
74 Recipe.transaction do
75 @recipe.save!
76 @recipe.tool_ids = tool_ids if tool_ids && !tool_ids.empty?
77 @recipe.book_ids = book_ids if book_ids && !book_ids.empty?
78 create_ingredients(@recipe, params[:new_preparation])
79 end
80
81 flash[:notice] = "Recipe created successfully."
82
83 if params[:commit] && params[:commit] == "Create"
84 redirect_to '/recipes/' + @recipe.uri
85 else
86 redirect_to :action => 'edit', :id => @recipe.id
87 end
88 rescue ActiveRecord::RecordInvalid => e
89 vars_for_selection_lists
90 render(:action => 'new')
91 end
92
93 def edit
94 @recipe = Recipe.find(params[:id])
95 vars_for_selection_lists
96 end
97
98 def update
99 @recipe = Recipe.find(params[:id])
100 @recipe.current_user = current_user
101
102 Recipe.transaction do
103 @recipe.update_attributes!(params[:recipe])
104 update_ingredients(@recipe, params[:preparation])
105 create_ingredients(@recipe, params[:new_preparation])
106 end
107
108 flash[:notice] = "Recipe updated successfully."
109
110 if params[:commit] && params[:commit].downcase == "save changes"
111 redirect_to '/recipes/' + @recipe.uri
112 else
113 redirect_to :action => 'edit', :id => @recipe.id
114 end
115 rescue ActiveRecord::RecordInvalid => e
116 vars_for_selection_lists
117 render(:action => 'edit')
118 end
119
120 def list
121 @recipes = Recipe.paginate :page => params[:page], :conditions => {:published => true}, :order => 'date desc'
122
123 render :action => 'search'
124 end
125
126 def search
127 @query = params[:query] ? re_ferret_query(params[:query]) : query_from_advanced_search
128
129 if @query.blank? && params[:sort].blank?
130 redirect_to(:action => 'list')
131 return
132 end
133
134 _search(@query, params[:page], params[:sort], params[:dir])
135 end
136
137 def search_by_category
138 @query = "ferret_tags: " + params[:category]
139 _search(@query, params[:page], params[:sort], params[:dir])
140
141 render :action => 'search'
142 end
143
144 def advanced_search
145 end
146
147 private
148 include RecipesHelper
149
150 def _search(query, page=1, sort=nil, dir=nil)
151 @sort = ferret_sort(sort, dir)
152
153 pagination_options = { :sort => @sort, :page => page }
154
155 if page.nil?
156 pagination_options[:page] = 1
157 elsif page.to_i == 0
158 pagination_options[:page] = 1
159 pagination_options[:per_page] = 1000
160 end
161
162 @recipes = Recipe.paginate_search(!query.blank? ? query : "ferret_number_of_ingredients:( >= 000)", pagination_options)
163
164 case query
165 when /\(\s*ferret_tags:\s*([^\)]+)\)/
166 @tags = $1.split(/\s*OR\s*/)
167 when /ferret_tags:\s*(\w+)/
168 @tags = $1
169 end
170 end
171
172 def query_from_advanced_search
173 query = []
174
175 query.push(params[:_all]) if !params[:_all].blank?
176 query.push('"' + params[:_phrase] + '"') if !params[:_phrase].blank?
177 query.push(params[:_any].split(/\s+/).join(' OR ')) if !params[:_any].blank?
178 query.push('-' + params[:_without].split(/\s+/).join(' -')) if !params[:_without].blank?
179
180 query.push(%Q|(title: #{params[:_title]})|) if !params[:_title].blank?
181 query.push(%Q|(ferret_ingredients: #{params[:_ingredient]})|) if !params[:_ingredient].blank?
182
183 if !params[:_ingredient_count_min].blank? && !params[:_ingredient_count_max].blank?
184 query.push(%Q|ferret_number_of_ingredients:( >= #{sprintf('%03d', params[:_ingredient_count_min])} <= #{sprintf('%03d', params[:_ingredient_count_max])})|)
185 elsif !params[:_ingredient_count_max].blank?
186 query.push(%Q|ferret_number_of_ingredients:( <= #{sprintf('%03d', params[:_ingredient_count_max])})|)
187 elsif !params[:_ingredient_count_min].blank?
188 query.push(%Q|ferret_number_of_ingredients:( >= #{sprintf('%03d', params[:_ingredient_count_min])})|)
189 end
190
191 if !params[:_prep_min].blank? && !params[:_prep_max].blank?
192 query.push(%Q|ferret_prep_time:( >= #{sprintf('%03d', params[:_prep_min])} <= #{sprintf('%03d', params[:_prep_max])})|)
193 elsif !params[:_prep_max].blank?
194 query.push(%Q|ferret_prep_time:( <= #{sprintf('%03d', params[:_prep_max])})|)
195 elsif !params[:_prep_min].blank?
196 query.push(%Q|ferret_prep_time:( >= #{sprintf('%03d', params[:_prep_min])})|)
197 end
198
199 if !params[:_cook_min].blank? && !params[:_cook_max].blank?
200 query.push(%Q|ferret_cook_time:( >= #{sprintf('%03d', params[:_cook_min])} <= #{sprintf('%03d', params[:_cook_max])})|)
201 elsif !params[:_cook_max].blank?
202 query.push(%Q|ferret_cook_time:( <= #{sprintf('%03d', params[:_cook_max])})|)
203 elsif !params[:_cook_min].blank?
204 query.push(%Q|ferret_cook_time:( >= #{sprintf('%03d', params[:_cook_min])})|)
205 end
206
207 if params.include?(:_category) && !params[:_category].reject(&:blank?).empty?
208 query.push(%Q|(ferret_tags: #{params[:_category].compact.join(' OR ')})|)
209 end
210
211 query.join(' ')
212 end
213
214 def vars_for_selection_lists
215 @books = Book.find(:all, :order => "title").map{|b| [b.title, b.id]}
216 @tools = Tool.find(:all, :order => "title").map{|t| [t.title, t.id]}
217 @people = Person.find(:all, :order => "name").map{|p| [p.name, p.id]}
218 end
219
220 def create_ingredients(recipe, preparation_instructions)
221 (preparation_instructions || []).reject{|i| i[:ingredient_name].blank?}.each do |attr|
222 attr = attr.dup
223 ingredient_name = attr.delete(:ingredient_name)
224 ingredient_kind = attr.delete(:ingredient_kind)
225 ingredient = Ingredient.find_or_create_by_name_and_kind(ingredient_name, ingredient_kind.blank? ? nil : ingredient_kind)
226 recipe.preparations.create(attr.merge(:ingredient_id => ingredient.id)) if ingredient.valid?
227 end
228 end
229
230 def update_ingredients(recipe, preparation_instructions_attributes)
231 (preparation_instructions_attributes || {}).each_pair do |id, attr|
232 attr = attr.dup
233 ingredient_name = attr.delete(:ingredient_name)
234 ingredient_kind = attr.delete(:ingredient_kind)
235 if ingredient_name.blank?
236 Preparation.find(id).destroy
237 else
238 preparation_instruction = Preparation.find(id)
239 preparation_instruction.update_attributes(attr)
240
241 ingredient_kind = nil if ingredient_kind.blank?
242 preparation_instruction.ingredient = Ingredient.find_or_create_by_name_and_kind(ingredient_name, ingredient_kind)
243 preparation_instruction.save
244 end
245 end
246
247 end
248
249 def destroy_cache_with_flash
250 if @recipe && !flash.empty?
251 expire_action("/recipes/#{@recipe.uri}")
252 end
253 end
254
255 def authorized?
256 current_user.author?
257 end
258 end
Generated using the rcov code coverage analysis tool for Ruby version 0.8.0.