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.



9
10
11
12
13
# File 'app/presenters/blacklight/link_alternate_presenter.rb', line 9

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.



15
16
17
# File 'app/presenters/blacklight/link_alternate_presenter.rb', line 15

def document
  @document
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'app/presenters/blacklight/link_alternate_presenter.rb', line 15

def options
  @options
end

#view_contextObject (readonly)

Returns the value of attribute view_context.



15
16
17
# File 'app/presenters/blacklight/link_alternate_presenter.rb', line 15

def view_context
  @view_context
end

Instance Method Details

#href(format) ⇒ Object



31
32
33
# File 'app/presenters/blacklight/link_alternate_presenter.rb', line 31

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.



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

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