##// END OF EJS Templates
Search engine now only searches objects the user is allowed to view....
Jean-Philippe Lang -
r747:42fe6c6e0456
parent child
Show More
@@ -25,10 +25,9 class SearchController < ApplicationController
25 @question = params[:q] || ""
25 @question = params[:q] || ""
26 @question.strip!
26 @question.strip!
27 @all_words = params[:all_words] || (params[:submit] ? false : true)
27 @all_words = params[:all_words] || (params[:submit] ? false : true)
28 @scope = params[:scope] || (params[:submit] ? [] : %w(projects issues changesets news documents wiki messages) )
29
28
30 # quick jump to an issue
29 # quick jump to an issue
31 if @scope.include?('issues') && @question.match(/^#?(\d+)$/) && Issue.find_by_id($1, :include => :project, :conditions => Project.visible_by(logged_in_user))
30 if @question.match(/^#?(\d+)$/) && Issue.find_by_id($1, :include => :project, :conditions => Project.visible_by(logged_in_user))
32 redirect_to :controller => "issues", :action => "show", :id => $1
31 redirect_to :controller => "issues", :action => "show", :id => $1
33 return
32 return
34 end
33 end
@@ -38,6 +37,20 class SearchController < ApplicationController
38 return unless check_project_privacy
37 return unless check_project_privacy
39 end
38 end
40
39
40 if @project
41 @object_types = %w(projects issues changesets news documents wiki_pages messages)
42 @object_types.delete('wiki_pages') unless @project.wiki
43 @object_types.delete('changesets') unless @project.repository
44 # only show what the user is allowed to view
45 @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, @project)}
46
47 @scope = @object_types.select {|t| params[t]}
48 # default objects to search if none is specified in parameters
49 @scope = @object_types if @scope.empty?
50 else
51 @scope = %w(projects)
52 end
53
41 # tokens must be at least 3 character long
54 # tokens must be at least 3 character long
42 @tokens = @question.split.uniq.select {|w| w.length > 2 }
55 @tokens = @question.split.uniq.select {|w| w.length > 2 }
43
56
@@ -49,7 +62,7 class SearchController < ApplicationController
49 operator = @all_words ? " AND " : " OR "
62 operator = @all_words ? " AND " : " OR "
50 limit = 10
63 limit = 10
51 @results = []
64 @results = []
52 if @project
65 if @project
53 @results += @project.issues.find(:all, :limit => limit, :include => :author, :conditions => [ (["(LOWER(subject) like ? OR LOWER(description) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @scope.include? 'issues'
66 @results += @project.issues.find(:all, :limit => limit, :include => :author, :conditions => [ (["(LOWER(subject) like ? OR LOWER(description) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @scope.include? 'issues'
54 Journal.with_scope :find => {:conditions => ["#{Issue.table_name}.project_id = ?", @project.id]} do
67 Journal.with_scope :find => {:conditions => ["#{Issue.table_name}.project_id = ?", @project.id]} do
55 @results += Journal.find(:all, :include => :issue, :limit => limit, :conditions => [ (["(LOWER(notes) like ? OR LOWER(notes) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ).collect(&:issue) if @scope.include? 'issues'
68 @results += Journal.find(:all, :include => :issue, :limit => limit, :conditions => [ (["(LOWER(notes) like ? OR LOWER(notes) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ).collect(&:issue) if @scope.include? 'issues'
@@ -57,7 +70,7 class SearchController < ApplicationController
57 @results.uniq!
70 @results.uniq!
58 @results += @project.news.find(:all, :limit => limit, :conditions => [ (["(LOWER(title) like ? OR LOWER(description) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort], :include => :author ) if @scope.include? 'news'
71 @results += @project.news.find(:all, :limit => limit, :conditions => [ (["(LOWER(title) like ? OR LOWER(description) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort], :include => :author ) if @scope.include? 'news'
59 @results += @project.documents.find(:all, :limit => limit, :conditions => [ (["(LOWER(title) like ? OR LOWER(description) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @scope.include? 'documents'
72 @results += @project.documents.find(:all, :limit => limit, :conditions => [ (["(LOWER(title) like ? OR LOWER(description) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @scope.include? 'documents'
60 @results += @project.wiki.pages.find(:all, :limit => limit, :include => :content, :conditions => [ (["(LOWER(title) like ? OR LOWER(text) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @project.wiki && @scope.include?('wiki')
73 @results += @project.wiki.pages.find(:all, :limit => limit, :include => :content, :conditions => [ (["(LOWER(title) like ? OR LOWER(text) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @project.wiki && @scope.include?('wiki_pages')
61 @results += @project.repository.changesets.find(:all, :limit => limit, :conditions => [ (["(LOWER(comments) like ?)"] * like_tokens.size).join(operator), * (like_tokens).sort] ) if @project.repository && @scope.include?('changesets')
74 @results += @project.repository.changesets.find(:all, :limit => limit, :conditions => [ (["(LOWER(comments) like ?)"] * like_tokens.size).join(operator), * (like_tokens).sort] ) if @project.repository && @scope.include?('changesets')
62 Message.with_scope :find => {:conditions => ["#{Board.table_name}.project_id = ?", @project.id]} do
75 Message.with_scope :find => {:conditions => ["#{Board.table_name}.project_id = ?", @project.id]} do
63 @results += Message.find(:all, :include => :board, :limit => limit, :conditions => [ (["(LOWER(subject) like ? OR LOWER(content) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @scope.include? 'messages'
76 @results += Message.find(:all, :include => :board, :limit => limit, :conditions => [ (["(LOWER(subject) like ? OR LOWER(content) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @scope.include? 'messages'
@@ -5,21 +5,8
5 <p><%= text_field_tag 'q', @question, :size => 30, :id => 'search-input' %>
5 <p><%= text_field_tag 'q', @question, :size => 30, :id => 'search-input' %>
6 <%= javascript_tag "Field.focus('search-input')" %>
6 <%= javascript_tag "Field.focus('search-input')" %>
7
7
8 <% if @project %>
8 <% @object_types.each do |t| %>
9 <%= check_box_tag 'scope[]', 'issues', (@scope.include? 'issues') %> <label><%= l(:label_issue_plural) %></label>
9 <label><%= check_box_tag t, 1, @scope.include?(t) %> <%= l("label_#{t.singularize}_plural")%></label>
10 <% if @project.repository %>
11 <%= check_box_tag 'scope[]', 'changesets', (@scope.include? 'changesets') %> <label><%= l(:label_revision_plural) %></label>
12 <% end %>
13 <%= check_box_tag 'scope[]', 'news', (@scope.include? 'news') %> <label><%= l(:label_news_plural) %></label>
14 <%= check_box_tag 'scope[]', 'documents', (@scope.include? 'documents') %> <label><%= l(:label_document_plural) %></label>
15 <% if @project.wiki %>
16 <%= check_box_tag 'scope[]', 'wiki', (@scope.include? 'wiki') %> <label><%= l(:label_wiki) %></label>
17 <% end %>
18 <% if @project.boards.any? %>
19 <%= check_box_tag 'scope[]', 'messages', (@scope.include? 'messages') %> <label><%= l(:label_message_plural) %></label>
20 <% end %>
21 <% else %>
22 <%= check_box_tag 'scope[]', 'projects', (@scope.include? 'projects') %> <label><%= l(:label_project_plural) %></label>
23 <% end %>
10 <% end %>
24 <br />
11 <br />
25 <%= check_box_tag 'all_words', 1, @all_words %> <%= l(:label_all_words) %></p>
12 <%= check_box_tag 'all_words', 1, @all_words %> <%= l(:label_all_words) %></p>
General Comments 0
You need to be logged in to leave comments. Login now