Class: BentoSearch::StandardDecorator
- Inherits:
-
DecoratorBase
- Object
- SimpleDelegator
- DecoratorBase
- BentoSearch::StandardDecorator
- Defined in:
- app/item_decorators/bento_search/standard_decorator.rb
Instance Method Summary collapse
-
#any_present?(*keys) ⇒ Boolean
convenience method that returns true if any of the keys are #present? eg item.any_present?(:source_title, :authors) === item.source_title.present? || item.authors.present?.
-
#author_display(author) ⇒ Object
How to display a BentoSearch::Author object as a name.
-
#complete_title ⇒ Object
Mix-in a default missing title marker for empty titles (Used to combine title and subtitle when those were different fields).
-
#display_date ⇒ Object
outputs a date for display, from #publication_date or #year.
-
#display_format ⇒ Object
format string to display to user.
-
#display_language ⇒ Object
A display method, this is like #langauge_str, but will be nil if the language_code matches the current default locale, used for printing language only when not “English” normally.
-
#has_source_info? ⇒ Boolean
if enough info is present that there will be non-empty render_source_info should be over-ridden to match display_source_info.
-
#html_id(prefix, index) ⇒ Object
Can be used as an id attribute for anchor destination in HTML.
-
#render_authors_list ⇒ Object
display multiple authors, with HTML markup, returns html_safe string.
-
#render_citation_details ⇒ Object
volume, issue, and page numbers.
-
#render_source_info ⇒ Object
Returns source publication name OR publisher, along with volume/issue/pages if present, all wrapped in various tags and labels.
-
#render_summary ⇒ Object
A summary.
-
#to_openurl ⇒ Object
Returns a ruby OpenURL::ContextObject (NISO Z39.88).
-
#to_openurl_kev ⇒ Object
Returns a kev encoded openurl, that is a URL query string representing openurl.
-
#uri_identifier ⇒ Object
A unique opaque identifier for a record may sometimes be required, for instance in Atom.
Methods inherited from DecoratorBase
#_base, #_h, decorate, #html_escape, #initialize
Constructor Details
This class inherits a constructor from BentoSearch::DecoratorBase
Instance Method Details
#any_present?(*keys) ⇒ Boolean
convenience method that returns true if any of the keys are #present? eg item.any_present?(:source_title, :authors) === item.source_title.present? || item.authors.present?
note present? is false for nil, empty strings, and empty arrays.
10 11 12 13 14 15 |
# File 'app/item_decorators/bento_search/standard_decorator.rb', line 10 def any_present?(*keys) keys.each do |key| return true if self.send(key).present? end return false end |
#author_display(author) ⇒ Object
How to display a BentoSearch::Author object as a name
18 19 20 21 22 23 24 25 26 27 28 |
# File 'app/item_decorators/bento_search/standard_decorator.rb', line 18 def () if (.first.present? && .last.present?) "#{.last}, #{.first.slice(0,1)}" elsif .display.present? .display elsif .last.present? .last else nil end end |
#complete_title ⇒ Object
Mix-in a default missing title marker for empty titles (Used to combine title and subtitle when those were different fields)
92 93 94 95 96 97 98 |
# File 'app/item_decorators/bento_search/standard_decorator.rb', line 92 def complete_title if self.title.present? self.title else I18n.translate("bento_search.missing_title") end end |
#display_date ⇒ Object
outputs a date for display, from #publication_date or #year. Uses it’s own logic to decide whether to output entire date or just year, if it has a complete date. (If volume and issue are present, just year).
Over-ride in a decorator if you want to always or never or different logic for complete date. Or if you want to change the format of the date, etc.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'app/item_decorators/bento_search/standard_decorator.rb', line 180 def display_date if self.publication_date if self.volume && self.issue # just the year, ma'am I18n.localize(self.publication_date, :format => "%Y") else # whole date, since we got it I18n.localize(self.publication_date, :format => "%d %b %Y") end elsif self.year self.year.to_s else nil end end |
#display_format ⇒ Object
format string to display to user. Uses #format_str if present, otherwise finds an i18n label from #format. Returns nil if none available.
165 166 167 168 169 170 |
# File 'app/item_decorators/bento_search/standard_decorator.rb', line 165 def display_format value = self.format_str || I18n.t(self.format, :scope => [:bento_search, :format], :default => self.format.to_s.titleize) return value.blank? ? nil : value end |
#display_language ⇒ Object
A display method, this is like #langauge_str, but will be nil if the language_code matches the current default locale, used for printing language only when not “English” normally.
(Sorry, will be ‘Spanish’ never ‘Espa~nol“, we don’t have a data source for language names in other languages right now. )
152 153 154 155 156 157 158 159 160 |
# File 'app/item_decorators/bento_search/standard_decorator.rb', line 152 def display_language default = I18n.locale.try {|l| l.to_s.gsub(/\-.*$/, '')} || "en" this_doc = self.language_obj.try(:iso_639_1) return nil if this_doc == default self.language_str end |
#has_source_info? ⇒ Boolean
if enough info is present that there will be non-empty render_source_info should be over-ridden to match display_source_info
86 87 88 |
# File 'app/item_decorators/bento_search/standard_decorator.rb', line 86 def has_source_info? self.any_present?(:source_title, :publisher, :start_page) end |
#html_id(prefix, index) ⇒ Object
Can be used as an id attribute for anchor destination in HTML. Will return “#prefix_#index” – if prefix is missing, will use #engine_id if present. If both are missing, returns nil. if index missing, returns nil.
222 223 224 225 226 227 228 229 230 |
# File 'app/item_decorators/bento_search/standard_decorator.rb', line 222 def html_id(prefix, index) prefix = prefix || self.engine_id prefix, index = prefix.to_s, index.to_s return nil if index.empty? return nil if prefix.empty? return "#{prefix}_#{index}" end |
#render_authors_list ⇒ Object
display multiple authors, with HTML markup, returns html_safe string. experimentally trying this as a decorator helper method rather than a view partial, not sure which is best.
Will limit to first three authors, with elipsis if there are more.
Over-ride if you want to format authors names differently, or show more or less than first 3, etc.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'app/item_decorators/bento_search/standard_decorator.rb', line 38 def parts = [] first_three = self..slice(0,3) first_three.each_with_index do |, index| parts << _h.content_tag("span", :class => "author") do self.() end if (index + 1) < first_three.length parts << "; " end end if self..length > 3 parts << I18n.t("bento_search.authors_et_al") end return _h.safe_join(parts, "") end |
#render_citation_details ⇒ Object
volume, issue, and page numbers. With prefixed labels from I18n. That’s it.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'app/item_decorators/bento_search/standard_decorator.rb', line 104 def render_citation_details # \u00A0 is unicode non-breaking space to keep labels and values from # getting separated. result_elements = [] result_elements.push("#{I18n.t('bento_search.volume')}\u00A0#{volume}") if volume.present? result_elements.push("#{I18n.t('bento_search.issue')}\u00A0#{issue}") if issue.present? if (! start_page.blank?) && (! end_page.blank?) result_elements.push html_escape "#{I18n.t('bento_search.pages')}\u00A0#{start_page}-#{end_page}" elsif ! start_page.blank? result_elements.push html_escape "#{I18n.t('bento_search.page')}\u00A0#{start_page}" end return nil if result_elements.empty? return result_elements.join(", ").html_safe end |
#render_source_info ⇒ Object
Returns source publication name OR publisher, along with volume/issue/pages if present, all wrapped in various tags and labels. Returns html_safe with tags.
Experiment to do this in a decorator helper instead of a partial template, might be more convenient we think.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'app/item_decorators/bento_search/standard_decorator.rb', line 65 def render_source_info parts = [] if self.source_title.present? parts << _h.content_tag("span", I18n.t("bento_search.published_in"), :class=> "source_label") parts << _h.content_tag("span", self.source_title, :class => "source_title") parts << ". " elsif self.publisher.present? parts << _h.content_tag("span", self.publisher, :class => "publisher") parts << ". " end if text = self.render_citation_details parts << text << "." end return _h.safe_join(parts, "") end |
#render_summary ⇒ Object
A summary. If config.for_dispaly.prefer_snippets_as_summary is set to true then prefers that, otherwise abstract.
Truncates for display.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'app/item_decorators/bento_search/standard_decorator.rb', line 128 def render_summary summary = nil max_chars = (self.display_configuration.try {|h| h["summary_max_chars"]}) || 280 if self.snippets.length > 0 && !(self.display_configuration.try {|h| h["prefer_abstract_as_summary"]} && self.abstract) summary = self.snippets.first self.snippets.slice(1, self.snippets.length).each do |snippet| summary += ' '.html_safe + snippet if (summary.length + snippet.length) <= max_chars end else summary = _h.bento_truncate( self.abstract, :length => max_chars ) end summary end |
#to_openurl ⇒ Object
Returns a ruby OpenURL::ContextObject (NISO Z39.88). or nil if none avail.
243 244 245 246 247 |
# File 'app/item_decorators/bento_search/standard_decorator.rb', line 243 def to_openurl return nil if openurl_disabled BentoSearch::OpenurlCreator.new(self).to_openurl end |
#to_openurl_kev ⇒ Object
Returns a kev encoded openurl, that is a URL query string representing openurl. Or nil if none available.
Right now just calls #to_openurl.kev, can conceivably be modified to do things more efficient, without a ruby openurl obj. Law of demeter, represent.
255 256 257 |
# File 'app/item_decorators/bento_search/standard_decorator.rb', line 255 def to_openurl_kev to_openurl.try(:kev) end |
#uri_identifier ⇒ Object
A unique opaque identifier for a record may sometimes be required, for instance in Atom.
We here provide a really dumb implementation, if and only if the result has an engine_id and unique_id available, (and a #root_url is available) by basically concatenating them to app base url.
That’s pretty lame, probably not resolvable, but best we can do without knowing details of host app. You may want to over-ride this in a decorator to do something more valid in an app-specific way.
yes uri_identifier is like PIN number, deal with it.
210 211 212 213 214 215 216 |
# File 'app/item_decorators/bento_search/standard_decorator.rb', line 210 def uri_identifier if self.engine_id.present? && self.unique_id.present? && _h.respond_to?(:root_url) "#{_h.root_url.chomp("/")}/bento_search_opaque_id/#{CGI.escape self.engine_id}/#{CGI.escape self.unique_id}" else nil end end |