module DocumentsHelper # Returns true if no document as been found for a given query. def nothing_found? @matching_documents.nil? or @matching_documents.entries.empty? end # Very basic pagination. # Provides liks to Next, Prev and FirstPage when needed. def should_paginate(page,query) [(link_to("←←", :action => :show, :id => query, :page => 1) if page.number>2), (link_to("←", :action => :show, :id => query, :page => page.prev.number) if page.prev?), (link_to("→", :action => :show, :id => query, :page => page.next.number) if page.next?)].compact.join(" | ") end # Returns a localized sentence like "Results 1-10 of 12 for Zimbabwe (0.472s)" or # "Résultats 1-2 parmi 2 pour whatever (0.012s)" def describe_results(page, total_hits, dt, query) [:results.l, content_tag(:strong,"#{page.first_item_number}-#{page.last_item_number}"), :of.l, content_tag(:strong,total_hits), :for.l, content_tag(:strong,query), show_time_needed(dt) ].join(' ') end # Returns the time needed to treat the query and launch the search, with a ms precision : (0.472s) def show_time_needed(dt) content_tag(:small,'('<\1').gsub(/\v|\f/,'')) }) if document.matching_content end # Returns icon and filename for any given document. def icon_and_filename_for(document) [icon_for(document.extname),document.filename].join(" ") end # Returns the location (if avaible) of the filetype icon. # TODO: Move this hash to a .yml config file. def icon_for(filetype) pic_for_exts={ :xls=>%w{xls xlsx ods}, :doc=>%w{doc odt rtf dot docx dotx}, :pdf=>%w{pdf}, :txt=>%w{txt text tex bib log ini no_extension}, :ogg=>%w{mp3 ogg wma wav wmv tee}, :html=>%w{html htm}, :ppt=>%w{ppt pps pptx odp}, :package=>%w{gz rar zip bak}, :picture=>%w{psd jpg png gif eps bmp ico}, :cad=>%w{dwg dxf}, :exe=>%w{exe dll}, :video=>%w{avi wmv mpg mpeg}, :code=>%w{for cpp c rb java}, :insel=>%w{ins vee} } pic=pic_for_exts.find{|pic, extensions| extensions.any? { |ext| filetype.sub(/\./,'').downcase==ext} } image_tag("icons/#{pic.first}.png") if pic end # Returns a link to a backup search engine that could maybe find more results for the same query. def link_to_backup_search_engine(query) link_to :backup_search_engine_name.l, :backup_search_engine_url.l<'_blank' end # For any indexed document, returns a link to show its content. def link_to_plain_text_content(document) link_name=image_tag('icons/plain_text_small.png')<<' '<