Module: SearchHelper
- Defined in:
- app/helpers/search_helper.rb
Overview
Markup generators for the search controller
Instance Method Summary (collapse)
-
- (String) document_bibliography_entry(doc)
Get the short, formatted representation of a document.
-
- (String) facet_link(text, facets)
Create a link to the given set of facets.
-
- (String) facet_link_list
Return a set of list items for faceted browsing.
-
- (String) list_links_for_facet(field, header, active_facets)
Get the list of facet links for one particular field.
-
- (String) num_results_string
Return a formatted version of the number of documents in the last search.
-
- (String) page_link(text, num, icon = '', right = false)
Make a link to a page for the pagination widget.
-
- (String) render_pagination
Render the pagination links.
-
- (Array<String>) sort_methods
Return an array of all sort methods.
-
- (String) sort_to_string(sort)
Get the given sort method as a string.
Instance Method Details
- (String) document_bibliography_entry(doc)
Get the short, formatted representation of a document
This function returns the short bibliographic entry for a document that will appear in the search results list. The formatting here depends on the current user's settings. By default, we use a jQuery Mobile-formatted partial with an H3 and some P's. The user can set, however, to format the bibliographic entries using their favorite CSL style.
276 277 278 279 280 281 282 |
# File 'app/helpers/search_helper.rb', line 276 def document_bibliography_entry(doc) if user_signed_in? && current_user.csl_style return doc.to_csl_entry(current_user.csl_style) end render :partial => 'document', :locals => { :document => doc } end |
- (String) facet_link(text, facets)
Create a link to the given set of facets
This function converts an array of facets to a link (generated via link_to) to the search page for that filtered query. All parameters other than :fq are simply duplicated (including the search query itself, :q).
165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'app/helpers/search_helper.rb', line 165 def facet_link(text, facets) new_params = params.dup if facets.empty? new_params[:fq] = nil return link_to text, search_path(new_params), 'data-transition' => 'none' end new_params[:fq] = [] facets.each { |f| new_params[:fq] << f.query } link_to text, search_path(new_params), 'data-transition' => 'none' end |
- (String) facet_link_list
Return a set of list items for faceted browsing
This function queries both the active facets on the current search and the available facets for authors, journals, and years. It returns a set of <li> elements (not a <ul>), including list dividers.
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'app/helpers/search_helper.rb', line 227 def facet_link_list # Make sure there are facets return ''.html_safe unless Document.facets # Convert the active facet queries to facets active_facets = [] if params[:fq] params[:fq].each do |query| active_facets << Document.facets.for_query(query) end active_facets.compact! end # Start with the active facets ret = ''.html_safe unless active_facets.empty? ret << content_tag(:li, I18n.t('search.index.active_filters'), 'data-role' => 'list-divider') ret << content_tag(:li, 'data-icon' => 'delete') do facet_link I18n.t('search.index.remove_all'), [] end active_facets.each do |f| ret << content_tag(:li, 'data-icon' => 'delete') do facet_link "#{f.field_label}: #{f.label}", active_facets.reject { |x| x == f } end end end # Run the facet-list code for all three facet fields ret << list_links_for_facet(:authors_facet, I18n.t('search.index.authors_facet'), active_facets) ret << list_links_for_facet(:journal_facet, I18n.t('search.index.journal_facet'), active_facets) ret << list_links_for_facet(:year, I18n.t('search.index.year_facet'), active_facets) ret end |
- (String) list_links_for_facet(field, header, active_facets)
Get the list of facet links for one particular field
This function takes the facets from the Document class, checks them against active_facets, and creates a set of list items. It is used by facet_link_list.
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'app/helpers/search_helper.rb', line 192 def list_links_for_facet(field, header, active_facets) return ''.html_safe unless Document.facets # Get the facets for this field facets = Document.facets.sorted_for_field(field).reject { |f| active_facets.include? f }.take(5) # Bail if there's no facets ret = ''.html_safe return ret if facets.empty? # Build the return value ret << content_tag(:li, header, 'data-role' => 'list-divider') facets.each do |f| ret << content_tag(:li) do # Link to whatever the current facets are, plus the new one link = facet_link f.label, active_facets + [f] count = content_tag :span, f.hits.to_s, :class => 'ui-li-count' link + count end end ret end |
- (String) num_results_string
Return a formatted version of the number of documents in the last search
12 13 14 15 16 17 18 |
# File 'app/helpers/search_helper.rb', line 12 def num_results_string if params[:precise] or params[:q] or params[:fq] I18n.t 'search.index.num_results_found', :count => Document.num_results else I18n.t 'search.index.num_results_database', :count => Document.num_results end end |
- (String) page_link(text, num, icon = '', right = false)
Make a link to a page for the pagination widget
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/helpers/search_helper.rb', line 31 def page_link(text, num, icon = '', right = false) new_params = params.dup if num == 0 new_params.delete :page else new_params[:page] = num end style = { 'data-transition' => :none, 'data-role' => :button } style['data-icon'] = icon unless icon.empty? style['data-iconpos'] = 'right' if right link_to text, search_path(new_params), style end |
- (String) render_pagination
Render the pagination links
We currently render four buttons, in a 4x1 grid: first, previous, next, and last. Pagination is difficult for an application like this; we don't want infinite scroll, as there are far too many items, but full pagination (like that on Google or Flickr) really doesn't work on mobile devices. So this is a compromise.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/helpers/search_helper.rb', line 58 def render_pagination num_pages = Document.num_results.to_f / @per_page.to_f num_pages = Integer(num_pages.ceil) return '' if num_pages == 0 content_tag :div, :class => 'ui-grid-c' do content = content_tag(:div, :class => 'ui-block-a') do if @page != 0 page_link(I18n.t(:search.index.first_button'), 0, 'back') end end content << content_tag(:div, :class => 'ui-block-b') do if @page != 0 page_link(I18n.t(:search.index.previous_button'), @page - 1, 'arrow-l') end end content << content_tag(:div, :class => 'ui-block-c') do if @page != (num_pages - 1) page_link(I18n.t(:search.index.next_button'), @page + 1, 'arrow-r', true) end end content << content_tag(:div, :class => 'ui-block-d') do if @page != (num_pages - 1) page_link(I18n.t(:search.index.last_button'), num_pages - 1, 'forward', true) end end content end end |
- (Array<String>) sort_methods
Return an array of all sort methods
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'app/helpers/search_helper.rb', line 98 def sort_methods [ 'score desc', 'authors_sort asc', 'authors_sort desc', 'title_sort asc', 'title_sort desc', 'journal_sort asc', 'journal_sort desc', 'year_sort asc', 'year_sort desc' ] end |
- (String) sort_to_string(sort)
Get the given sort method as a string
This function converts a sort method ('relevance', 'title', 'author', 'journal', 'year') and sort direction ('asc' or 'desc') into a user-friendly string.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'app/helpers/search_helper.rb', line 124 def sort_to_string(sort) parts = sort.split(' ') return I18n.t('search.index.sort_unknown') unless parts.count == 2 method = parts[0] dir = I18n.t("search.index.sort_#{parts[1]}") method_spec = case method when 'score' I18n.t('search.index.sort_score') when 'title_sort' "#{Document.human_attribute_name('title')} #{dir}" when 'authors_sort' "#{Document.human_attribute_name('authors')} #{dir}" when 'journal_sort' "#{Document.human_attribute_name('journal')} #{dir}" when 'year_sort' "#{Document.human_attribute_name('year')} #{dir}" end "#{I18n.t('search.index.sort_prefix')} #{method_spec}" end |