-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathsrch_scope.rb
67 lines (58 loc) · 1.9 KB
/
srch_scope.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
# This class provides common methods that Typehead and Search services use
class SrchScope
def self.find_users(input, limit)
if ActiveRecord::Base.connection.adapter_name == 'Mysql2'
User.order('id DESC')
.where(status: 1)
.where('username LIKE ?', '%' + input + '%')
.limit(limit)
else
User.order('id DESC')
.where('username LIKE ? AND status = 1', '%' + input + '%')
.limit(limit)
end
end
def self.find_tags(input, limit)
tags = Tag.includes(:node)
.references(:node)
.where('node.status = 1')
.limit(limit)
.where('name LIKE ?', '%' + input + '%')
tags.limit(limit)
end
def self.find_nodes(input, limit, order)
nodes = Node.search(query: input, order: order, limit: limit)
.group(:nid)
.where('node.status': 1)
end
def self.find_notes(input, limit)
notes = Node.order('nid DESC')
.where('type = "note" AND node.status = 1 AND title LIKE ?', '%' + input + '%')
notes.limit(limit)
end
def self.find_comments(input, limit)
if ActiveRecord::Base.connection.adapter_name == 'Mysql2'
Comment.search(input)
.order('nid DESC')
.where(status: 1)
.limit(limit)
else
Comment.order('nid DESC')
.where('status = 1 AND comment LIKE ?', '%' + input + '%')
.limit(limit)
end
end
def self.find_maps(input, limit)
maps = Node.where('type = "map" AND node.status = 1 AND title LIKE ?', '%' + input + '%')
.limit(limit)
end
def self.find_wikis(input, limit, order)
wikis = find_nodes(input, limit, order).where("node.type": "page")
end
def self.find_questions(input, limit, order)
questions = find_nodes(input, limit, order)
.where('node.type': 'note')
.joins(:tag)
.where('term_data.name LIKE ?', 'question:%')
end
end