-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathapplication_helper.rb
142 lines (129 loc) · 4.33 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
module ApplicationHelper
# returns true if user is logged in and has any of the roles given, as ['admin','moderator']
def logged_in_as(roles)
if current_user
has_valid_role = false
roles.each do |role|
has_valid_role = true if current_user.role == role
end
has_valid_role
else
false
end
end
def emojify(content)
if content.present?
content.to_str.gsub(/:([\w+-]+):/) do |match|
if emoji = Emoji.find_by_alias(Regexp.last_match(1))
if emoji.raw
emoji.raw
else
%(<img class="emoji" alt="#{Regexp.last_match(1)}" src="#{image_path("emoji/#{emoji.image_filename}")}" style="vertical-align:middle" width="20" height="20" />)
end
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://assets-cdn.github.com/images/icons/emoji/unicode/1f44d.png",
"thumbs-down" => "https://assets-cdn.github.com/images/icons/emoji/unicode/1f44e.png",
"laugh" => "https://assets-cdn.github.com/images/icons/emoji/unicode/1f604.png",
"hooray" => "https://assets-cdn.github.com/images/icons/emoji/unicode/1f389.png",
"confused" => "https://assets-cdn.github.com/images/icons/emoji/unicode/1f615.png",
"heart" => "https://assets-cdn.github.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.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 }
end
# we should move this to the Comment model:
# returns the comment body which is to be shown in the comments section
def render_comment_body(comment)
raw RDiscount.new(
title_suggestion(comment),
:autolink
).to_html
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 = current_user.drupal_user == Node.find(comment.nid).author
title = Regexp.last_match(1)
output = a.render_to_string(template: "notes/_title_suggestion",
layout: false,
locals: {
user: comment.drupal_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
end