Module: DocumentsHelper
- Defined in:
- lib/picolena/templates/app/helpers/documents_helper.rb
Instance Method Summary collapse
-
#describe_results(page, total_hits, dt, query) ⇒ Object
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)”.
-
#highlight_matching_content(document) ⇒ Object
When possible, highlights content of the document that matches the query.
-
#highlighted_cache(document, query) ⇒ Object
Returns multicolor cache à la Google.
-
#icon_and_filename_for(document) ⇒ Object
Returns icon and filename for any given document.
-
#icon_for(document) ⇒ Object
Returns the location (if avaible) of the filetype icon.
- #language_icon_for(document, query) ⇒ Object
-
#link_to_backup_search_engine(query) ⇒ Object
Returns a link to a backup search engine that could maybe find more results for the same query.
-
#link_to_cached_content(document, query) ⇒ Object
For any indexed document, returns a link to show its cached content.
-
#link_to_containing_directory(document) ⇒ Object
For any indexed document, returns a link to its containing directory.
-
#link_to_plain_text_content(document) ⇒ Object
For any indexed document, returns a link to show its content.
-
#nothing_found? ⇒ Boolean
Returns true if no document as been found for a given query.
-
#should_paginate(page, query, sort_by) ⇒ Object
Very basic pagination.
-
#show_time_needed(dt) ⇒ Object
Returns the time needed to treat the query and launch the search, with a ms precision : (0.472s).
- #sort_by_date_or_relevance(query, params) ⇒ Object
Instance Method Details
#describe_results(page, total_hits, dt, query) ⇒ Object
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)”
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/picolena/templates/app/helpers/documents_helper.rb', line 17 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 |
#highlight_matching_content(document) ⇒ Object
When possible, highlights content of the document that matches the query.
34 35 36 37 38 |
# File 'lib/picolena/templates/app/helpers/documents_helper.rb', line 34 def highlight_matching_content(document) content_tag(:ul,document.matching_content.collect{|sentence| content_tag(:li,h(sentence).gsub(/<<(.*?)>>/,'<strong>\1</strong>').gsub(/\v|\f/,'')) }) if document.matching_content end |
#highlighted_cache(document, query) ⇒ Object
Returns multicolor cache à la Google.
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/picolena/templates/app/helpers/documents_helper.rb', line 82 def highlighted_cache(document, query) # What are the terms related to :content field? content_terms=Query.content_terms(query) # Parses cache for found terms # Replaces linebreak by <br/> # < and > by < and > h(document.highlighted_cache(query)).gsub(/\n/,'<br/>').gsub(/<<(.*?)>>/){|c| term=$1 # and affects a span class to each term content_tag(:span, term, :class=>"matching_content_#{content_terms.index(term.downcase)}") } end |
#icon_and_filename_for(document) ⇒ Object
Returns icon and filename for any given document.
48 49 50 |
# File 'lib/picolena/templates/app/helpers/documents_helper.rb', line 48 def icon_and_filename_for(document) [icon_for(document),document.filename].join(" ") end |
#icon_for(document) ⇒ Object
Returns the location (if avaible) of the filetype icon.
53 54 55 56 |
# File 'lib/picolena/templates/app/helpers/documents_helper.rb', line 53 def icon_for(document) path=document.icon_path image_tag(document.icon_path) if path end |
#language_icon_for(document, query) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/picolena/templates/app/helpers/documents_helper.rb', line 40 def language_icon_for(document,query) lang=document.language return if lang.blank? query_with_lang= query=~/#{:language.l}/ ? query : "#{query} lang:#{lang}" link_to(image_tag("flags/#{lang}.png"), document_url(query_with_lang)) end |
#link_to_backup_search_engine(query) ⇒ Object
Returns a link to a backup search engine that could maybe find more results for the same query.
59 60 61 |
# File 'lib/picolena/templates/app/helpers/documents_helper.rb', line 59 def link_to_backup_search_engine(query) link_to :backup_search_engine_name.l, :backup_search_engine_url.l<<query end |
#link_to_cached_content(document, query) ⇒ Object
For any indexed document, returns a link to show its cached content.
76 77 78 79 |
# File 'lib/picolena/templates/app/helpers/documents_helper.rb', line 76 def link_to_cached_content(document, query) link_name="("<<content_tag(:small,:cached.l)<<")" link_to link_name, cached_document_path(:id => document.probably_unique_id, :query => query) end |
#link_to_containing_directory(document) ⇒ Object
For any indexed document, returns a link to its containing directory.
64 65 66 67 |
# File 'lib/picolena/templates/app/helpers/documents_helper.rb', line 64 def link_to_containing_directory(document) link_name=image_tag('icons/remote_folder.png')<<' '<<content_tag(:small,document.alias_path) link_to link_name, document.alias_path, :target=>'_blank' end |
#link_to_plain_text_content(document) ⇒ Object
For any indexed document, returns a link to show its content.
70 71 72 73 |
# File 'lib/picolena/templates/app/helpers/documents_helper.rb', line 70 def link_to_plain_text_content(document) link_name=image_tag('icons/plain_text_small.png')<<' '<<content_tag(:small,:text_content.l) link_to link_name, content_document_path(document.probably_unique_id) end |
#nothing_found? ⇒ Boolean
Returns true if no document as been found for a given query.
3 4 5 |
# File 'lib/picolena/templates/app/helpers/documents_helper.rb', line 3 def nothing_found? @matching_documents.nil? or @matching_documents.entries.empty? end |
#should_paginate(page, query, sort_by) ⇒ Object
Very basic pagination. Provides liks to Next, Prev and FirstPage when needed.
9 10 11 12 13 |
# File 'lib/picolena/templates/app/helpers/documents_helper.rb', line 9 def should_paginate(page,query, sort_by) [(link_to("←←", :action => :show, :id => query, :sort_by=>sort_by) if page.number>2), (link_to("←", :action => :show, :id => query, :page => page.prev.number, :sort_by=>sort_by) if page.prev?), (link_to("→", :action => :show, :id => query, :page => page.next.number, :sort_by=>sort_by) if page.next?)].compact.join(" | ") end |
#show_time_needed(dt) ⇒ Object
Returns the time needed to treat the query and launch the search, with a ms precision : (0.472s)
29 30 31 |
# File 'lib/picolena/templates/app/helpers/documents_helper.rb', line 29 def show_time_needed(dt) content_tag(:small,'('<<number_with_precision(dt,:precision => 3)<<'s)') end |
#sort_by_date_or_relevance(query, params) ⇒ Object
94 95 96 97 |
# File 'lib/picolena/templates/app/helpers/documents_helper.rb', line 94 def sort_by_date_or_relevance(query,params) [link_to_if(params[:sort_by]!='date',:by_date.l, document_path(query, :sort_by=>'date')), link_to_if(params[:sort_by],:by_relevance.l, document_path(query))].join(" ") end |