module Birds::App::Helpers::View
Public Instance Methods
cl(text)
click to toggle source
# File lib/birds/app/helpers/view.rb, line 43 def cl(text) text .gsub(settings.highlighting_prefix, '') .gsub(settings.highlighting_postfix, '') end
facet_query(field, value, gap = nil)
click to toggle source
# File lib/birds/app/helpers/view.rb, line 81 def facet_query(field, value, gap = nil) gap ? range_query(field, value, gap) : field_query(field, value) end
field_query(field, value)
click to toggle source
# File lib/birds/app/helpers/view.rb, line 85 def field_query(field, value) %Q{#{field}:"#{value}"} end
glyphicon(name, options = {})
click to toggle source
# File lib/birds/app/helpers/view.rb, line 127 def glyphicon(name, options = {}) name, options = name.to_s.tr('_', '-'), options.dup options[:class] = %W[glyphicon glyphicon-#{name}] .push(*options.delete(:class)).compact.join(' ') tag_(:span, options) end
hl(text)
click to toggle source
# File lib/birds/app/helpers/view.rb, line 37 def hl(text) h(text) .gsub(h(settings.highlighting_prefix), settings.highlighting_prefix) .gsub(h(settings.highlighting_postfix), settings.highlighting_postfix) end
link_to_document(document, highlighting = {})
click to toggle source
# File lib/birds/app/helpers/view.rb, line 49 def link_to_document(document, highlighting = {}) label = format_label(settings.document_label) { |k| h, d = highlighting.delete(k), document[k] h ? d.map { |v| h.find { |w| cl(w) == v } || v } : d } label = "##{document.id}" if label.empty? link_to(hl(label), :document, document.id) end
link_to_facet(field, value, gap = nil)
click to toggle source
# File lib/birds/app/helpers/view.rb, line 75 def link_to_facet(field, value, gap = nil) link_to_filter( gap ? range_label(value, gap) : value, @query, facet_query(field, value, gap)) end
link_to_field(field, value, query = nil)
click to toggle source
# File lib/birds/app/helpers/view.rb, line 71 def link_to_field(field, value, query = nil) link_to_filter(value, query, field_query(field, value)) end
link_to_filter(text, query, filter)
click to toggle source
# File lib/birds/app/helpers/view.rb, line 67 def link_to_filter(text, query, filter) link_to_search(hl(text), query, cl(filter), *@filter) end
link_to_search(text, query, *filters)
click to toggle source
# File lib/birds/app/helpers/view.rb, line 60 def link_to_search(text, query, *filters) options = filters.last.is_a?(Hash) ? filters.pop : {} link_to(text, :search, options.merge(params: query ? query_params(query, filters) : { q: filters.first })) end
pagination_for(*args)
click to toggle source
# File lib/birds/app/helpers/view.rb, line 103 def pagination_for(*args) params = args.last.is_a?(Hash) ? args.pop.reject { |_, v| v.nil? } : {} ul_([ [@prev_page, :first, 1], [@prev_page, :prev, @prev_page], [@next_page, :next, @next_page], [@next_page, :last, @total_pages] ], class: 'pagination') { |condition, type, page| [link_to_if(condition, pagination_icon(type), *args, rel: type, params: params.merge(page: page)), class: disabled?(condition)] } end
pagination_icon(type)
click to toggle source
# File lib/birds/app/helpers/view.rb, line 118 def pagination_icon(type) tag_(:span, glyphicon(*{ first: [:fast_backward, title: 'First page'], prev: [:backward, title: 'Previous page'], next: [:forward, title: 'Next page'], last: [:fast_forward, title: 'Last page'] }[type])) end
query_params(q = @query, fq = @filter)
click to toggle source
# File lib/birds/app/helpers/view.rb, line 99 def query_params(q = @query, fq = @filter) { q: q, 'fq[]' => fq } end
range_label(value, gap)
click to toggle source
# File lib/birds/app/helpers/view.rb, line 95 def range_label(value, gap) value < 0 ? "before #{-value}" : "#{value}–#{value + gap - 1}" end
range_query(field, value, gap)
click to toggle source
# File lib/birds/app/helpers/view.rb, line 89 def range_query(field, value, gap) value < 0 ? %Q|#{field}:[* TO #{-value}}| : %Q|#{field}:[#{value} TO #{value + gap}}| end
values_for(key, document = @document)
click to toggle source
# File lib/birds/app/helpers/view.rb, line 136 def values_for(key, document = @document) Array(document[key = key.to_s]).dup.tap { |values| return if values.empty? if settings.browse_fields.include?(key) values.map! { |v| link_to(v = hl(v), :browse, key, v) } elsif field = settings.linkable_fields[key] values.map! { |v| link_to_field(field, v) } else values.map!(&method(:hl)) end } end