Class: Blacklight::LinkAlternatePresenter

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::OutputSafetyHelper, ActionView::Helpers::TagHelper
Defined in:
app/presenters/blacklight/link_alternate_presenter.rb

Overview

Create <link rel=“alternate”> links from a documents dynamically

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view_context, document, options) ⇒ LinkAlternatePresenter

Returns a new instance of LinkAlternatePresenter.



7
8
9
10
11
# File 'app/presenters/blacklight/link_alternate_presenter.rb', line 7

def initialize(view_context, document, options)
  @view_context = view_context
  @document = document
  @options = { unique: false, exclude: [] }.merge(options)
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



13
14
15
# File 'app/presenters/blacklight/link_alternate_presenter.rb', line 13

def document
  @document
end

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'app/presenters/blacklight/link_alternate_presenter.rb', line 13

def options
  @options
end

#view_contextObject (readonly)

Returns the value of attribute view_context.



13
14
15
# File 'app/presenters/blacklight/link_alternate_presenter.rb', line 13

def view_context
  @view_context
end

Instance Method Details

#href(format) ⇒ Object



29
30
31
# File 'app/presenters/blacklight/link_alternate_presenter.rb', line 29

def href(format)
  view_context.polymorphic_url(view_context.search_state.url_for_document(document), format: format)
end

#renderObject

Renders links to alternate representations provided by export formats. Returns empty string if no links available.



17
18
19
20
21
22
23
24
25
26
27
# File 'app/presenters/blacklight/link_alternate_presenter.rb', line 17

def render
  seen = Set.new

  safe_join(document.export_formats.map do |format, spec|
    next if options[:exclude].include?(format) || (options[:unique] && seen.include?(spec[:content_type]))

    seen.add(spec[:content_type])

    tag(:link, rel: "alternate", title: format, type: spec[:content_type], href: href(format))
  end.compact, "\n")
end