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 diff --git a/app/helpers/search_helper.rb b/app/helpers/search_helper.rb index 676a7e8e3..75412c70a 100644 --- a/app/helpers/search_helper.rb +++ b/app/helpers/search_helper.rb @@ -17,7 +17,7 @@ module SearchHelper def highlight_tokens(text, tokens) - return text unless tokens && !tokens.empty? + return text unless text && tokens && !tokens.empty? regexp = Regexp.new "(#{tokens.join('|')})", Regexp::IGNORECASE result = '' text.split(regexp).each_with_index do |words, i| diff --git a/app/models/changeset.rb b/app/models/changeset.rb index 9400df869..330338ab1 100644 --- a/app/models/changeset.rb +++ b/app/models/changeset.rb @@ -25,6 +25,11 @@ class Changeset < ActiveRecord::Base :datetime => :committed_on, :author => :committer, :url => Proc.new {|o| {:controller => 'repositories', :action => 'revision', :id => o.repository.project_id, :rev => o.revision}} + + acts_as_searchable :columns => 'comments', + :include => :repository, + :project_key => "#{Repository.table_name}.project_id", + :date_column => 'committed_on' validates_presence_of :repository_id, :revision, :committed_on, :commit_date validates_numericality_of :revision, :only_integer => true diff --git a/app/models/document.rb b/app/models/document.rb index 6989191ce..d95427ee6 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -20,7 +20,9 @@ class Document < ActiveRecord::Base belongs_to :category, :class_name => "Enumeration", :foreign_key => "category_id" has_many :attachments, :as => :container, :dependent => :destroy - acts_as_event :url => Proc.new {|o| {:controller => 'documents', :action => 'show', :id => o.id}} + acts_as_searchable :columns => ['title', 'description'] + acts_as_event :title => Proc.new {|o| "#{l(:label_document)}: #{o.title}"}, + :url => Proc.new {|o| {:controller => 'documents', :action => 'show', :id => o.id}} validates_presence_of :project, :title, :category validates_length_of :title, :maximum => 60 diff --git a/app/models/issue.rb b/app/models/issue.rb index 23cc71f7c..7d214de8a 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -36,8 +36,9 @@ 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_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id}: #{o.subject}"}, - :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}} + :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}} validates_presence_of :subject, :description, :priority, :tracker, :author, :status validates_length_of :subject, :maximum => 255 diff --git a/app/models/journal.rb b/app/models/journal.rb index f70a69863..6e7632e73 100644 --- a/app/models/journal.rb +++ b/app/models/journal.rb @@ -23,4 +23,9 @@ class Journal < ActiveRecord::Base belongs_to :user has_many :details, :class_name => "JournalDetail", :dependent => :delete_all + + acts_as_searchable :columns => 'notes', + :include => :issue, + :project_key => "#{Issue.table_name}.project_id", + :date_column => "#{Issue.table_name}.created_on" end diff --git a/app/models/message.rb b/app/models/message.rb index 935141b7f..b48fb2557 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -22,6 +22,11 @@ class Message < ActiveRecord::Base has_many :attachments, :as => :container, :dependent => :destroy belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id' + acts_as_searchable :columns => ['subject', 'content'], :include => :board, :project_key => "project_id" + acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"}, + :description => :content, + :url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id, :id => o.id}} + validates_presence_of :subject, :content validates_length_of :subject, :maximum => 255 diff --git a/app/models/news.rb b/app/models/news.rb index 4352363d9..3d8c4d661 100644 --- a/app/models/news.rb +++ b/app/models/news.rb @@ -24,6 +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_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 702a896f0..b17f7ba74 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -38,7 +38,11 @@ class Project < ActiveRecord::Base has_one :wiki, :dependent => :destroy has_and_belongs_to_many :custom_fields, :class_name => 'IssueCustomField', :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}", :association_foreign_key => 'custom_field_id' acts_as_tree :order => "name", :counter_cache => true - + + acts_as_searchable :columns => ['name', 'description'], :project_key => 'id' + acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"}, + :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}} + attr_protected :status, :enabled_module_names validates_presence_of :name, :description, :identifier diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index 1ef8b7db4..cbca4fd68 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -21,7 +21,16 @@ class WikiPage < ActiveRecord::Base belongs_to :wiki has_one :content, :class_name => 'WikiContent', :foreign_key => 'page_id', :dependent => :destroy has_many :attachments, :as => :container, :dependent => :destroy - + + acts_as_event :title => Proc.new {|o| "#{l(:label_wiki)}: #{o.title}"}, + :description => :text, + :datetime => :created_on, + :url => Proc.new {|o| {:controller => 'wiki', :id => o.wiki.project_id, :page => o.title}} + + acts_as_searchable :columns => ['title', 'text'], + :include => [:wiki, :content], + :project_key => "#{Wiki.table_name}.project_id" + attr_accessor :redirect_existing_links validates_presence_of :title @@ -85,6 +94,10 @@ class WikiPage < ActiveRecord::Base def project wiki.project end + + def text + content.text if content + end end class WikiDiff diff --git a/app/views/search/index.rhtml b/app/views/search/index.rhtml index 05b96cfc7..4dc26affd 100644 --- a/app/views/search/index.rhtml +++ b/app/views/search/index.rhtml @@ -15,39 +15,27 @@ <% if @results %> -

<%= lwr(:label_result, @results.length) %>

+

<%= l(:label_result_plural) %>

<% end %> + +

+<% if @pagination_previous_date %> +<%= link_to_remote ('« ' + l(:label_previous)), + {:update => :content, + :url => params.merge(:previous => 1, :offset => @pagination_previous_date.strftime("%Y%m%d%H%M%S")) + }, :href => url_for(params.merge(:previous => 1, :offset => @pagination_previous_date.strftime("%Y%m%d%H%M%S"))) %>  +<% end %> +<% if @pagination_next_date %> +<%= link_to_remote (l(:label_next) + ' »'), + {:update => :content, + :url => params.merge(:previous => nil, :offset => @pagination_next_date.strftime("%Y%m%d%H%M%S")) + }, :href => url_for(params.merge(:previous => nil, :offset => @pagination_next_date.strftime("%Y%m%d%H%M%S"))) %> +<% end %> +

diff --git a/config/environment.rb b/config/environment.rb index a6c5149ed..cbb1896a2 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -15,6 +15,8 @@ Rails::Initializer.run do |config| # Add additional load paths for sweepers config.load_paths += %W( #{RAILS_ROOT}/app/sweepers ) + + config.plugin_paths = ['lib/plugins', 'vendor/plugins'] # Force all environments to use the same logger level # (by default production uses :info, the others :debug) diff --git a/lang/en.yml b/lang/en.yml index 4b3537f0a..0be3071bb 100644 --- a/lang/en.yml +++ b/lang/en.yml @@ -350,8 +350,7 @@ label_roadmap_due_in: Due in label_roadmap_overdue: %s late label_roadmap_no_issues: No issues for this version label_search: Search -label_result: %d result -label_result_plural: %d results +label_result_plural: Results label_all_words: All words label_wiki: Wiki label_wiki_edit: Wiki edit diff --git a/lang/fr.yml b/lang/fr.yml index d6b7fb4e7..06665af26 100644 --- a/lang/fr.yml +++ b/lang/fr.yml @@ -350,8 +350,7 @@ label_roadmap_due_in: Echéance dans label_roadmap_overdue: En retard de %s label_roadmap_no_issues: Aucune demande pour cette version label_search: Recherche -label_result: %d résultat -label_result_plural: %d résultats +label_result_plural: Résultats label_all_words: Tous les mots label_wiki: Wiki label_wiki_edit: Révision wiki diff --git a/lib/redmine/acts_as_event/init.rb b/lib/plugins/acts_as_event/init.rb similarity index 100% rename from lib/redmine/acts_as_event/init.rb rename to lib/plugins/acts_as_event/init.rb diff --git a/lib/redmine/acts_as_event/lib/acts_as_event.rb b/lib/plugins/acts_as_event/lib/acts_as_event.rb similarity index 100% rename from lib/redmine/acts_as_event/lib/acts_as_event.rb rename to lib/plugins/acts_as_event/lib/acts_as_event.rb diff --git a/lib/plugins/acts_as_searchable/init.rb b/lib/plugins/acts_as_searchable/init.rb new file mode 100644 index 000000000..063721756 --- /dev/null +++ b/lib/plugins/acts_as_searchable/init.rb @@ -0,0 +1,2 @@ +require File.dirname(__FILE__) + '/lib/acts_as_searchable' +ActiveRecord::Base.send(:include, Redmine::Acts::Searchable) diff --git a/lib/plugins/acts_as_searchable/lib/acts_as_searchable.rb b/lib/plugins/acts_as_searchable/lib/acts_as_searchable.rb new file mode 100644 index 000000000..ee4d5d6f8 --- /dev/null +++ b/lib/plugins/acts_as_searchable/lib/acts_as_searchable.rb @@ -0,0 +1,89 @@ +# redMine - project management software +# Copyright (C) 2006-2007 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +module Redmine + module Acts + module Searchable + def self.included(base) + base.extend ClassMethods + end + + module ClassMethods + def acts_as_searchable(options = {}) + return if self.included_modules.include?(Redmine::Acts::Searchable::InstanceMethods) + + cattr_accessor :searchable_options + self.searchable_options = options + + if searchable_options[:columns].nil? + raise 'No searchable column defined.' + elsif !searchable_options[:columns].is_a?(Array) + searchable_options[:columns] = [] << searchable_options[:columns] + end + + if searchable_options[:project_key] + elsif column_names.include?('project_id') + searchable_options[:project_key] = "#{table_name}.project_id" + else + raise 'No project key defined.' + end + + if searchable_options[:date_column] + elsif column_names.include?('created_on') + searchable_options[:date_column] = "#{table_name}.created_on" + else + raise 'No date column defined defined.' + end + + send :include, Redmine::Acts::Searchable::InstanceMethods + end + end + + module InstanceMethods + def self.included(base) + base.extend ClassMethods + end + + module ClassMethods + def search(tokens, all_tokens, project, options={}) + tokens = [] << tokens unless tokens.is_a?(Array) + find_options = {:include => searchable_options[:include]} + find_options[:limit] = options[:limit] if options[:limit] + find_options[:order] = "#{searchable_options[:date_column]} " + (options[:before] ? 'DESC' : 'ASC') + + sql = ([ '(' + searchable_options[:columns].collect {|column| "(LOWER(#{column}) LIKE ?)"}.join(' OR ') + ')' ] * tokens.size).join(all_tokens ? ' AND ' : ' OR ') + if options[:offset] + sql = "(#{sql}) AND (#{searchable_options[:date_column]} " + (options[:before] ? '<' : '>') + "'#{connection.quoted_date(options[:offset])}')" + end + find_options[:conditions] = [sql, * (tokens * searchable_options[:columns].size).sort] + + results = with_scope(:find => {:conditions => ["#{searchable_options[:project_key]} = ?", project.id]}) do + find(:all, find_options) + end + if searchable_options[:with] + searchable_options[:with].each do |model, assoc| + results += model.to_s.camelcase.constantize.search(tokens, all_tokens, project, options).collect {|r| r.send assoc} + end + results.uniq! + end + results + end + end + end + end + end +end diff --git a/lib/redmine/acts_as_watchable/init.rb b/lib/plugins/acts_as_watchable/init.rb similarity index 100% rename from lib/redmine/acts_as_watchable/init.rb rename to lib/plugins/acts_as_watchable/init.rb diff --git a/lib/redmine/acts_as_watchable/lib/acts_as_watchable.rb b/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb similarity index 100% rename from lib/redmine/acts_as_watchable/lib/acts_as_watchable.rb rename to lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb diff --git a/lib/redmine.rb b/lib/redmine.rb index d14f886c2..8dd191c33 100644 --- a/lib/redmine.rb +++ b/lib/redmine.rb @@ -1,8 +1,6 @@ require 'redmine/access_control' require 'redmine/menu_manager' require 'redmine/mime_type' -require 'redmine/acts_as_watchable/init' -require 'redmine/acts_as_event/init' require 'redmine/plugin' begin diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index 69e78ac62..4ed7931f6 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -31,7 +31,7 @@ class SearchControllerTest < Test::Unit::TestCase assert_template 'index' assert_not_nil assigns(:project) - get :index, :id => 1, :q => "can", :scope => ["issues", "news", "documents"] + get :index, :id => 1, :q => "can" assert_response :success assert_template 'index' end