forked from patcito/shapado
/
application_helper.rb
455 lines (383 loc) · 13.2 KB
/
application_helper.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
def multiauth_dropdown(title)
render 'shared/login_menu', :title => title
end
def with_facebook?
return true if current_group.share.fb_active
if request.host =~ Regexp.new("#{AppConfig.domain}$", Regexp::IGNORECASE)
AppConfig.facebook["activate"]
else
false
end
end
def context_panel_ads(group)
if AppConfig.enable_adbard && request.domain == AppConfig.domain &&
!Adbard.where(:group_id => current_group.id).first
adbard = "<!--Ad Bard advertisement snippet, begin -->
<script type='text/javascript'>
var ab_h = '#{AppConfig.adbard_host_id}';
var ab_s = '#{AppConfig.adbard_site_key}';
</script>
<script type='text/javascript' src='http://cdn1.adbard.net/js/ab1.js'></script>
<!--Ad Bard, end -->"
else
adbard = ""
end
if group.has_custom_ads == true
ads = []
Ad.where(:group_id => group.id,:position =>'context_panel').each do |ad|
ads << ad.code
end
ads << adbard
return ads.join.html_safe unless ads.empty?
end
end
def header_ads(group)
if group.has_custom_ads
ads = []
Ad.where(:group_id => group.id,:position => 'header').each do |ad|
ads << ad.code
end
return ads.join.html_safe unless ads.empty?
end
end
def content_ads(group)
if group.has_custom_ads
ads = []
Ad.where(:group_id => group.id,:position => 'content').each do |ad|
ads << ad.code
end
return ads.join.html_safe unless ads.empty?
end
end
def footer_ads(group)
if group.has_custom_ads
ads = []
Ad.where(:group_id => group.id,:position => 'footer').each do |ad|
ads << ad.code
end
return ads.join.html_safe unless ads.empty?
end
end
def language_desc(langs)
(langs.kind_of?(Array) ? langs : [langs]).map do |lang|
I18n.t("languages.#{lang}", :default => lang).capitalize
end.join(', ')
end
def language_select(f, question, opts = {})
selected = if question.new?
logged_in? ? current_user.main_language : question.language
else
question.language
end
languages = logged_in? ? current_user.preferred_languages : current_group.languages
f.select :language, languages_options(languages), {:selected => selected}, {:class => "select"}.merge(opts)
end
def language_select_tag(name = "language", value = nil, opts = {})
languages = logged_in? ? current_user.preferred_languages : current_group.languages
select_tag name, options_for_select(languages_options(languages)), {:value => value, :class => "select"}.merge(opts)
end
def languages_options(languages=nil, current_languages = [])
languages = AVAILABLE_LANGUAGES-current_languages if languages.blank?
locales_options(languages)
end
def locales_options(languages=nil)
languages = AVAILABLE_LOCALES if languages.blank?
languages.collect do |lang|
[language_desc(lang), lang]
end
end
def tag_cloud(tags = [], options = {}, limit = nil)
if tags.empty?
tags = Question.tag_cloud({:group_id => current_group.id, :banned => false}.
merge(language_conditions.merge(language_conditions)), limit)
end
return '' if tags.size <= 2
# Sizes: xxs xs s l xl xxl
css = {1 => "xxs", 2 => "xs", 3 => "s", 4 => "l", 5 => "xl" }
max_size = 5
min_size = 1
tag_class = options.delete(:tag_class) || "tag"
lowest_value = tags.min { |a, b| a["count"].to_i <=> b["count"].to_i }
highest_value = tags.max { |a, b| a["count"].to_i <=> b["count"].to_i }
spread = (highest_value["count"] - lowest_value["count"])
spread = 1 if spread == 0
ratio = (max_size - min_size) / spread
render 'shared/tag_cloud', :tags => tags, :css => css,
:lowest_value => lowest_value, :ratio => ratio,
:min_size => min_size, :tag_class => tag_class
end
def country_flag(code, name)
if code
image_tag("flags/flag_#{code.downcase}.gif", :title => name, :alt => "")
end
end
def markdown(txt, options = {})
raw = options.delete(:raw)
body = render_page_links(txt.to_s, options)
txt = if raw
(defined?(RDiscount) ? RDiscount.new(body) : Maruku.new(body)).to_html
else
(defined?(RDiscount) ? RDiscount.new(body, :smart, :strict) : Maruku.new(sanitize(body))).to_html
end
if options[:sanitize] != false
txt = defined?(Sanitize) ? Sanitize.clean(txt, SANITIZE_CONFIG) : sanitize(txt)
end
txt.html_safe
end
def render_page_links(text, options = {})
group = options[:group]
group = current_group if group.nil?
in_controller = respond_to?(:logged_in?)
text.gsub!(/\[\[([^\,\[\'\"]+)\]\]/) do |m|
link = $1.split("|", 2)
# FIXME mongoid .only(:title, :slug).where()
page = Page.by_title(link.first, :group_id => group.id)
if page.present?
%@<a href="/pages/#{page.slug}" class="page_link">#{link[1] || page.title}</a>@
else
%@<a href="/pages/#{link.first.parameterize.to_s}?create=true&title=#{link.first}" class="missing_page">#{link.last}</a>@
end
end
return text if !in_controller
text.gsub(/%(\S+)%/) do |m|
case $1
when 'site'
group.domain
when 'site_name'
group.name
when 'current_user'
if logged_in?
link_to(current_user.login, user_path(current_user))
else
"anonymous"
end
when 'hottest_today'
question = Question.where(:activity_at.gt => Time.zone.now.yesterday, :order => "hotness desc, views_count asc", :group_id => group.id, :select => [:slug, :title]).first
if question.present?
link_to(question.title, question_path(question))
end
else
m
end
end
end
def format_number(number)
return if number.nil?
if number < 1000
number.to_s
elsif number >= 1000 && number < 1000000
"%.01fK" % (number/1000.0)
elsif number >= 1000000
"%.01fM" % (number/1000000.0)
end
end
def class_for_number(number)
return if number.nil?
if number >= 1000 && number < 10000
"medium_number"
elsif number >= 10000
"big_number"
elsif number < 0
"negative_number"
end
end
def shapado_auto_link(text, options = {})
text = auto_link(text, :all, { "rel" => 'nofollow', :class => 'auto-link' })
if options[:link_users]
text = TwitterRenderer.auto_link_usernames_or_lists(text, :username_url_base => "#{users_path}/", :suppress_lists => true)
end
text
end
def require_js(*files)
content_for(:js) { javascript_include_tag(*files) }
end
def require_css(*files)
content_for(:css) { stylesheet_link_tag(*files) }
end
def render_tag(tag)
%@<span class="tag"><a href="#{questions_path(:tags => tag)}">#{@badge.token}</a></span>@
end
def class_for_question(question)
klass = ""
if question.accepted
klass << "accepted"
elsif !question.answered
klass << "unanswered"
end
if logged_in?
if current_user.is_preferred_tag?(current_group, *question.tags)
klass << " highlight"
end
if current_user == question.user
klass << " own_question"
end
end
klass
end
def googlean_script(analytics_id, domain)
"<script type=\"text/javascript\">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '#{analytics_id}']);
_gaq.push(['_trackPageview'],['_setDomainName', '#{domain}']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
})();
</script>".html_safe
end
def logged_out_language_filter
custom_lang = session["user.language_filter"]
case custom_lang
when "any"
languages = "any"
else
languages = session["user.language_filter"] || I18n.locale.to_s.split('-').first
end
languages
end
def clean_seo_keywords(tags, text = "")
if tags.size < 5
text.scan(/(\S+)/) do |s|
word = s.to_s.downcase
if word.length > 3 && !tags.include?(word)
tags << word
end
break if tags.size >= 5
end
end
tags.join(', ')
end
def current_announcements(hide_time = nil)
conditions = {:starts_at.lte => Time.zone.now.to_i,
:ends_at.gte => Time.zone.now.to_i,
:group_id.in => [current_group.id, nil]}
if hide_time
conditions[:updated_at] = {:$gt => hide_time}
end
if logged_in?
conditions[:only_anonymous] = false
end
Announcement.order_by(:starts_at.desc).where(conditions)
end
def top_bar_links
top_bar = raw(current_group.custom_html.top_bar)
return [] if top_bar.blank?
top_bar.split("\n").map do |line|
render_page_links(line.strip)
end
end
def gravatar(*args)
super(*args).html_safe
end
def include_latex
if current_group.enable_latex
require_css 'http://fonts.googleapis.com/css?family=UnifrakturMaguntia'
require_css domain_url(:custom => current_group.domain)+'/javascripts/mathscribe/jqmath-0.1.1.css'
require_js domain_url(:custom => current_group.domain)+'/javascripts/mathscribe/jqmath-etc-0.1.1.min.js'
end
end
def find_answer(question)
if question.accepted
question.answer
else
question.answers.order_by(:votes_average.asc).first
end
end
def widget_css(widget)
"<style type='text/css'>#{widget.settings["custom_external_css"]}</style>"
end
def widget_code(widget)
path = embedded_widget_path(:id => widget.id)
url = domain_url(:custom => current_group.domain) + path
%@<iframe src="#{url}" height="200px"></iframe>@
end
def facebook_avatar(user)
image_tag("http://graph.facebook.com/#{user.facebook_id}/picture")
end
def twitter_avatar(user)
if user.user_info["twitter"]["image"]
image_tag(user.user_info["twitter"]["image"])
else
gravatar(user.email.to_s, :size => 32)
end
end
def identica_avatar(user)
image_tag(user.user_info["identica"]["image"])
end
#TODO css for image tag size
def linked_in_avatar(user)
image_tag(user.user_info["linked_in"]["image"])
end
def suggestion_avatar(suggestion)
if suggestion.class == User
avatar_tag = if suggestion.twitter_login?
twitter_avatar(suggestion)
elsif suggestion.identica_login?
identica_avatar(suggestion)
elsif suggestion.linked_in_login?
linked_in_avatar(suggestion)
else
gravatar(suggestion.email.to_s, :size => 32)
end
else
tag = Tag.where(:name => suggestion[0], :group_id => current_group.id).first
avatar_tag = tag_icon_image_link(tag)
end
avatar_tag
end
def tag_icon_image_link(tag)
image_tag(tag_icon_path(current_group, tag)) if tag.has_icon?
end
def common_follower(user, suggestion)
if suggestion.class == User
suggested_friend = suggestion
friend = user.common_follower(suggested_friend)
elsif (suggestion[1] && suggestion[1]["followed_by"])
friend = suggestion[1]["followed_by"].sample
end
if friend
raw(t('widgets.suggestions.followed_by', :user => "#{link_to friend.login, user_path(friend)}"))
end
end
def suggestion_link(suggestion)
if suggestion.class == User
link_to(suggestion.login, user_path(suggestion))
else
tag_link(suggestion[0])
end
end
def follow_suggestion_link(suggestion)
if suggestion.class == User
link_to "+ #{t("users.show.follow")}", follow_user_path(suggestion), :class => "follow_link", 'data-class' => "unfollow_link", 'data-title' => t("users.show.unfollow"), 'data-undo' => unfollow_user_path(suggestion)
else
follow_tag_link(Tag.where(:name => suggestion[0], :group_id => current_group.id).first)
end
end
def follow_tag_link(tag)
if logged_in?
if current_user.preferred_tags_on(current_group).include?(tag.name)
follow_class = 'unfollow-tag'
follow_data = 'follow-tag'
data_title = t("global.follow")
title = t("global.unfollow")
path = unfollow_tags_user_path(current_user)
data_undo = follow_tags_user_path(current_user)
else
follow_data = 'unfollow-tag'
follow_class = 'follow-tag'
data_title = t("global.unfollow")
title = t("global.follow")
opt = 'add'
path = follow_tags_user_path(current_user)
data_undo = unfollow_tags_user_path(current_user)
end
link_to title, path, :class => follow_class, 'data-tag' => tag.name, 'data-class' => follow_data, 'data-title' => data_title, 'data-undo' => data_undo
end
end
def tag_link(tag)
link_to h(tag), tag_path(:id => tag), :rel => "tag", :title => t("questions.tags.tooltip", :tag => tag)
end
end