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 %> -
- <% if e.is_a? Project %>
- <%= link_to highlight_tokens(h(e.name), @tokens), :controller => 'projects', :action => 'show', :id => e %>
- <%= highlight_tokens(e.description, @tokens) %>
- <% elsif e.is_a? Issue %>
- <%= link_to_issue e %>: <%= highlight_tokens(h(e.subject), @tokens) %>
- <%= highlight_tokens(e.description, @tokens) %>
- <%= e.author.name %>, <%= format_time(e.created_on) %>
- <% elsif e.is_a? News %>
- <%=l(:label_news)%>: <%= link_to highlight_tokens(h(e.title), @tokens), :controller => 'news', :action => 'show', :id => e %>
- <%= highlight_tokens(e.description, @tokens) %>
- <%= e.author.name %>, <%= format_time(e.created_on) %>
- <% elsif e.is_a? Document %>
- <%=l(:label_document)%>: <%= link_to highlight_tokens(h(e.title), @tokens), :controller => 'documents', :action => 'show', :id => e %>
- <%= highlight_tokens(e.description, @tokens) %>
- <%= format_time(e.created_on) %>
- <% elsif e.is_a? WikiPage %>
- <%=l(:label_wiki)%>: <%= link_to highlight_tokens(h(e.pretty_title), @tokens), :controller => 'wiki', :action => 'index', :id => @project, :page => e.title %>
- <%= highlight_tokens(e.content.text, @tokens) %>
- <%= e.content.author ? e.content.author.name : "Anonymous" %>, <%= format_time(e.content.updated_on) %>
- <% elsif e.is_a? Changeset %>
- <%=l(:label_revision)%> <%= link_to h(e.revision), :controller => 'repositories', :action => 'revision', :id => @project, :rev => e.revision %>
- <%= highlight_tokens(e.comments, @tokens) %>
- <%= e.committer.blank? ? e.committer : "Anonymous" %>, <%= format_time(e.committed_on) %>
- <% elsif e.is_a? Message %>
- <%=h e.board.name %>: <%= link_to_message e %>
- <%= highlight_tokens(e.content, @tokens) %>
- <%= e.author ? e.author.name : "Anonymous" %>, <%= format_time(e.created_on) %>
- <% end %>
-
<%= link_to highlight_tokens(truncate(e.event_title, 255), @tokens), e.event_url %>
+ <%= highlight_tokens(e.event_description, @tokens) %>
+