summaryrefslogtreecommitdiffstats
path: root/app/controllers/search_controller.rb
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2007-09-27 17:28:22 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2007-09-27 17:28:22 +0000
commita96421019f3a813b93798e4ec8099edd09f8a661 (patch)
tree102827aea563063b29ae21800bfff88fb16550f9 /app/controllers/search_controller.rb
parentd723bea502faedb218f929b40849935d61c577e2 (diff)
downloadredmine-a96421019f3a813b93798e4ec8099edd09f8a661.tar.gz
redmine-a96421019f3a813b93798e4ec8099edd09f8a661.zip
Search engines now supports pagination.
Results are sorted in reverse chronological order. git-svn-id: http://redmine.rubyforge.org/svn/trunk@766 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/controllers/search_controller.rb')
-rw-r--r--app/controllers/search_controller.rb37
1 files changed, 22 insertions, 15 deletions
diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb
index a5ecc129c..292472fba 100644
--- a/app/controllers/search_controller.rb
+++ b/app/controllers/search_controller.rb
@@ -26,6 +26,9 @@ class SearchController < ApplicationController
@question.strip!
@all_words = params[:all_words] || (params[:submit] ? false : true)
+ offset = nil
+ begin; offset = params[:offset].to_time if params[:offset]; rescue; end
+
# quick jump to an issue
if @question.match(/^#?(\d+)$/) && Issue.find_by_id($1, :include => :project, :conditions => Project.visible_by(logged_in_user))
redirect_to :controller => "issues", :action => "show", :id => $1
@@ -38,14 +41,11 @@ class SearchController < ApplicationController
end
if @project
- @object_types = %w(projects issues changesets news documents wiki_pages messages)
- @object_types.delete('wiki_pages') unless @project.wiki
- @object_types.delete('changesets') unless @project.repository
# 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]}
- # default objects to search if none is specified in parameters
@scope = @object_types if @scope.empty?
else
@object_types = @scope = %w(projects)
@@ -60,20 +60,26 @@ class SearchController < ApplicationController
# strings used in sql like statement
like_tokens = @tokens.collect {|w| "%#{w.downcase}%"}
operator = @all_words ? " AND " : " OR "
- limit = 10
@results = []
+ limit = 10
if @project
- @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'
- Journal.with_scope :find => {:conditions => ["#{Issue.table_name}.project_id = ?", @project.id]} do
- @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'
+ @scope.each do |s|
+ @results += s.singularize.camelcase.constantize.search(like_tokens, @all_words, @project,
+ :limit => (limit+1), :offset => offset, :before => params[:previous].nil?)
end
- @results.uniq!
- @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'
- @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'
- @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')
- @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')
- Message.with_scope :find => {:conditions => ["#{Board.table_name}.project_id = ?", @project.id]} do
- @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'
+ @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
end
else
Project.with_scope(:find => {:conditions => Project.visible_by(logged_in_user)}) do
@@ -86,6 +92,7 @@ class SearchController < ApplicationController
else
@question = ""
end
+ render :layout => false if request.xhr?
end
private