Mercurial > hg > soundsoftware-site
comparison .svn/pristine/f7/f75cc0973adb64fe86bec853f1ce941fdd75f24d.svn-base @ 1298:4f746d8966dd redmine_2.3_integration
Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author | Chris Cannam |
---|---|
date | Fri, 14 Jun 2013 09:28:30 +0100 |
parents | 622f24f53b42 |
children |
comparison
equal
deleted
inserted
replaced
1297:0a574315af3e | 1298:4f746d8966dd |
---|---|
1 # Redmine - project management software | |
2 # Copyright (C) 2006-2013 Jean-Philippe Lang | |
3 # | |
4 # This program is free software; you can redistribute it and/or | |
5 # modify it under the terms of the GNU General Public License | |
6 # as published by the Free Software Foundation; either version 2 | |
7 # of the License, or (at your option) any later version. | |
8 # | |
9 # This program is distributed in the hope that it will be useful, | |
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 # GNU General Public License for more details. | |
13 # | |
14 # You should have received a copy of the GNU General Public License | |
15 # along with this program; if not, write to the Free Software | |
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 | |
18 class SearchController < ApplicationController | |
19 before_filter :find_optional_project | |
20 | |
21 def index | |
22 @question = params[:q] || "" | |
23 @question.strip! | |
24 @all_words = params[:all_words] ? params[:all_words].present? : true | |
25 @titles_only = params[:titles_only] ? params[:titles_only].present? : false | |
26 | |
27 projects_to_search = | |
28 case params[:scope] | |
29 when 'all' | |
30 nil | |
31 when 'my_projects' | |
32 User.current.memberships.collect(&:project) | |
33 when 'subprojects' | |
34 @project ? (@project.self_and_descendants.active.all) : nil | |
35 else | |
36 @project | |
37 end | |
38 | |
39 offset = nil | |
40 begin; offset = params[:offset].to_time if params[:offset]; rescue; end | |
41 | |
42 # quick jump to an issue | |
43 if (m = @question.match(/^#?(\d+)$/)) && (issue = Issue.visible.find_by_id(m[1].to_i)) | |
44 redirect_to issue_path(issue) | |
45 return | |
46 end | |
47 | |
48 @object_types = Redmine::Search.available_search_types.dup | |
49 if projects_to_search.is_a? Project | |
50 # don't search projects | |
51 @object_types.delete('projects') | |
52 # only show what the user is allowed to view | |
53 @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, projects_to_search)} | |
54 end | |
55 | |
56 @scope = @object_types.select {|t| params[t]} | |
57 @scope = @object_types if @scope.empty? | |
58 | |
59 # extract tokens from the question | |
60 # eg. hello "bye bye" => ["hello", "bye bye"] | |
61 @tokens = @question.scan(%r{((\s|^)"[\s\w]+"(\s|$)|\S+)}).collect {|m| m.first.gsub(%r{(^\s*"\s*|\s*"\s*$)}, '')} | |
62 # tokens must be at least 2 characters long | |
63 @tokens = @tokens.uniq.select {|w| w.length > 1 } | |
64 | |
65 if !@tokens.empty? | |
66 # no more than 5 tokens to search for | |
67 @tokens.slice! 5..-1 if @tokens.size > 5 | |
68 | |
69 @results = [] | |
70 @results_by_type = Hash.new {|h,k| h[k] = 0} | |
71 | |
72 limit = 10 | |
73 @scope.each do |s| | |
74 r, c = s.singularize.camelcase.constantize.search(@tokens, projects_to_search, | |
75 :all_words => @all_words, | |
76 :titles_only => @titles_only, | |
77 :limit => (limit+1), | |
78 :offset => offset, | |
79 :before => params[:previous].nil?) | |
80 @results += r | |
81 @results_by_type[s] += c | |
82 end | |
83 @results = @results.sort {|a,b| b.event_datetime <=> a.event_datetime} | |
84 if params[:previous].nil? | |
85 @pagination_previous_date = @results[0].event_datetime if offset && @results[0] | |
86 if @results.size > limit | |
87 @pagination_next_date = @results[limit-1].event_datetime | |
88 @results = @results[0, limit] | |
89 end | |
90 else | |
91 @pagination_next_date = @results[-1].event_datetime if offset && @results[-1] | |
92 if @results.size > limit | |
93 @pagination_previous_date = @results[-(limit)].event_datetime | |
94 @results = @results[-(limit), limit] | |
95 end | |
96 end | |
97 else | |
98 @question = "" | |
99 end | |
100 render :layout => false if request.xhr? | |
101 end | |
102 | |
103 private | |
104 def find_optional_project | |
105 return true unless params[:id] | |
106 @project = Project.find(params[:id]) | |
107 check_project_privacy | |
108 rescue ActiveRecord::RecordNotFound | |
109 render_404 | |
110 end | |
111 end |