From 83c96d4f3ef2adb8ec3226f82bc68e8f1accd6d9 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Wed, 28 Feb 2007 22:35:22 +0000 Subject: [PATCH] search engine improvements and simple test git-svn-id: http://redmine.rubyforge.org/svn/trunk@285 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/projects_controller.rb | 8 +- app/helpers/projects_helper.rb | 6 +- app/views/projects/search.rhtml | 16 +- test/fixtures/permissions.yml | 170 +++++++++++++++++++- test/functional/projects_controller_test.rb | 10 ++ 5 files changed, 195 insertions(+), 15 deletions(-) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index ab1cb3e1..a8d94342 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -544,10 +544,12 @@ class ProjectsController < ApplicationController @scope = params[:scope] || (params[:submit] ? [] : %w(issues news documents) ) if @token and @token.length > 2 + @token.strip! + like_token = "%#{@token}%" @results = [] - @results += @project.issues.find(:all, :include => :author, :conditions => ["issues.subject like ?", "%#{@token}%"] ) if @scope.include? 'issues' - @results += @project.news.find(:all, :conditions => ["news.title like ?", "%#{@token}%"], :include => :author ) if @scope.include? 'news' - @results += @project.documents.find(:all, :conditions => ["title like ?", "%#{@token}%"] ) if @scope.include? 'documents' + @results += @project.issues.find(:all, :include => :author, :conditions => ["issues.subject like ? or issues.description like ?", like_token, like_token] ) if @scope.include? 'issues' + @results += @project.news.find(:all, :conditions => ["news.title like ? or news.description like ?", like_token, like_token], :include => :author ) if @scope.include? 'news' + @results += @project.documents.find(:all, :conditions => ["title like ? or description like ?", like_token, like_token] ) if @scope.include? 'documents' end end diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb index 0c85ce24..a6c033d0 100644 --- a/app/helpers/projects_helper.rb +++ b/app/helpers/projects_helper.rb @@ -15,5 +15,9 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -module ProjectsHelper +module ProjectsHelper + def result_overview(text, token) + match = excerpt(text, token) + match ? highlight(match, token) : truncate(text, 150) + end end diff --git a/app/views/projects/search.rhtml b/app/views/projects/search.rhtml index 32e486fe..df95ee3a 100644 --- a/app/views/projects/search.rhtml +++ b/app/views/projects/search.rhtml @@ -16,15 +16,17 @@ <% @results.each do |e| %>
  • <% if e.is_a? Issue %> - <%= link_to "#{e.tracker.name} ##{e.id}", :controller => 'issues', :action => 'show', :id => e %>: <%= highlight(h(e.subject), @token) %>
    - <%= e.author.name %>, <%= format_time(e.created_on) %> + <%= link_to "#{e.tracker.name} ##{e.id}", :controller => 'issues', :action => 'show', :id => e %>: <%= highlight(h(e.subject), @token) %>
    + <%= result_overview(e.description, @token) %>
    + <%= e.author.name %>, <%= format_time(e.created_on) %> <% elsif e.is_a? News %> - <%=l(:label_news)%>: <%= link_to highlight(h(e.title), @token), :controller => 'news', :action => 'show', :id => e %>
    - <% unless e.summary.empty? %><%=h e.summary %>
    <% end %> - <%= e.author.name %>, <%= format_time(e.created_on) %> + <%=l(:label_news)%>: <%= link_to highlight(h(e.title), @token), :controller => 'news', :action => 'show', :id => e %>
    + <%= result_overview(e.description, @token) %>
    + <%= e.author.name %>, <%= format_time(e.created_on) %> <% elsif e.is_a? Document %> - <%=l(:label_document)%>: <%= link_to highlight(h(e.title), @token), :controller => 'documents', :action => 'show', :id => e %>
    - <%= format_time(e.created_on) %> + <%=l(:label_document)%>: <%= link_to highlight(h(e.title), @token), :controller => 'documents', :action => 'show', :id => e %>
    + <%= result_overview(e.description, @token) %>
    + <%= format_time(e.created_on) %> <% end %>

  • <% end %> diff --git a/test/fixtures/permissions.yml b/test/fixtures/permissions.yml index 81350e1a..2f0bc548 100644 --- a/test/fixtures/permissions.yml +++ b/test/fixtures/permissions.yml @@ -1,11 +1,20 @@ --- +permissions_052: + action: destroy_comment + id: 52 + description: label_comment_delete + controller: news + mail_enabled: false + mail_option: false + sort: 1133 + is_public: false permissions_041: action: add_file id: 41 description: button_add controller: projects mail_enabled: false - mail_option: false + mail_option: true sort: 1320 is_public: false permissions_030: @@ -35,6 +44,15 @@ permissions_008: mail_option: false sort: 221 is_public: false +permissions_053: + action: add_query + id: 53 + description: button_create + controller: projects + mail_enabled: false + mail_option: false + sort: 600 + is_public: false permissions_042: action: destroy_file id: 42 @@ -71,6 +89,24 @@ permissions_009: mail_option: false sort: 222 is_public: false +permissions_054: + action: show + id: 54 + description: button_view + controller: repositories + mail_enabled: false + mail_option: false + sort: 1450 + is_public: true +permissions_043: + action: move_issues + id: 43 + description: button_move + controller: projects + mail_enabled: false + mail_option: false + sort: 1061 + is_public: false permissions_032: action: show id: 32 @@ -98,6 +134,24 @@ permissions_010: mail_option: false sort: 320 is_public: false +permissions_055: + action: browse + id: 55 + description: label_browse + controller: repositories + mail_enabled: false + mail_option: false + sort: 1460 + is_public: true +permissions_044: + action: add_note + id: 44 + description: label_add_note + controller: issues + mail_enabled: false + mail_option: true + sort: 1057 + is_public: false permissions_033: action: download id: 33 @@ -125,13 +179,31 @@ permissions_011: mail_option: false sort: 321 is_public: false +permissions_056: + action: entry + id: 56 + description: entry + controller: repositories + mail_enabled: false + mail_option: false + sort: 1462 + is_public: true +permissions_045: + action: export_issues_pdf + id: 45 + description: label_export_pdf + controller: projects + mail_enabled: false + mail_option: false + sort: 1002 + is_public: true permissions_034: action: add_document id: 34 description: button_add controller: projects mail_enabled: false - mail_option: false + mail_option: true sort: 1220 is_public: false permissions_023: @@ -161,6 +233,24 @@ permissions_001: mail_option: false sort: 100 is_public: true +permissions_057: + action: revisions + id: 57 + description: label_view_revisions + controller: repositories + mail_enabled: false + mail_option: false + sort: 1470 + is_public: true +permissions_046: + action: export_pdf + id: 46 + description: label_export_pdf + controller: issues + mail_enabled: false + mail_option: false + sort: 1015 + is_public: true permissions_035: action: edit id: 35 @@ -176,7 +266,7 @@ permissions_024: description: label_attachment_new controller: issues mail_enabled: false - mail_option: false + mail_option: true sort: 1070 is_public: false permissions_013: @@ -197,6 +287,24 @@ permissions_002: mail_option: false sort: 105 is_public: true +permissions_058: + action: revision + id: 58 + description: label_view_revisions + controller: repositories + mail_enabled: false + mail_option: false + sort: 1472 + is_public: true +permissions_047: + action: activity + id: 47 + description: label_activity + controller: projects + mail_enabled: false + mail_option: false + sort: 160 + is_public: true permissions_036: action: destroy id: 36 @@ -233,13 +341,31 @@ permissions_003: mail_option: false sort: 110 is_public: true +permissions_059: + action: diff + id: 59 + description: diff + controller: repositories + mail_enabled: false + mail_option: false + sort: 1480 + is_public: true +permissions_048: + action: calendar + id: 48 + description: label_calendar + controller: projects + mail_enabled: false + mail_option: false + sort: 165 + is_public: true permissions_037: action: add_attachment id: 37 description: label_attachment_new controller: documents mail_enabled: false - mail_option: false + mail_option: true sort: 1223 is_public: false permissions_026: @@ -269,6 +395,24 @@ permissions_004: mail_option: false sort: 150 is_public: false +permissions_060: + action: search + id: 61 + description: label_search + controller: projects + mail_enabled: false + mail_option: false + sort: 130 + is_public: true +permissions_049: + action: gantt + id: 49 + description: label_gantt + controller: projects + mail_enabled: false + mail_option: false + sort: 166 + is_public: true permissions_038: action: destroy_attachment id: 38 @@ -305,6 +449,15 @@ permissions_005: mail_option: false sort: 151 is_public: false +permissions_050: + action: history + id: 50 + description: label_history + controller: issues + mail_enabled: false + mail_option: false + sort: 1006 + is_public: true permissions_039: action: list_files id: 39 @@ -341,6 +494,15 @@ permissions_006: mail_option: false sort: 200 is_public: true +permissions_051: + action: add_comment + id: 51 + description: label_comment_add + controller: news + mail_enabled: false + mail_option: false + sort: 1130 + is_public: false permissions_040: action: download id: 40 diff --git a/test/functional/projects_controller_test.rb b/test/functional/projects_controller_test.rb index f20f8ad0..c267981e 100644 --- a/test/functional/projects_controller_test.rb +++ b/test/functional/projects_controller_test.rb @@ -111,4 +111,14 @@ class ProjectsControllerTest < Test::Unit::TestCase assert_template 'changelog' assert_not_nil assigns(:fixed_issues) end + + def test_search + get :search, :id => 1 + assert_response :success + assert_template 'search' + + get :search, :id => 1, :token => "can", :scope => ["issues", "news", "documents"] + assert_response :success + assert_template 'search' + end end