File: app/helpers/search_helper.rb

Overview
Module Structure
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: SearchHelper#20
has properties
method: highlight_tokens / 2 #21
method: type_label / 1 #43
method: project_select_tag #47
method: render_results_by_type / 1 #56

Code

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