/
helpers.rb
141 lines (113 loc) · 3.86 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
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 recipe_category_link(recipe, category)
if recipe['tag_names'] && recipe['tag_names'].include?(category.downcase)
%Q|<a class="active">#{category}</a>|
else
%Q|<a>#{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) }
RedCloth.new(text).to_html
end
def kid_nicknames
@@kid_kicknames ||= JSON.parse(RestClient.get("#{_db}/kids"))
end
def _db
@@db
end
def recipe_link(permalink, title=nil)
recipe = JSON.parse(RestClient.get("#{_db}/#{permalink}"))
%Q|<a href="/recipes/#{recipe['_id']}">#{title || recipe['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 = "/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
links = []
links <<
if current_page == 1
%Q|<span class="inactive">« Previous</span>|
else
%Q|<a href="#{link}&page=#{current_page - 1}">« Previous</a>|
end
links << (1..last_page).map do |page|
if page == current_page
%Q|<span class="current">#{page}</span>|
else
%Q|<a href="#{link}&page=#{page}">#{page}</a>|
end
end
links <<
if current_page == last_page
%Q|<span class="inactive">Next »</span>|
else
%Q|<a href="#{link}&page=#{current_page + 1}">Next »</a>|
end
%Q|<div class="pagination">#{links.join}</div>|
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
def link_to_adjacent_view_date(current, couch_view, options={})
compare = options[:previous] ?
Proc.new { |date_fragment, current| date_fragment < current} :
Proc.new { |date_fragment, current| date_fragment > current}
next_result = couch_view.
send(options[:previous] ? :reverse : :map).
detect{|result| compare[result['key'], current.to_s]}
if next_result
next_uri = next_result['key'].gsub(/-/, '/')
%Q|<a href="/meals/#{next_uri}">#{next_result['key']}</a>|
else
""
end
end
end
end