/
helpers.rb
325 lines (263 loc) · 9.96 KB
/
helpers.rb
1
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
module Eee
module Helpers
require 'RedCloth'
def hours(minutes)
h = minutes.to_i / 60
m = minutes.to_i % 60
h > 0 ? "#{h} hours" : "#{m} minutes"
end
def amazon_url(asin)
"http://www.amazon.com/exec/obidos/ASIN/#{asin}/eeecooks-20"
end
def categories(context)
categories = %w{Italian Asian Latin Breakfast Chicken Fish Meat Salad Vegetarian}
links = categories.map do |category|
%Q|<li>#{recipe_category_link(context, category)}</li>|
end
links << %Q|<li><a href="/recipes/search?q=">Recipes</a></li>|
%Q|<ul id="eee-categories">#{links}</ul>|
end
def recipe_category_link(recipe, category)
recipes = recipe.is_a?(Array) ? recipe : [recipe]
href = "/recipes/search?q=category:#{category.downcase}"
if recipes.any? { |r|
r['tag_names'] &&
r['tag_names'].include?(category.downcase)
}
%Q|<a class="active" href="#{href}">#{category}</a>|
else
%Q|<a href="#{href}">#{category}</a>|
end
end
def wiki(original)
text = (original || '').dup
text.gsub!(/\b(\d+)F/, "\\1° F")
text.gsub!(/\[kid:(\w+)\]/m) { |kid| kid_nicknames[$1] }
text.gsub!(/\[recipe:(\S+)\]/m) { |r| recipe_link($1) }
text.gsub!(/\[recipe:(\S+)\s(.+?)\]/m) { |r| recipe_link($1, $2) }
text.gsub!(/\[meal:(\S+)\]/m) { |m| meal_link($1) }
text.gsub!(/\[meal:(\S+)\s(.+?)\]/m) { |m| meal_link($1, $2) }
RedCloth.new(text).to_html
end
def kid_nicknames
@@kid_kicknames ||= JSON.parse(RestClient.get("#{_db}/kids"))
end
def _db
self.class.send(:class_variable_get, "@@db")
end
def wiki_recipe(text)
if text =~ /\[recipe:([-\/\w]+)/
permalink = $1.gsub(/\//, '-')
JSON.parse(RestClient.get("#{_db}/#{permalink}"))
end
end
def recipe_link(link, title=nil)
permalink = link.gsub(/\//, '-')
recipe = JSON.parse(RestClient.get("#{_db}/#{permalink}"))
%Q|<a href="/recipes/#{recipe['_id']}">#{title || recipe['title']}</a>|
end
def meal_link(link, title=nil)
permalink = link.gsub(/\//, '-')
title ||= JSON.parse(RestClient.get("#{_db}/#{permalink}"))['title']
%Q|<a href="/meals/#{link}">#{title}</a>|
end
def image_link(doc, options={ })
return nil unless doc['_attachments']
filename = doc['_attachments'].
keys.
detect{ |f| f =~ /jpg/ }
return nil unless filename
attrs = options.map{|kv| %Q|#{kv.first}="#{kv.last}"|}.join(" ")
%Q|<img #{attrs} src="/images/#{doc['_id']}/#{filename}"/>|
end
def pagination(query, results)
total = results['total_rows']
limit = results['limit']
skip = results['skip']
last_page = (total + limit - 1) / limit
current_page = skip / limit + 1
link = base_pagination_link(query, results)
links = []
links << edge_page_link(current_page == 1, link, current_page-1, "« Previous")
links << page_link(link, 1) if current_page != 1
start_window = current_page > 4 ? current_page - 3 : 2
links << "..." if start_window > 2
links << (start_window...current_page).map { |p| page_link(link, p) }
links << %Q|<span class="current">#{current_page}</span>|
end_window = current_page + 3 < last_page ? current_page + 3 : last_page - 1
links << (current_page+1..end_window).map { |p| page_link(link, p) }
links << "..." if end_window < last_page - 1
links << page_link(link, last_page) if current_page != last_page
links << edge_page_link(current_page == last_page, link, current_page+1, "Next »")
%Q|<div class="pagination">#{links.join}</div>|
end
def base_pagination_link(query, results)
link = "/recipes/search?q=#{query}"
if results['sort_order']
link += "&sort=#{results['sort_order'].first['field']}"
if results['sort_order'].first['reverse']
link += "&order=desc"
end
end
return link
end
def edge_page_link(disabled, link, page, text)
disabled ?
%Q|<span class="inactive">#{text}</span>| :
page_link(link, page, text)
end
def page_link(link, page, text=nil)
%Q|<a href="#{link}&page=#{page}">#{text || page}</a>|
end
def sort_link(text, sort_field, results, options = { })
id = "sort-by-#{text.downcase}"
query = options[:query] || results['query']
# Current state of sort on the requested field
sort_field_current =
results["sort_order"] &&
results["sort_order"].detect { |sort_options|
sort_options["field"] == sort_field
}
if sort_field_current
order = sort_field_current["reverse"] ? "" : "&order=desc"
elsif options[:reverse]
order = "&order=desc"
end
url = "/recipes/search?q=#{query}&sort=#{sort_field}#{order}"
%Q|<a href="#{url}" id="#{id}">#{text}</a>|
end
# TODO: use CouchDB view directly here, with limit=1 to determine
# the next record
def link_to_adjacent_view_date(current, couch_view, options={})
# If looking for the record previous to this one, then we seek a
# date prior to the current one - build a Proc capable of
# finding that
compare = options[:previous] ?
Proc.new { |date_fragment, current| date_fragment < current} :
Proc.new { |date_fragment, current| date_fragment > current}
# If looking for the record previous to this one, then we need
# to reverse the list before using the compare Proc to detect
# the record
next_result = couch_view.
send(options[:previous] ? :reverse : :map).
detect{|result| compare[result['key'], current.to_s]}
# If a next record was found, then return link text - either by
if next_result
if block_given?
yield next_result['key'], next_result['value']
else
next_uri = next_result['key'].gsub(/-/, '/')
%Q|<a href="/meals/#{next_uri}">#{next_result['key']}</a>|
end
else
nil
end
end
def month_text(date_frag)
Date.parse("#{date_frag}-01").strftime("%B %Y")
end
def breadcrumbs(date, context=nil)
crumbs = [ %Q|<a href="/">home</a>| ]
if context == :year
crumbs << %Q|<span>#{date.year}</span>|
else
crumbs << %Q|<a href="/meals/#{date.year}">#{date.year}</a>|
end
if context == :month
crumbs << %Q|<span>#{date.strftime("%B")}</span>|
elsif context == :day || context == nil
crumbs << %Q|<a href="#{date.strftime("/meals/%Y/%m")}">#{date.strftime("%B")}</a>|
end
if context == :day
crumbs << %Q|<span>#{date.day}</span>|
elsif context == nil
crumbs << %Q|<a href="#{date.strftime("/meals/%Y/%m/%d")}">#{date.day}</a>|
end
crumbs.join(" > ")
end
def couch_recipe_update_of(permalink)
url = "#{_db}/_design/recipes/_view/update_of?key=%22#{permalink}%22"
data = RestClient.get url
results = JSON.parse(data)['rows']
results.first && results.first['value']
end
def recipe_update_of(permalink)
previous = couch_recipe_update_of(permalink)
if previous
links = previous.map do |update_permalink|
date_str = Date.parse(update_permalink).strftime("%B %e, %Y")
%Q|<a href="/recipes/#{update_permalink}">#{date_str}</a>|
end
%Q|<span class="update-of">| +
%Q|This is an update of a previous recipe: | +
links.join(", ") +
%Q|</span>|
end
end
def couch_recipe_updated_by(permalink)
url = "#{_db}/_design/recipes/_view/updated_by?key=%22#{permalink}%22"
data = RestClient.get url
results = JSON.parse(data)['rows']
results.first && results.first['value']
end
def recipe_updated_by(permalink)
update = couch_recipe_updated_by(permalink)
if update
date_str = Date.parse(update).strftime("%B %e, %Y")
link = %Q|<a href="/recipes/#{update}">#{date_str}</a>|
%Q|<span class="update">| +
%Q|This recipe has been updated: | +
link +
%Q|</span>|
end
end
def couch_alternatives(permalink)
url = "#{_db}/_design/recipes/_view/alternatives?key=%22#{permalink}%22"
data = RestClient.get url
results = JSON.parse(data)['rows']
results.first && results.first['value']
end
def alternate_preparations(permalink)
ids = couch_alternatives(permalink)
if ids && ids.size > 0
%Q|<span class="label">Alternate Preparations:</span> | +
couch_recipe_titles(ids).
map{ |recipe| %Q|<a href="/recipes/#{recipe[:id]}">#{recipe[:title]}</a>|}.
join(", ")
end
end
def couch_recipe_titles(ids)
data = RestClient.post "#{_db}/_design/recipes/_view/titles",
%Q|{"keys":[#{ids.map{|id| "\"#{id}\""}.join(',')}]}|
JSON.parse(data)['rows'].map do |recipe|
{ :id => recipe["id"], :title => recipe["value"] }
end
end
def rss_for_date_view(feed)
url = "#{_db}/_design/#{feed}/_view/by_date?limit=10&descending=true"
data = RestClient.get url
view = JSON.parse(data)['rows']
rss = RSS::Maker.make("2.0") do |maker|
maker.channel.title = "EEE Cooks: #{feed.capitalize}"
maker.channel.link = ROOT_URL
maker.channel.description = "#{feed.capitalize} from a Family Cookbook"
view.each do |couch_rec|
data = RestClient.get "#{_db}/#{couch_rec['id']}"
record = JSON.parse(data)
maker.items.new_item do |item|
item.title = record['title']
item.pubDate = Time.parse(record['date'])
item.description = record['summary']
yield item, record
end
end
end
rss.to_s
end
# Swiped from the Sinatra Book
# Usage: partial :foo
def partial(page, options={})
haml page, options.merge!(:layout => false)
end
end
end