##// END OF EJS Templates
Search engines now supports pagination....
Jean-Philippe Lang -
r755:a96421019f3a
parent child
Show More
@@ -0,0 +1,2
1 require File.dirname(__FILE__) + '/lib/acts_as_searchable'
2 ActiveRecord::Base.send(:include, Redmine::Acts::Searchable)
@@ -0,0 +1,89
1 # redMine - project management software
2 # Copyright (C) 2006-2007 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 module Redmine
19 module Acts
20 module Searchable
21 def self.included(base)
22 base.extend ClassMethods
23 end
24
25 module ClassMethods
26 def acts_as_searchable(options = {})
27 return if self.included_modules.include?(Redmine::Acts::Searchable::InstanceMethods)
28
29 cattr_accessor :searchable_options
30 self.searchable_options = options
31
32 if searchable_options[:columns].nil?
33 raise 'No searchable column defined.'
34 elsif !searchable_options[:columns].is_a?(Array)
35 searchable_options[:columns] = [] << searchable_options[:columns]
36 end
37
38 if searchable_options[:project_key]
39 elsif column_names.include?('project_id')
40 searchable_options[:project_key] = "#{table_name}.project_id"
41 else
42 raise 'No project key defined.'
43 end
44
45 if searchable_options[:date_column]
46 elsif column_names.include?('created_on')
47 searchable_options[:date_column] = "#{table_name}.created_on"
48 else
49 raise 'No date column defined defined.'
50 end
51
52 send :include, Redmine::Acts::Searchable::InstanceMethods
53 end
54 end
55
56 module InstanceMethods
57 def self.included(base)
58 base.extend ClassMethods
59 end
60
61 module ClassMethods
62 def search(tokens, all_tokens, project, options={})
63 tokens = [] << tokens unless tokens.is_a?(Array)
64 find_options = {:include => searchable_options[:include]}
65 find_options[:limit] = options[:limit] if options[:limit]
66 find_options[:order] = "#{searchable_options[:date_column]} " + (options[:before] ? 'DESC' : 'ASC')
67
68 sql = ([ '(' + searchable_options[:columns].collect {|column| "(LOWER(#{column}) LIKE ?)"}.join(' OR ') + ')' ] * tokens.size).join(all_tokens ? ' AND ' : ' OR ')
69 if options[:offset]
70 sql = "(#{sql}) AND (#{searchable_options[:date_column]} " + (options[:before] ? '<' : '>') + "'#{connection.quoted_date(options[:offset])}')"
71 end
72 find_options[:conditions] = [sql, * (tokens * searchable_options[:columns].size).sort]
73
74 results = with_scope(:find => {:conditions => ["#{searchable_options[:project_key]} = ?", project.id]}) do
75 find(:all, find_options)
76 end
77 if searchable_options[:with]
78 searchable_options[:with].each do |model, assoc|
79 results += model.to_s.camelcase.constantize.search(tokens, all_tokens, project, options).collect {|r| r.send assoc}
80 end
81 results.uniq!
82 end
83 results
84 end
85 end
86 end
87 end
88 end
89 end
@@ -26,6 +26,9 class SearchController < ApplicationController
26 26 @question.strip!
27 27 @all_words = params[:all_words] || (params[:submit] ? false : true)
28 28
29 offset = nil
30 begin; offset = params[:offset].to_time if params[:offset]; rescue; end
31
29 32 # quick jump to an issue
30 33 if @question.match(/^#?(\d+)$/) && Issue.find_by_id($1, :include => :project, :conditions => Project.visible_by(logged_in_user))
31 34 redirect_to :controller => "issues", :action => "show", :id => $1
@@ -38,14 +41,11 class SearchController < ApplicationController
38 41 end
39 42
40 43 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 44 # only show what the user is allowed to view
45 @object_types = %w(issues news documents changesets wiki_pages messages)
45 46 @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, @project)}
46 47
47 48 @scope = @object_types.select {|t| params[t]}
48 # default objects to search if none is specified in parameters
49 49 @scope = @object_types if @scope.empty?
50 50 else
51 51 @object_types = @scope = %w(projects)
@@ -60,20 +60,26 class SearchController < ApplicationController
60 60 # strings used in sql like statement
61 61 like_tokens = @tokens.collect {|w| "%#{w.downcase}%"}
62 62 operator = @all_words ? " AND " : " OR "
63 limit = 10
64 63 @results = []
64 limit = 10
65 65 if @project
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'
67 Journal.with_scope :find => {:conditions => ["#{Issue.table_name}.project_id = ?", @project.id]} do
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'
66 @scope.each do |s|
67 @results += s.singularize.camelcase.constantize.search(like_tokens, @all_words, @project,
68 :limit => (limit+1), :offset => offset, :before => params[:previous].nil?)
69 69 end
70 @results.uniq!
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'
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'
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')
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')
75 Message.with_scope :find => {:conditions => ["#{Board.table_name}.project_id = ?", @project.id]} do
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'
70 @results = @results.sort {|a,b| b.event_datetime <=> a.event_datetime}
71 if params[:previous].nil?
72 @pagination_previous_date = @results[0].event_datetime if offset && @results[0]
73 if @results.size > limit
74 @pagination_next_date = @results[limit-1].event_datetime
75 @results = @results[0, limit]
76 end
77 else
78 @pagination_next_date = @results[-1].event_datetime if offset && @results[-1]
79 if @results.size > limit
80 @pagination_previous_date = @results[-(limit)].event_datetime
81 @results = @results[-(limit), limit]
82 end
77 83 end
78 84 else
79 85 Project.with_scope(:find => {:conditions => Project.visible_by(logged_in_user)}) do
@@ -86,6 +92,7 class SearchController < ApplicationController
86 92 else
87 93 @question = ""
88 94 end
95 render :layout => false if request.xhr?
89 96 end
90 97
91 98 private
@@ -17,7 +17,7
17 17
18 18 module SearchHelper
19 19 def highlight_tokens(text, tokens)
20 return text unless tokens && !tokens.empty?
20 return text unless text && tokens && !tokens.empty?
21 21 regexp = Regexp.new "(#{tokens.join('|')})", Regexp::IGNORECASE
22 22 result = ''
23 23 text.split(regexp).each_with_index do |words, i|
@@ -25,6 +25,11 class Changeset < ActiveRecord::Base
25 25 :datetime => :committed_on,
26 26 :author => :committer,
27 27 :url => Proc.new {|o| {:controller => 'repositories', :action => 'revision', :id => o.repository.project_id, :rev => o.revision}}
28
29 acts_as_searchable :columns => 'comments',
30 :include => :repository,
31 :project_key => "#{Repository.table_name}.project_id",
32 :date_column => 'committed_on'
28 33
29 34 validates_presence_of :repository_id, :revision, :committed_on, :commit_date
30 35 validates_numericality_of :revision, :only_integer => true
@@ -20,7 +20,9 class Document < ActiveRecord::Base
20 20 belongs_to :category, :class_name => "Enumeration", :foreign_key => "category_id"
21 21 has_many :attachments, :as => :container, :dependent => :destroy
22 22
23 acts_as_event :url => Proc.new {|o| {:controller => 'documents', :action => 'show', :id => o.id}}
23 acts_as_searchable :columns => ['title', 'description']
24 acts_as_event :title => Proc.new {|o| "#{l(:label_document)}: #{o.title}"},
25 :url => Proc.new {|o| {:controller => 'documents', :action => 'show', :id => o.id}}
24 26
25 27 validates_presence_of :project, :title, :category
26 28 validates_length_of :title, :maximum => 60
@@ -36,8 +36,9 class Issue < ActiveRecord::Base
36 36 has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
37 37
38 38 acts_as_watchable
39 acts_as_searchable :columns => ['subject', 'description'], :with => {:journal => :issue}
39 40 acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id}: #{o.subject}"},
40 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}}
41 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}}
41 42
42 43 validates_presence_of :subject, :description, :priority, :tracker, :author, :status
43 44 validates_length_of :subject, :maximum => 255
@@ -23,4 +23,9 class Journal < ActiveRecord::Base
23 23
24 24 belongs_to :user
25 25 has_many :details, :class_name => "JournalDetail", :dependent => :delete_all
26
27 acts_as_searchable :columns => 'notes',
28 :include => :issue,
29 :project_key => "#{Issue.table_name}.project_id",
30 :date_column => "#{Issue.table_name}.created_on"
26 31 end
@@ -22,6 +22,11 class Message < ActiveRecord::Base
22 22 has_many :attachments, :as => :container, :dependent => :destroy
23 23 belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id'
24 24
25 acts_as_searchable :columns => ['subject', 'content'], :include => :board, :project_key => "project_id"
26 acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"},
27 :description => :content,
28 :url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id, :id => o.id}}
29
25 30 validates_presence_of :subject, :content
26 31 validates_length_of :subject, :maximum => 255
27 32
@@ -24,6 +24,7 class News < ActiveRecord::Base
24 24 validates_length_of :title, :maximum => 60
25 25 validates_length_of :summary, :maximum => 255
26 26
27 acts_as_searchable :columns => ['title', 'description']
27 28 acts_as_event :url => Proc.new {|o| {:controller => 'news', :action => 'show', :id => o.id}}
28 29
29 30 # returns latest news for projects visible by user
@@ -38,7 +38,11 class Project < ActiveRecord::Base
38 38 has_one :wiki, :dependent => :destroy
39 39 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'
40 40 acts_as_tree :order => "name", :counter_cache => true
41
41
42 acts_as_searchable :columns => ['name', 'description'], :project_key => 'id'
43 acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"},
44 :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}}
45
42 46 attr_protected :status, :enabled_module_names
43 47
44 48 validates_presence_of :name, :description, :identifier
@@ -21,7 +21,16 class WikiPage < ActiveRecord::Base
21 21 belongs_to :wiki
22 22 has_one :content, :class_name => 'WikiContent', :foreign_key => 'page_id', :dependent => :destroy
23 23 has_many :attachments, :as => :container, :dependent => :destroy
24
24
25 acts_as_event :title => Proc.new {|o| "#{l(:label_wiki)}: #{o.title}"},
26 :description => :text,
27 :datetime => :created_on,
28 :url => Proc.new {|o| {:controller => 'wiki', :id => o.wiki.project_id, :page => o.title}}
29
30 acts_as_searchable :columns => ['title', 'text'],
31 :include => [:wiki, :content],
32 :project_key => "#{Wiki.table_name}.project_id"
33
25 34 attr_accessor :redirect_existing_links
26 35
27 36 validates_presence_of :title
@@ -85,6 +94,10 class WikiPage < ActiveRecord::Base
85 94 def project
86 95 wiki.project
87 96 end
97
98 def text
99 content.text if content
100 end
88 101 end
89 102
90 103 class WikiDiff
@@ -15,39 +15,27
15 15 </div>
16 16
17 17 <% if @results %>
18 <h3><%= lwr(:label_result, @results.length) %></h3>
18 <h3><%= l(:label_result_plural) %></h3>
19 19 <ul>
20 20 <% @results.each do |e| %>
21 <li><p>
22 <% if e.is_a? Project %>
23 <%= link_to highlight_tokens(h(e.name), @tokens), :controller => 'projects', :action => 'show', :id => e %><br />
24 <%= highlight_tokens(e.description, @tokens) %>
25 <% elsif e.is_a? Issue %>
26 <%= link_to_issue e %>: <%= highlight_tokens(h(e.subject), @tokens) %><br />
27 <%= highlight_tokens(e.description, @tokens) %><br />
28 <i><%= e.author.name %>, <%= format_time(e.created_on) %></i>
29 <% elsif e.is_a? News %>
30 <%=l(:label_news)%>: <%= link_to highlight_tokens(h(e.title), @tokens), :controller => 'news', :action => 'show', :id => e %><br />
31 <%= highlight_tokens(e.description, @tokens) %><br />
32 <i><%= e.author.name %>, <%= format_time(e.created_on) %></i>
33 <% elsif e.is_a? Document %>
34 <%=l(:label_document)%>: <%= link_to highlight_tokens(h(e.title), @tokens), :controller => 'documents', :action => 'show', :id => e %><br />
35 <%= highlight_tokens(e.description, @tokens) %><br />
36 <i><%= format_time(e.created_on) %></i>
37 <% elsif e.is_a? WikiPage %>
38 <%=l(:label_wiki)%>: <%= link_to highlight_tokens(h(e.pretty_title), @tokens), :controller => 'wiki', :action => 'index', :id => @project, :page => e.title %><br />
39 <%= highlight_tokens(e.content.text, @tokens) %><br />
40 <i><%= e.content.author ? e.content.author.name : "Anonymous" %>, <%= format_time(e.content.updated_on) %></i>
41 <% elsif e.is_a? Changeset %>
42 <%=l(:label_revision)%> <%= link_to h(e.revision), :controller => 'repositories', :action => 'revision', :id => @project, :rev => e.revision %><br />
43 <%= highlight_tokens(e.comments, @tokens) %><br />
44 <em><%= e.committer.blank? ? e.committer : "Anonymous" %>, <%= format_time(e.committed_on) %></em>
45 <% elsif e.is_a? Message %>
46 <%=h e.board.name %>: <%= link_to_message e %><br />
47 <%= highlight_tokens(e.content, @tokens) %><br />
48 <em><%= e.author ? e.author.name : "Anonymous" %>, <%= format_time(e.created_on) %></em>
49 <% end %>
50 </p></li>
21 <li><p><%= link_to highlight_tokens(truncate(e.event_title, 255), @tokens), e.event_url %><br />
22 <%= highlight_tokens(e.event_description, @tokens) %><br />
23 <span class="author"><%= format_time(e.event_datetime) %></span></p></li>
51 24 <% end %>
52 25 </ul>
53 26 <% end %>
27
28 <p><center>
29 <% if @pagination_previous_date %>
30 <%= link_to_remote ('&#171; ' + l(:label_previous)),
31 {:update => :content,
32 :url => params.merge(:previous => 1, :offset => @pagination_previous_date.strftime("%Y%m%d%H%M%S"))
33 }, :href => url_for(params.merge(:previous => 1, :offset => @pagination_previous_date.strftime("%Y%m%d%H%M%S"))) %>&nbsp;
34 <% end %>
35 <% if @pagination_next_date %>
36 <%= link_to_remote (l(:label_next) + ' &#187;'),
37 {:update => :content,
38 :url => params.merge(:previous => nil, :offset => @pagination_next_date.strftime("%Y%m%d%H%M%S"))
39 }, :href => url_for(params.merge(:previous => nil, :offset => @pagination_next_date.strftime("%Y%m%d%H%M%S"))) %>
40 <% end %>
41 </center></p>
@@ -15,6 +15,8 Rails::Initializer.run do |config|
15 15
16 16 # Add additional load paths for sweepers
17 17 config.load_paths += %W( #{RAILS_ROOT}/app/sweepers )
18
19 config.plugin_paths = ['lib/plugins', 'vendor/plugins']
18 20
19 21 # Force all environments to use the same logger level
20 22 # (by default production uses :info, the others :debug)
@@ -350,8 +350,7 label_roadmap_due_in: Due in
350 350 label_roadmap_overdue: %s late
351 351 label_roadmap_no_issues: No issues for this version
352 352 label_search: Search
353 label_result: %d result
354 label_result_plural: %d results
353 label_result_plural: Results
355 354 label_all_words: All words
356 355 label_wiki: Wiki
357 356 label_wiki_edit: Wiki edit
@@ -350,8 +350,7 label_roadmap_due_in: Echéance dans
350 350 label_roadmap_overdue: En retard de %s
351 351 label_roadmap_no_issues: Aucune demande pour cette version
352 352 label_search: Recherche
353 label_result: %d résultat
354 label_result_plural: %d résultats
353 label_result_plural: Résultats
355 354 label_all_words: Tous les mots
356 355 label_wiki: Wiki
357 356 label_wiki_edit: Révision wiki
1 NO CONTENT: file renamed from lib/redmine/acts_as_event/init.rb to lib/plugins/acts_as_event/init.rb
1 NO CONTENT: file renamed from lib/redmine/acts_as_event/lib/acts_as_event.rb to lib/plugins/acts_as_event/lib/acts_as_event.rb
1 NO CONTENT: file renamed from lib/redmine/acts_as_watchable/init.rb to lib/plugins/acts_as_watchable/init.rb
1 NO CONTENT: file renamed from lib/redmine/acts_as_watchable/lib/acts_as_watchable.rb to lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb
@@ -1,8 +1,6
1 1 require 'redmine/access_control'
2 2 require 'redmine/menu_manager'
3 3 require 'redmine/mime_type'
4 require 'redmine/acts_as_watchable/init'
5 require 'redmine/acts_as_event/init'
6 4 require 'redmine/plugin'
7 5
8 6 begin
@@ -31,7 +31,7 class SearchControllerTest < Test::Unit::TestCase
31 31 assert_template 'index'
32 32 assert_not_nil assigns(:project)
33 33
34 get :index, :id => 1, :q => "can", :scope => ["issues", "news", "documents"]
34 get :index, :id => 1, :q => "can"
35 35 assert_response :success
36 36 assert_template 'index'
37 37 end
General Comments 0
You need to be logged in to leave comments. Login now