Module: ResolveHelper
- Defined in:
- app/helpers/resolve_helper.rb
Constant Summary collapse
- @@bg_update_sections =
Methods to grab SectionRenderer definitions from config. Caching in class-level variables.
nil
- @@partial_update_sections =
nil
Instance Method Summary collapse
-
#bg_update_sections ⇒ Object
Called by background updater to get a list of all sections configured in application config parameter resolve_sections to be included in background updates.
-
#config_resolve_sections ⇒ Object
Looks up umlaut_config.lookup!(“resolve_sections”), then passes it through resolve_sections_filter config if available – caches result in an iVar, for this action.
-
#cover_image_response(size = 'medium') ⇒ Object
size can be ‘small’, ‘medium’, or ‘large.
-
#coverage_summary(response) ⇒ Object
Outputs “yyyy - yyyy” coverage summary, with html tags, IF coverage dates are available, it is a title-level request, and we’re configured to show with config resolve_display.show_coverage_summary.
- #display_not_found_warning?(uml_request) ⇒ Boolean
-
#expand_contract_section(arg_heading, id, options = {}, &block) ⇒ Object
Generate content in an expand-contract block, with a heading that you can click on to show/hide it.
- #html_section_by_div_id(div_id) ⇒ Object
-
#html_sections(area) ⇒ Object
Called by resolve/index view to find sections configured in application config resolve_sections list for a specific part of the page.
- #item_icon(section_id) ⇒ Object
-
#list_with_limit(id, list, options = {}, &block) ⇒ Object
Code-generating helper to add a “More” link to a list, with a maximum number of items to show before ‘more’.
-
#partial_html_sections ⇒ Object
Called by partial_html_sections action to get a list of all sections that should be included in the partial_html_sections api response.
-
#render_section(arguments = {}) ⇒ Object
Will render an Umlaut HTML section.
-
#response_content(service_response) ⇒ Object
If response has a :content key returns it – and marks it html_safe if response has a :content_html_safe == true key.
-
#section_css_classes(umlaut_request, section_id, unused = nil) ⇒ Object
Return an array of css classes that should be attached to an .umlaut_section generally ‘umlaut-section’, plus the section_id, plus possibly ‘umlaut-section-highlighted’.
-
#section_highlights(umlaut_request = @umlaut_request) ⇒ Object
Returns a section highlighter for the current umlaut request.
-
#should_highlight_section?(umlaut_request, section_id) ⇒ Boolean
Deprecated! Left for backwards compat.
-
#user_entered_citation?(uml_request) ⇒ Boolean
Did this come from citation linker style entry? We check the referrer.
Instance Method Details
#bg_update_sections ⇒ Object
Called by background updater to get a list of all sections configured in application config parameter resolve_sections to be included in background updates.
198 199 200 201 202 203 204 205 |
# File 'app/helpers/resolve_helper.rb', line 198 def bg_update_sections unless (@@bg_update_sections) @@bg_update_sections = config_resolve_sections.find_all do |section| section[:bg_update] != false end end @@bg_update_sections end |
#config_resolve_sections ⇒ Object
Looks up umlaut_config.lookup!(“resolve_sections”), then passes it through resolve_sections_filter config if available – caches result in an iVar, for this action.
230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'app/helpers/resolve_helper.rb', line 230 def config_resolve_sections unless defined? @_config_resolve_sections @_config_resolve_sections = umlaut_config.lookup!("resolve_sections", UmlautConfigurable::ResolveSectionsArray.new) # deep dup it, so we aren't reordering the original @_config_resolve_sections = @_config_resolve_sections.deep_dup umlaut_config.lookup!("resolve_sections_filter", []).each do |reorder_proc| reorder_proc.call(@user_request, @_config_resolve_sections) end end return @_config_resolve_sections end |
#cover_image_response(size = 'medium') ⇒ Object
size can be ‘small’, ‘medium’, or ‘large. returns a ServiceResponse object, or nil.
69 70 71 72 73 74 75 |
# File 'app/helpers/resolve_helper.rb', line 69 def cover_image_response(size='medium') cover_images = get_service_type('cover_image') cover_images.each do |service_response| return service_response if service_response.service_data[:size] == size end return nil end |
#coverage_summary(response) ⇒ Object
Outputs “yyyy - yyyy” coverage summary, with html tags, IF coverage dates are available, it is a title-level request, and we’re configured to show with config resolve_display.show_coverage_summary
259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'app/helpers/resolve_helper.rb', line 259 def coverage_summary(response) unless (@user_request.title_level_citation? && umlaut_config.lookup!("resolve_display.show_coverage_summary", false) && (response[:coverage_begin_date] || response[:coverage_end_date])) return nil end start = response[:coverage_begin_date].try(:year) || I18n.t("umlaut.citation.coverage_summary.open_start") finish = response[:coverage_end_date].try(:year) || I18n.t("umlaut.citation.coverage_summary.open_end") content_tag("span", :class=>"coverage_summary") do "#{start} – #{finish}:" end end |
#display_not_found_warning?(uml_request) ⇒ Boolean
85 86 87 88 89 |
# File 'app/helpers/resolve_helper.rb', line 85 def display_not_found_warning?(uml_request) = uml_request.referent. display_manually_entered_typo_warning = umlaut_config.lookup!("entry_not_in_kb_warning", false) return (['genre'] != 'book' && ['object_id'].blank? && user_entered_citation?(@user_request) && display_manually_entered_typo_warning) end |
#expand_contract_section(arg_heading, id, options = {}, &block) ⇒ Object
Generate content in an expand-contract block, with a heading that you can click on to show/hide it. Actual content in block. Example, in view:
<% expand_contract_section("My Content", "div_id_to_use") do %>
this will be hidden and shown
<% end %>
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'app/helpers/resolve_helper.rb', line 97 def (arg_heading, id, ={}, &block) = (params["umlaut.show_#{id}"] == "true") || [:initial_expand] || false icon = content_tag(:i, nil, :class => [] << ( ? "umlaut_icons-list-open" : "umlaut_icons-list-closed")) heading = content_tag(:span,( ? "Hide " : "Show "), :class=>'expand_contract_action_label') + arg_heading body_class = ( ? "in" : "") link_params = params.merge('umlaut.request_id' => @user_request.id, "umlaut.show_#{id}" => (! ).to_s, # Need to zero out format-related params for when we're coming # from a partial html api request, so the link we generate # is not to format json/xml/etc. :format => nil, 'umlaut.response_format' => nil, 'umlaut.jsonp'=>nil, # In Rails3, an :anchor param will actually be used for #fragmentIdentifier # on end of url :anchor => "#{id}_toggle_link" ) # Make sure a self-referencing link from partial_html_sections # really goes to full HTML view. link_params[:action] = "index" if link_params[:action] == "partial_html_sections" return content_tag(:div, :class => "collapsible", :id => "collapse_#{id}") do link_to(icon + " " + heading, link_params, :class => "collapse-toggle", "data-target" => "##{id}", "data-toggle" => "collapse") + content_tag(:div, :id => id, :class => ["collapse"]<< body_class, &block) end end |
#html_section_by_div_id(div_id) ⇒ Object
244 245 246 247 248 |
# File 'app/helpers/resolve_helper.rb', line 244 def html_section_by_div_id(div_id) config_resolve_sections.find do |defn| defn[:div_id] == div_id end end |
#html_sections(area) ⇒ Object
Called by resolve/index view to find sections configured in application config resolve_sections list for a specific part of the page. :main, :sidebar, or :resource_info.
221 222 223 224 225 |
# File 'app/helpers/resolve_helper.rb', line 221 def html_sections(area) config_resolve_sections.find_all do |section| section[:html_area] == area end end |
#item_icon(section_id) ⇒ Object
250 251 252 253 |
# File 'app/helpers/resolve_helper.rb', line 250 def item_icon(section_id) sections_with_icons = ["fulltext", "audio", "excerpts"] content_tag(:i, nil) if sections_with_icons.include? section_id end |
#list_with_limit(id, list, options = {}, &block) ⇒ Object
Code-generating helper to add a “More” link to a list, with a maximum number of items to show before ‘more’. AJAXy show, with unobtrusive degredation when no javascript. Based on the idea here for a helper that takes a block. Uses expand_contract_section for actual hidden overflow. Will split list into two different <ul>‘s, one before the cut and one after. Will generate <ul> tags itself. blog.zmok.net/articles/2008/04/22/block-level-helpers-in-ruby-on-rails
id: id to use for HTML div for hidden part of list. Other ids
will be based on this id too.
list: your list limit: how many lines to show before cut off. Default 5. Note that
at least two items will always be included in 'more'. If cutoff
is 5 and your list is 5, all 5 will be shown. If cut-off is 5
and list is 6, 4 items will be shown, with more. This is five
total lines if the 'more' is considered a line.
block: will be passed |item, index|, should generate html for that
item in block.
Example, in a view: <% list_with_limit(“div_id_for_list”, list, :limit=>10) do |item, index| %>
<li>Item Number: <%= index %>: <%= item.title %></li>
<% end %>
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'app/helpers/resolve_helper.rb', line 157 def list_with_limit(id, list, = {}, &block) # backwards compatible to when third argument was just a number # for limit. = {:limit => } unless .kind_of?(Hash) [:limit] ||= 5 [:ul_class] ||= "list-unstyled" return "" if list.empty? visible_list = (list.length > [:limit]) ? list.slice(0, [:limit]-1) : list hidden_list = (list.length > [:limit]) ? list.slice(([:limit]-1)..list.length-1) : [] parts =[] parts << content_tag(:ul, :class => [:ul_class]) do safe_join( visible_list.enum_for(:each_with_index).collect do |item, index| yield(item, index) end, " \n " ) end if (hidden_list.present?) parts << ("#{hidden_list.length} more", id) do content_tag(:ul, :class=>[:ul_class]) do safe_join( hidden_list.enum_for(:each_with_index).collect do |item, index| yield(item, index + [:limit] - 1) end, " \n " ) end end end return safe_join(parts, "\n") end |
#partial_html_sections ⇒ Object
Called by partial_html_sections action to get a list of all sections that should be included in the partial_html_sections api response.
209 210 211 212 213 214 215 216 |
# File 'app/helpers/resolve_helper.rb', line 209 def partial_html_sections unless (@@partial_update_sections) @@partial_update_sections = config_resolve_sections.find_all do |section| section[:partial_html_api] != false end end @@partial_update_sections end |
#render_section(arguments = {}) ⇒ Object
Will render an Umlaut HTML section. See SectionRenderer. Argument can be:
-
An already initialized SectionRenderer
-
:id => X, will load Section description hash from the resolve_sections configuration, finding description hash with :div_id == X.
-
A complete section description hash. Ordinarily only used when that complete hash was previously looked up from resolve_sections config.
For documentation of possible values in the section descripton hash, see SectionRenderer.
28 29 30 31 32 |
# File 'app/helpers/resolve_helper.rb', line 28 def render_section(arguments = {}) presenter = (arguments.kind_of?( SectionRenderer )) ? arguments : SectionRenderer.new(@user_request, arguments ) render(:partial => "section_display", :locals => {:presenter => presenter }) end |
#response_content(service_response) ⇒ Object
If response has a :content key returns it – and marks it html_safe if response has a :content_html_safe == true key. returns false if no :content.
126 127 128 129 130 131 |
# File 'app/helpers/resolve_helper.rb', line 126 def response_content(service_response) content = service_response[:content] return false unless content content = content.html_safe if service_response[:content_html_safe] == true return content end |
#section_css_classes(umlaut_request, section_id, unused = nil) ⇒ Object
Return an array of css classes that should be attached to an .umlaut_section generally ‘umlaut-section’, plus the section_id, plus possibly ‘umlaut-section-highlighted’. See #should_highlight_section?
pass in:
-
current Umlaut Request object
-
string section id
-
array of umlaut ServiceResponses already fetched for this section.
third paramter used to exist but was not used, deprecated but left there for now for backwards compat. TODO.
45 46 47 48 49 |
# File 'app/helpers/resolve_helper.rb', line 45 def section_css_classes(umlaut_request, section_id, unused = nil) classes = ["umlaut-section", section_id] classes << 'umlaut-section-highlighted' if section_highlights(umlaut_request).should_highlight_section?(section_id) return classes end |
#section_highlights(umlaut_request = @umlaut_request) ⇒ Object
Returns a section highlighter for the current umlaut request. (Cached for given @umlaut_request)
60 61 62 63 64 65 |
# File 'app/helpers/resolve_helper.rb', line 60 def section_highlights(umlaut_request = @umlaut_request) # Need to cache for efficiency, need to cache by request id since we take # it as an arg. @_section_highlighters ||= {} return @_section_highlighters[umlaut_request.id] ||= Umlaut::SectionHighlights.new(umlaut_request) end |
#should_highlight_section?(umlaut_request, section_id) ⇒ Boolean
Deprecated! Left for backwards compat. TODO.
53 54 55 56 |
# File 'app/helpers/resolve_helper.rb', line 53 def should_highlight_section?(umlaut_request, section_id) $stderr.warn "should_highlight_section? is deprecated, use `section_highlighter(umlaut_request).should_highlight_section?(section_id)`" highlighted_sections(umlaut_request).include? section_id.to_s end |
#user_entered_citation?(uml_request) ⇒ Boolean
Did this come from citation linker style entry? We check the referrer.
79 80 81 82 83 |
# File 'app/helpers/resolve_helper.rb', line 79 def user_entered_citation?(uml_request) return false unless uml_request && uml_request.referrer_id id = uml_request.referrer_id return id == 'info:sid/sfxit.com:citation' || id == umlaut_config.lookup!("rfr_ids.citation") || id == umlaut_config.lookup!('rfr_ids.opensearch') end |