-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
application_helper.rb
153 lines (134 loc) · 5.04 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
module ApplicationHelper
# returns true if user is logged in and has any of the roles given, as ['admin','moderator']
def logged_in_as(roles)
return false unless current_user
has_valid_role = false
roles.each do |role|
has_valid_role = true if current_user.role == role
end
has_valid_role
end
def emojify(content)
if content.present?
content.to_str.gsub(/:([\w+-]+):(?![^\[]*\])/) do |match|
if emoji = Emoji.find_by_alias(Regexp.last_match(1))
emoji.raw || %(<img class="emoji" alt="#{Regexp.last_match(1)}" src="#{image_path("emoji/#{emoji.image_filename}")}" style="vertical-align:middle" width="20" height="20" />)
else
match
end
end
end
end
def emoji_names_list
emojis = []
image_map = {}
Emoji.all.each do |e|
next unless e.raw
val = ":#{e.name}:"
emojis << { value: val, text: e.name }
image_map[e.name] = e.raw
end
{ emojis: emojis, image_map: image_map }
end
def emoji_info
emoji_names = ["thumbs-up", "thumbs-down", "laugh", "hooray", "confused", "heart"]
emoji_image_map = {
"thumbs-up" => "https://github.githubassets.com/images/icons/emoji/unicode/1f44d.png",
"thumbs-down" => "https://github.githubassets.com/images/icons/emoji/unicode/1f44e.png",
"laugh" => "https://github.githubassets.com/images/icons/emoji/unicode/1f604.png",
"hooray" => "https://github.githubassets.com/images/icons/emoji/unicode/1f389.png",
"confused" => "https://github.githubassets.com/images/icons/emoji/unicode/1f615.png",
"heart" => "https://github.githubassets.com/images/icons/emoji/unicode/2764.png"
}
[emoji_names, emoji_image_map]
end
def feature(title)
features = Node.where(type: 'feature', title: title)
if !features.empty?
return features.last.body.to_s.html_safe
else
''
end
end
def feature_node(title)
features = Node.where(type: 'feature', title: title)
if !features.empty?
return features.last
else
''
end
end
def locale_name_pairs
I18n.available_locales.map do |locale|
[I18n.t('language', locale: locale), locale]
end
end
def insert_extras(body)
body = NodeShared.button(body)
body = NodeShared.nodes_grid(body)
body = NodeShared.notes_thumbnail_grid(body)
body = NodeShared.notes_grid(body)
body = NodeShared.questions_grid(body)
body = NodeShared.activities_grid(body)
body = NodeShared.upgrades_grid(body)
body = NodeShared.notes_map(body)
body = NodeShared.notes_map_by_tag(body)
body = NodeShared.people_map(body)
body = NodeShared.people_grid(body, @current_user || nil) # <= to allow testing of insert_extras
body = NodeShared.graph_grid(body)
body = NodeShared.wikis_grid(body)
body
end
# we should move this to the Node model:
def render_map(lat, lon)
render partial: 'map/leaflet', locals: { lat: lat, lon: lon, top_map: false }
end
def render_top_map(lat, lon)
render partial: 'map/leaflet', locals: { lat: lat, lon: lon, top_map: true }
end
# we should move this to the Comment model:
# replaces inline title suggestion(e.g: {New Title}) with the required link to change the title
def title_suggestion(comment)
comment.body.gsub(/\[propose:title\](.*?)\[\/propose\]/) do
a = ActionController::Base.new
is_creator = ((defined? current_user) && current_user == Node.find(comment.nid).author) == true
title = Regexp.last_match(1)
output = a.render_to_string(template: "notes/_title_suggestion",
layout: false,
locals: {
user: comment.user.name,
nid: comment.nid,
title: title,
is_creator: is_creator
})
output
end
end
def filtered_comment_body(comment_body)
if contain_trimmed_body?(comment_body)
return comment_body.split(Comment::COMMENT_FILTER).first
end
comment_body
end
def contain_trimmed_body?(comment_body)
comment_body.include?(Comment::COMMENT_FILTER)
end
def trimmed_body(comment_body)
comment_body.split(Comment::COMMENT_FILTER).second
end
def current_page?(page)
'is-active' if request.path == page
end
def translation(key, options = {})
translated_string = t(key, options)
options[:fallback] = false
translated_string2 = t(key, options)
if current_user&.has_tag('translation-helper') && translated_string2.include?("translation missing") && !translated_string.include?("<")
%(<span>#{translated_string} <a href="https://www.transifex.com/publiclab/publiclaborg/translate/#de/$?q=text%3A#{translated_string}">
<i data-toggle='tooltip' data-placement='top' title='Needs translation? Click to help translate this text.' style='position:relative; right:2px; color:#bbb; font-size: 15px;' class='fa fa-globe'></i></a>
</span>)
else
translated_string
end
end
end