annotate app/helpers/search_helper.rb @ 8:0c83d98252d9 yuya

* Add custom repo prefix and proper auth realm, remove auth cache (seems like an unwise feature), pass DB handle around, various other bits of tidying
author Chris Cannam
date Thu, 12 Aug 2010 15:31:37 +0100
parents 513646585e45
children 94944d00e43c
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@0 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@0 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 module SearchHelper
Chris@0 19 def highlight_tokens(text, tokens)
Chris@0 20 return text unless text && tokens && !tokens.empty?
Chris@0 21 re_tokens = tokens.collect {|t| Regexp.escape(t)}
Chris@0 22 regexp = Regexp.new "(#{re_tokens.join('|')})", Regexp::IGNORECASE
Chris@0 23 result = ''
Chris@0 24 text.split(regexp).each_with_index do |words, i|
Chris@0 25 if result.length > 1200
Chris@0 26 # maximum length of the preview reached
Chris@0 27 result << '...'
Chris@0 28 break
Chris@0 29 end
Chris@0 30 words = words.mb_chars
Chris@0 31 if i.even?
Chris@0 32 result << h(words.length > 100 ? "#{words.slice(0..44)} ... #{words.slice(-45..-1)}" : words)
Chris@0 33 else
Chris@0 34 t = (tokens.index(words.downcase) || 0) % 4
Chris@0 35 result << content_tag('span', h(words), :class => "highlight token-#{t}")
Chris@0 36 end
Chris@0 37 end
Chris@0 38 result
Chris@0 39 end
Chris@0 40
Chris@0 41 def type_label(t)
Chris@0 42 l("label_#{t.singularize}_plural")
Chris@0 43 end
Chris@0 44
Chris@0 45 def project_select_tag
Chris@0 46 options = [[l(:label_project_all), 'all']]
Chris@0 47 options << [l(:label_my_projects), 'my_projects'] unless User.current.memberships.empty?
Chris@0 48 options << [l(:label_and_its_subprojects, @project.name), 'subprojects'] unless @project.nil? || @project.descendants.active.empty?
Chris@0 49 options << [@project.name, ''] unless @project.nil?
Chris@0 50 select_tag('scope', options_for_select(options, params[:scope].to_s)) if options.size > 1
Chris@0 51 end
Chris@0 52
Chris@0 53 def render_results_by_type(results_by_type)
Chris@0 54 links = []
Chris@0 55 # Sorts types by results count
Chris@0 56 results_by_type.keys.sort {|a, b| results_by_type[b] <=> results_by_type[a]}.each do |t|
Chris@0 57 c = results_by_type[t]
Chris@0 58 next if c == 0
Chris@0 59 text = "#{type_label(t)} (#{c})"
Chris@0 60 links << link_to(text, :q => params[:q], :titles_only => params[:title_only], :all_words => params[:all_words], :scope => params[:scope], t => 1)
Chris@0 61 end
Chris@0 62 ('<ul>' + links.map {|link| content_tag('li', link)}.join(' ') + '</ul>') unless links.empty?
Chris@0 63 end
Chris@0 64 end