forked from publiclab/plots2
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnode_shared.rb
127 lines (119 loc) · 4.98 KB
/
node_shared.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
module NodeShared
extend ActiveSupport::Concern
def likes
cached_likes
end
def liked_by(uid)
likers.collect(&:uid).include?(uid)
end
# rubular regex: http://rubular.com/r/hBEThNL4qd
def self.notes_grid(body, _page = 1)
body.gsub(/[^\>`](\<p\>)?\[notes\:(\S+)\]/) do |_tagname|
tagname = Regexp.last_match(2).parameterize
nodes = Node.where(status: 1, type: 'note')
.includes(:drupal_node_revision, :tag)
.where('term_data.name = ?', tagname)
.order('node_revisions.timestamp DESC')
output = ''
output += '<p>' if Regexp.last_match(1) == '<p>'
a = ActionController::Base.new()
output += a.render_to_string(template: "grids/_notes",
layout: false,
locals: {
tagname: tagname,
randomSeed: rand(1000).to_s,
className: 'notes-grid-' + tagname,
nodes: nodes,
type: "notes"
})
output
end
end
# rubular regex: http://rubular.com/r/hBEThNL4qd
def self.questions_grid(body, _page = 1)
body.gsub(/[^\>`](\<p\>)?\[questions\:(\S+)\]/) do |_tagname|
tagname = Regexp.last_match(2).parameterize
nodes = Node.where(status: 1, type: 'note')
.includes(:drupal_node_revision, :tag)
.where('term_data.name = ?', "question:#{tagname}")
.order('node_revisions.timestamp DESC')
output = ''
output += '<p>' if Regexp.last_match(1) == '<p>'
a = ActionController::Base.new()
output += a.render_to_string(template: "grids/_notes",
layout: false,
locals: {
tagname: tagname,
randomSeed: rand(1000).to_s,
className: 'questions-grid-' + tagname,
nodes: nodes,
type: "questions"
})
output
end
end
def self.activities_grid(body)
body.gsub(/[^\>`](\<p\>)?\[activities\:(\S+)\]/) do |_tagname|
tagname = Regexp.last_match(2).parameterize
nodes = Node.activities(tagname)
.order('node.cached_likes DESC')
output = ''
output += '<p>' if Regexp.last_match(1) == '<p>'
a = ActionController::Base.new()
output += a.render_to_string(template: "grids/_notes",
layout: false,
locals: {
tagname: tagname,
randomSeed: rand(1000).to_s,
className: 'activity-grid-' + tagname,
nodes: nodes,
type: "activity"
})
output
end
end
def self.upgrades_grid(body)
body.gsub(/[^\>`](\<p\>)?\[upgrades\:(\S+)\]/) do |_tagname|
tagname = Regexp.last_match(2).parameterize
nodes = Node.upgrades(tagname)
.order('node.cached_likes DESC')
output = ''
output += '<p>' if Regexp.last_match(1) == '<p>'
a = ActionController::Base.new()
output += a.render_to_string(template: "grids/_notes",
layout: false,
locals: {
tagname: tagname,
randomSeed: rand(1000).to_s,
className: 'upgrades-grid-' + tagname,
nodes: nodes,
type: "upgrades"
})
output
end
end
def self.notes_map(body)
body.gsub(/[^\>`](\<p\>)?\[map\:content\:(\S+)\:(\S+)\]/) do |_tagname|
lat = Regexp.last_match(2)
lon = Regexp.last_match(3)
nids = DrupalNodeCommunityTag.joins(:tag)
.where('name LIKE ?', 'lat:' + lat[0..lat.length - 2] + '%')
.collect(&:nid)
nids = nids || []
items = Node.includes(:tag)
.where('node.nid IN (?) AND term_data.name LIKE ?', nids, 'lon:' + lon[0..lon.length - 2] + '%')
.limit(200)
.order('node.nid DESC')
a = ActionController::Base.new()
output = a.render_to_string(template: "map/_leaflet",
layout: false,
locals: {
lat: lat,
lon: lon,
items: items
}
)
output
end
end
end