From: Jean-Philippe Lang Date: Sun, 18 May 2008 16:15:22 +0000 (+0000) Subject: Ability to search all projects or the projects the user belongs to (#791). X-Git-Tag: 0.8.0-RC1~536 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=073818f8bc46580e118a7550f1faaa55b3be13d9;p=redmine.git Ability to search all projects or the projects the user belongs to (#791). git-svn-id: http://redmine.rubyforge.org/svn/trunk@1435 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index f15653b63..d93c63808 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -29,6 +29,16 @@ class SearchController < ApplicationController @all_words = params[:all_words] || (params[:submit] ? false : true) @titles_only = !params[:titles_only].nil? + projects_to_search = + case params[:projects] + when 'all' + nil + when 'my_projects' + User.current.memberships.collect(&:project) + else + @project + end + offset = nil begin; offset = params[:offset].to_time if params[:offset]; rescue; end @@ -38,16 +48,16 @@ class SearchController < ApplicationController return end - if @project + @object_types = %w(issues news documents changesets wiki_pages messages projects) + if projects_to_search.is_a? Project + # don't search projects + @object_types.delete('projects') # only show what the user is allowed to view - @object_types = %w(issues news documents changesets wiki_pages messages) - @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, @project)} - - @scope = @object_types.select {|t| params[t]} - @scope = @object_types if @scope.empty? - else - @object_types = @scope = %w(projects) + @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, projects_to_search)} end + + @scope = @object_types.select {|t| params[t]} + @scope = @object_types if @scope.empty? # extract tokens from the question # eg. hello "bye bye" => ["hello", "bye bye"] @@ -62,37 +72,27 @@ class SearchController < ApplicationController like_tokens = @tokens.collect {|w| "%#{w.downcase}%"} @results = [] limit = 10 - if @project - @scope.each do |s| - @results += s.singularize.camelcase.constantize.search(like_tokens, @project, - :all_words => @all_words, - :titles_only => @titles_only, - :limit => (limit+1), - :offset => offset, - :before => params[:previous].nil?) - end - @results = @results.sort {|a,b| b.event_datetime <=> a.event_datetime} - if params[:previous].nil? - @pagination_previous_date = @results[0].event_datetime if offset && @results[0] - if @results.size > limit - @pagination_next_date = @results[limit-1].event_datetime - @results = @results[0, limit] - end - else - @pagination_next_date = @results[-1].event_datetime if offset && @results[-1] - if @results.size > limit - @pagination_previous_date = @results[-(limit)].event_datetime - @results = @results[-(limit), limit] - end + @scope.each do |s| + @results += s.singularize.camelcase.constantize.search(like_tokens, projects_to_search, + :all_words => @all_words, + :titles_only => @titles_only, + :limit => (limit+1), + :offset => offset, + :before => params[:previous].nil?) + end + @results = @results.sort {|a,b| b.event_datetime <=> a.event_datetime} + if params[:previous].nil? + @pagination_previous_date = @results[0].event_datetime if offset && @results[0] + if @results.size > limit + @pagination_next_date = @results[limit-1].event_datetime + @results = @results[0, limit] end else - operator = @all_words ? ' AND ' : ' OR ' - @results += Project.find(:all, - :limit => limit, - :conditions => [ (["(#{Project.visible_by(User.current)}) AND (LOWER(name) like ? OR LOWER(description) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] - ) if @scope.include? 'projects' - # if only one project is found, user is redirected to its overview - redirect_to :controller => 'projects', :action => 'show', :id => @results.first and return if @results.size == 1 + @pagination_next_date = @results[-1].event_datetime if offset && @results[-1] + if @results.size > limit + @pagination_previous_date = @results[-(limit)].event_datetime + @results = @results[-(limit), limit] + end end else @question = "" diff --git a/app/helpers/search_helper.rb b/app/helpers/search_helper.rb index ed2f40b69..6b5a2cede 100644 --- a/app/helpers/search_helper.rb +++ b/app/helpers/search_helper.rb @@ -35,4 +35,11 @@ module SearchHelper end result end + + def project_select_tag + options = [[l(:label_project_all), 'all']] + options << [l(:label_my_projects), 'my_projects'] unless User.current.memberships.empty? + options << [@project.name, ''] unless @project.nil? + select_tag('projects', options_for_select(options, params[:projects].to_s)) if options.size > 1 + end end diff --git a/app/models/changeset.rb b/app/models/changeset.rb index 3e95ce111..9a05e6a68 100644 --- a/app/models/changeset.rb +++ b/app/models/changeset.rb @@ -27,7 +27,7 @@ class Changeset < ActiveRecord::Base :url => Proc.new {|o| {:controller => 'repositories', :action => 'revision', :id => o.repository.project_id, :rev => o.revision}} acts_as_searchable :columns => 'comments', - :include => :repository, + :include => {:repository => :project}, :project_key => "#{Repository.table_name}.project_id", :date_column => 'committed_on' diff --git a/app/models/document.rb b/app/models/document.rb index 7a432b46b..9e2818fc7 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -20,7 +20,7 @@ class Document < ActiveRecord::Base belongs_to :category, :class_name => "Enumeration", :foreign_key => "category_id" has_many :attachments, :as => :container, :dependent => :destroy - acts_as_searchable :columns => ['title', 'description'] + acts_as_searchable :columns => ['title', "#{table_name}.description"], :include => :project acts_as_event :title => Proc.new {|o| "#{l(:label_document)}: #{o.title}"}, :author => Proc.new {|o| (a = o.attachments.find(:first, :order => "#{Attachment.table_name}.created_on ASC")) ? a.author : nil }, :url => Proc.new {|o| {:controller => 'documents', :action => 'show', :id => o.id}} diff --git a/app/models/issue.rb b/app/models/issue.rb index 8082e43b7..0618b0f0a 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -36,7 +36,7 @@ class Issue < ActiveRecord::Base has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all acts_as_watchable - acts_as_searchable :columns => ['subject', 'description'], :with => {:journal => :issue} + acts_as_searchable :columns => ['subject', "#{table_name}.description"], :include => :project, :with => {:journal => :issue} acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id}: #{o.subject}"}, :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}} diff --git a/app/models/journal.rb b/app/models/journal.rb index 1376d349e..edf261e6d 100644 --- a/app/models/journal.rb +++ b/app/models/journal.rb @@ -26,7 +26,7 @@ class Journal < ActiveRecord::Base attr_accessor :indice acts_as_searchable :columns => 'notes', - :include => :issue, + :include => {:issue => :project}, :project_key => "#{Issue.table_name}.project_id", :date_column => "#{Issue.table_name}.created_on" diff --git a/app/models/message.rb b/app/models/message.rb index a18d126c9..f57b90985 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -23,9 +23,9 @@ class Message < ActiveRecord::Base belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id' acts_as_searchable :columns => ['subject', 'content'], - :include => :board, + :include => {:board, :project}, :project_key => 'project_id', - :date_column => 'created_on' + :date_column => "#{table_name}.created_on" acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"}, :description => :content, :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'}, diff --git a/app/models/news.rb b/app/models/news.rb index 3d8c4d661..71e2a2d5e 100644 --- a/app/models/news.rb +++ b/app/models/news.rb @@ -24,7 +24,7 @@ class News < ActiveRecord::Base validates_length_of :title, :maximum => 60 validates_length_of :summary, :maximum => 255 - acts_as_searchable :columns => ['title', 'description'] + acts_as_searchable :columns => ['title', "#{table_name}.description"], :include => :project acts_as_event :url => Proc.new {|o| {:controller => 'news', :action => 'show', :id => o.id}} # returns latest news for projects visible by user diff --git a/app/models/project.rb b/app/models/project.rb index 8c32c8562..2f2937fd9 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -46,7 +46,7 @@ class Project < ActiveRecord::Base acts_as_tree :order => "name", :counter_cache => true - acts_as_searchable :columns => ['name', 'description'], :project_key => 'id' + acts_as_searchable :columns => ['name', 'description'], :project_key => 'id', :permission => nil acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"}, :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}} @@ -202,6 +202,10 @@ class Project < ActiveRecord::Base @all_custom_fields ||= (IssueCustomField.for_all + custom_fields).uniq end + def project + self + end + def <=>(project) name.downcase <=> project.name.downcase end diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index 95750f37b..65fc1f68c 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -29,7 +29,7 @@ class WikiPage < ActiveRecord::Base :url => Proc.new {|o| {:controller => 'wiki', :id => o.wiki.project_id, :page => o.title}} acts_as_searchable :columns => ['title', 'text'], - :include => [:wiki, :content], + :include => [{:wiki => :project}, :content], :project_key => "#{Wiki.table_name}.project_id" attr_accessor :redirect_existing_links diff --git a/app/views/search/index.rhtml b/app/views/search/index.rhtml index 29c604a21..0be97a504 100644 --- a/app/views/search/index.rhtml +++ b/app/views/search/index.rhtml @@ -4,23 +4,25 @@ <% form_tag({}, :method => :get) do %>

<%= text_field_tag 'q', @question, :size => 60, :id => 'search-input' %> <%= javascript_tag "Field.focus('search-input')" %> - +<%= project_select_tag %> + + +

+

<% @object_types.each do |t| %> <% end %> -
- -

-<%= submit_tag l(:button_submit), :name => 'submit' %> + +

<%= submit_tag l(:button_submit), :name => 'submit' %>

<% end %> <% if @results %>

<%= l(:label_result_plural) %>

-