-
Notifications
You must be signed in to change notification settings - Fork 1
/
view_helpers.rb
320 lines (266 loc) · 10.2 KB
/
view_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
gem 'activesupport', ">= 2.3.2"
require 'active_support'
module ViewHelpers
def number_to_currency(number)
number
end
def t(string)
string.titleize
end
# The NavTab Class holds the structure of a navigation tab (including
# its sub-nav items).
class NavTab < Array
attr_reader :name, :proper_name
def initialize(name, proper_name)
@name, @proper_name = name, proper_name
end
def [](id)
unless id.kind_of? Fixnum
self.find {|subnav_item| subnav_item.name.to_s == id.to_s }
else
super
end
end
end
# Simple structure for storing the properties of a tab's sub items.
class NavSubItem
attr_reader :name, :proper_name, :url
def initialize(name, proper_name, url = "#")
@name, @proper_name, @url = name, proper_name, url
end
end
def nav_tabs
content = NavTab.new(:content, "Content")
content << NavSubItem.new(:pages, "Pages", "/admin/pages/")
design = NavTab.new(:design, "Design")
design << NavSubItem.new(:layouts, "Layouts", "/admin/layouts/")
design << NavSubItem.new(:snippets, "Snippets", "/admin/snippets/")
media = NavTab.new(:assets, "Assets")
media << NavSubItem.new(:images, "Images", "/admin/images/")
media << NavSubItem.new(:stylesheets, "Stylesheets", "/admin/stylesheets/")
media << NavSubItem.new(:javascripts, "Javascripts", "/admin/javascripts/")
media << NavSubItem.new(:files, "Other Files", "/admin/files/")
settings = NavTab.new(:settings, "Settings")
settings << NavSubItem.new(:general, "General", "/admin/settings/")
settings << NavSubItem.new(:users, "Users", "/admin/users/")
settings << NavSubItem.new(:extensions, "Extensions", "/admin/extensions/")
store = NavTab.new(:store, "Store")
store << NavSubItem.new(:products, "Products", "/admin/products/")
store << NavSubItem.new(:orders, "Orders", "/admin/orders/")
store << NavSubItem.new(:reports, "Reports", "/admin/reports/")
store << NavSubItem.new(:configurations, "Configurations", "/admin/configurations/")
[content, design, media, settings, store]
end
def body_classes
@body_classes ||= []
end
def button_to(title, url, html_options={})
html_options.update(:onclick=>"window.location = '#{url}'")
content_tag :button, title, html_options
end
module GravatarHelper
# Returns a Gravatar URL associated with the email parameter.
# See: http://douglasfshearer.com/blog/gravatar-for-ruby-and-ruby-on-rails
def gravatar_url(email, options={})
# Default to highest rating. Rating can be one of G, PG, R X.
options[:rating] ||= "G"
# Default size of the image.
options[:size] ||= "32px"
# Default image url to be used when no gravatar is found
# or when an image exceeds the rating parameter.
options[:default] ||= "http://localhost:4000/images/admin/avatar_#{([options[:size].to_i] * 2).join('x')}.png"
# Build the Gravatar url.
url = 'http://www.gravatar.com/avatar.php?'
url << "gravatar_id=#{Digest::MD5.new.update(email)}"
url << "&rating=#{options[:rating]}" if options[:rating]
url << "&size=#{options[:size]}" if options[:size]
url << "&default=#{options[:default]}" if options[:default]
url
end
end
include GravatarHelper
module EscapeHelper
HTML_ESCAPE = { '&' => '&', '>' => '>', '<' => '<', '"' => '"' }
JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003E', '<' => '\u003C' }
# A utility method for escaping HTML tag characters.
# This method is also aliased as <tt>h</tt>.
#
# In your ERb templates, use this method to escape any unsafe content. For example:
# <%=h @person.name %>
#
# ==== Example:
# puts html_escape("is a > 0 & a < 10?")
# # => is a > 0 & a < 10?
def html_escape(s)
s.to_s.gsub(/[&"><]/) { |special| HTML_ESCAPE[special] }
end
alias h html_escape
# A utility method for escaping HTML entities in JSON strings.
# This method is also aliased as <tt>j</tt>.
#
# In your ERb templates, use this method to escape any HTML entities:
# <%=j @person.to_json %>
#
# ==== Example:
# puts json_escape("is a > 0 & a < 10?")
# # => is a \u003E 0 \u0026 a \u003C 10?
def json_escape(s)
s.to_s.gsub(/[&"><]/) { |special| JSON_ESCAPE[special] }
end
alias j json_escape
end
include EscapeHelper
module FixtureHelper
@@fixture_count = 0
class HashStruct
def metaclass
class << self; self; end
end
def initialize(hash={})
merge(hash, true)
end
def merge(hash, force=false)
hash.each do |(k,v)|
metaclass.send(:define_method, "#{k}") { instance_variable_get("@#{k}") } if force or !respond_to?("#{k}")
metaclass.send(:define_method, "#{k}=") { |value| instance_variable_set("@#{k}", value) } if force or !respond_to?("#{k}=")
send("#{k}=", v)
end
end
end
def fixture(hash={}, extensions=Module.new)
defaults = {:id => (@@fixture_count += 1)}
f = HashStruct.new(defaults.merge(hash))
f.extend(extensions)
f.initialize_fixture if f.respond_to? :initialize_fixture
f
end
end
include FixtureHelper
module FlashHelper
def flash
@flash ||= {}
end
end
include FlashHelper
module ParamsHelper
# Key based access to query parameters. Keys can be strings or symbols.
def params
@params ||= begin
q = request.query.dup
q.each { |(k,v)| q[k.to_s.intern] = v }
q
end
end
# Extract the value for a bool param. Handy for rendering templates in
# different states.
def boolean_param(key, default = false)
key = key.to_s.intern
value = params[key]
return default if value.nil?
case value.strip.downcase
when 'true'
true
when 'false'
false
else
raise 'Invalid value'
end
end
end
include ParamsHelper
module TagHelper
def content_tag(name, content, html_options={})
%{<#{name}#{html_attributes(html_options)}>#{content}</#{name}>}
end
def tag(name, html_options={})
%{<#{name}#{html_attributes(html_options)} />}
end
def image_tag(src, html_options = {})
tag(:img, html_options.merge({:src=>src}))
end
def image(name, options = {})
image_tag(append_image_extension("/images/admin/#{name}"), options)
end
def javascript_tag(content = nil, html_options = {})
content_tag(:script, javascript_cdata_section(content), html_options.merge(:type => "text/javascript"))
end
def link_to(name, href, html_options = {})
html_options = html_options.stringify_keys
confirm = html_options.delete("confirm")
onclick = "if (!confirm('#{html_escape(confirm)}')) return false;" if confirm
content_tag(:a, name, html_options.merge(:href => href, :onclick=>onclick))
end
def link_to_function(name, *args, &block)
html_options = {}
html_options = args.pop if args.last.is_a? Hash
function = args[0] || ''
onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function}; return false;"
href = html_options[:href] || '#'
content_tag(:a, name, html_options.merge(:href => href, :onclick => onclick))
end
def mail_to(email_address, name = nil, html_options = {})
html_options = html_options.stringify_keys
encode = html_options.delete("encode").to_s
cc, bcc, subject, body = html_options.delete("cc"), html_options.delete("bcc"), html_options.delete("subject"), html_options.delete("body")
string = ''
extras = ''
extras << "cc=#{CGI.escape(cc).gsub("+", "%20")}&" unless cc.nil?
extras << "bcc=#{CGI.escape(bcc).gsub("+", "%20")}&" unless bcc.nil?
extras << "body=#{CGI.escape(body).gsub("+", "%20")}&" unless body.nil?
extras << "subject=#{CGI.escape(subject).gsub("+", "%20")}&" unless subject.nil?
extras = "?" << extras.gsub!(/&?$/,"") unless extras.empty?
email_address = email_address.to_s
email_address_obfuscated = email_address.dup
email_address_obfuscated.gsub!(/@/, html_options.delete("replace_at")) if html_options.has_key?("replace_at")
email_address_obfuscated.gsub!(/\./, html_options.delete("replace_dot")) if html_options.has_key?("replace_dot")
if encode == "javascript"
"document.write('#{content_tag("a", name || email_address_obfuscated, html_options.merge({ "href" => "mailto:"+email_address+extras }))}');".each_byte do |c|
string << sprintf("%%%x", c)
end
"<script type=\"#{Mime::JS}\">eval(decodeURIComponent('#{string}'))</script>"
elsif encode == "hex"
email_address_encoded = ''
email_address_obfuscated.each_byte do |c|
email_address_encoded << sprintf("&#%d;", c)
end
protocol = 'mailto:'
protocol.each_byte { |c| string << sprintf("&#%d;", c) }
email_address.each_byte do |c|
char = c.chr
string << (char =~ /\w/ ? sprintf("%%%x", c) : char)
end
content_tag "a", name || email_address_encoded, html_options.merge({ "href" => "#{string}#{extras}" })
else
content_tag "a", name || email_address_obfuscated, html_options.merge({ "href" => "mailto:#{email_address}#{extras}" })
end
end
private
def cdata_section(content)
"<![CDATA[#{content}]]>"
end
def javascript_cdata_section(content) #:nodoc:
"\n//#{cdata_section("\n#{content}\n//")}\n"
end
def html_attributes(options)
unless options.blank?
attrs = []
options.each_pair do |key, value|
if value == true
attrs << %(#{key}="#{key}") if value
else
attrs << %(#{key}="#{value}") unless value.nil?
end
end
" #{attrs.sort * ' '}" unless attrs.empty?
end
end
def append_image_extension(name)
unless name =~ /\.(.*?)$/
name + '.png'
else
name
end
end
end
include TagHelper
end