-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathredsun_search_controller.rb
More file actions
253 lines (221 loc) · 8.39 KB
/
Copy pathredsun_search_controller.rb
File metadata and controls
253 lines (221 loc) · 8.39 KB
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
# :nodoc:
class RedsunSearchController < ApplicationController
unloadable
before_action :find_optional_project
before_action :set_search_form
def index
searchstring = @search_params[:searchstring] || ''
# Redirect to Issue if ticket ID is entered
if searchstring.match(/^\s*[#]?(\d+).*$/) && Issue.visible.find_by_id($1.to_i)
redirect_to controller: 'issues', action: 'show', id: $1.to_i
return
end
allowed_projects = []
allowed_issues = []
allowed_wikis = []
if @project && search_scope == 'project'
@search_params[:project_id] = @project.id
projects = @project.self_and_descendants.all
allowed_projects << projects.collect { |p| p.id if User.current.allowed_to?(:view_project, p) }.compact
allowed_issues << projects.collect { |project| project.id if User.current.allowed_to?(:view_issues, project) }.compact
allowed_wikis << projects.collect { |project| project.id if User.current.allowed_to?(:view_wiki_pages, @project) }.compact
elsif search_scope == 'my_projects'
projects = User.current.memberships.collect(&:project).compact.uniq
allowed_projects << projects.collect(&:id)
allowed_issues << projects.collect { |project| project.id if User.current.allowed_to?(:view_issues, project) }.compact
allowed_wikis << projects.collect { |project| project.id if User.current.allowed_to?(:view_wiki_pages, project) }.compact
# Search all projects
else
projects = Project.all
allowed_projects << projects.collect { |p| p.id if User.current.allowed_to?(:view_project, p) }.compact
allowed_issues << projects.collect { |p| p.id if User.current.allowed_to?(:view_issues, p) }.compact
allowed_wikis << projects.collect { |p| p.id if User.current.allowed_to?(:view_wiki_pages, p) }.compact
end
allowed_projects = allowed_projects.push(0)
allowed_issues = allowed_issues.push(0)
allowed_wikis = allowed_wikis.push(0)
@search = Sunspot.search([Project, Issue, WikiPage, Journal, Attachment, News]) do
fulltext searchstring do
highlight :description
highlight :summary
highlight :comments
highlight :subject
highlight :wiki_content
highlight :name
highlight :notes
highlight :id
highlight :title
highlight :filename
boost_fields(
:filename => 9,
:subject => 9,
:description => 7,
:comments => 9,
:notes => 9,
:project_name => 10,
:title => 10,
:wiki_content => 8,
)
end
any_of do
all_of do
with :class, Issue
with(:project_id).any_of allowed_issues.flatten
with(:is_private, false)
end
all_of do
with :class, WikiPage
with(:project_id).any_of allowed_wikis.flatten
end
all_of do
with :class, Project
with(:project_id).any_of allowed_projects.flatten
end
all_of do
with :class, Journal
with(:project_id).any_of allowed_issues.flatten
with(:journalized_type, 'Issue')
with(:is_private, false)
end
all_of do
with :class, Attachment
with(:project_id).any_of allowed_projects.flatten
end
all_of do
with :class, News
with(:project_id).any_of allowed_projects.flatten
end
end
%w(author_id
project_name
assigned_to_id
priority_id
tracker_id
status_id
category_id
filetype
class_name).each do |easy_facet|
facet_filter = if params[:search_form].key?(easy_facet)
with(easy_facet).any_of(params[:search_form][easy_facet])
else
all_of {} # not necessary for searching, but object is needed to set exclude option
end
facet easy_facet, minimum_count: 0, exclude: facet_filter
end
# Class
class_facet_filter = if params[:search_form].key?(:class_name)
with(:class_name).any_of(params[:search_form][:class_name])
else
all_of {} # not necessary for searching, but object is needed to set exclude option
end
facet :class_name, minimum_count: 0, exclude: class_facet_filter
%w(created_on updated_on).each do |date_facet|
if params[:search_form].present? && params[:search_form][date_facet].present?
date_range = date_conditions.collect { |c| c[:date] if (c[:name].to_s == params[:search_form][date_facet.to_sym]) }.compact.first
with(date_facet).greater_than(date_range) unless date_range.nil?
end
end
# Pagination
paginate(page: params[:page], per_page: 15)
# Created on facet
facet :created_on do
date_conditions.each do |condition|
row(condition[:name].to_s) do
with(:created_on).greater_than condition[:date]
end
end
end
# Updated on facet
facet :updated_on do
date_conditions.each do |condition|
row(condition[:name].to_s) do
with(:updated_on).greater_than condition[:date]
end
end
end
order_by(sort_field.to_sym, sort_order.downcase.to_sym)
order_by(:score, :desc)
spellcheck(count: 3)
end
@searchstring = searchstring
redirect_if_neccessary
rescue Errno::ECONNREFUSED
render 'connection_refused'
rescue RSolr::Error::Http
render 'connection_refused'
end
protected
def set_search_form
params[:search_form] = {} unless params[:search_form].present?
@search_params = params[:search_form].permit(
:created_on,
:scope,
:searchstring,
:sort_field,
:sort_order,
:updated_on,
:assigned_to_id => [],
:author_id => [],
:category_id => [],
:class_name => [],
:filetype => [],
:priority_id => [],
:project_id => [],
:project_name => [],
:status_id => [],
:tracker_id => [],
)
@scope = @search_params[:scope] || 'all_projects'
@search_params[:class_name] ||= []
@search_params[:class_name] = [] if @search_params[:class_name].blank?
@sort_field = sort_field
@sort_order = sort_order
@scope_selector = [[l(:label_my_projects), 'my_projects'], [l(:label_project_all), 'all_projects']]
@redsun_path = @project.present? ? redsun_project_search_path(@project) : redsun_search_path
@scope_selector.push([l(:label_and_its_subprojects, @project.name), 'project']) if @project
end
def date_conditions
conditions = []
conditions << { name: :last_7_days, date: (Time.now - 7.day) }
conditions << { name: :last_month, date: (Time.now - 31.day) }
conditions << { name: :last_3_months, date: (Time.now - 3.months) }
conditions << { name: :last_6_months, date: (Time.now - 6.months) }
conditions << { name: :older, date: (Time.now - 12.months) }
conditions
end
private
def redirect_if_neccessary
# No need to redirect
return true if @search.total > 0
# Class name was select but no result was found
if @search_params.key?(:class_name) && @search_params[:class_name].any? && @search.facet(:class_name).rows.map(&:count).count > 0
flash_message = I18n.translate("redsun.redirect_for_missing_results",
class_name: I18n.translate("redsun.#{@search_params[:class_name].try(:first).try(:downcase)}") )
redirect_to redsun_search_url(search_form: @search_params.except(:class_name)), notice: flash_message
else
true
end
end
def search_scope
@search_params[:scope] || 'all_projects'
end
def sort_order
return @search_params[:sort_order] if Issue::SORT_ORDER.collect(&:first).include?(@search_params[:sort_order])
'DESC'
end
def sort_field
return @search_params[:sort_field] if Issue::SORT_FIELDS.include?(@search_params[:sort_field])
'score'
end
def find_optional_project
return true unless (params[:id] || params[:project_id])
if params[:id] && controller.controller_name == "projects"
@project = Project.find(params[:id])
else
@project = Project.find(params[:project_id])
end
check_project_privacy
rescue ActiveRecord::RecordNotFound
render_404
end
end