Sha256: 4dabeadfe91d6a90d5f13754f797aacc1c8edbea600223d83caf6ef719ab5113

Contents?: true

Size: 1.08 KB

Versions: 25

Compression:

Stored size: 1.08 KB

Contents

module Blacklight
  # Create <link rel="alternate"> links from a documents dynamically
  class LinkAlternatePresenter
    include ActionView::Helpers::OutputSafetyHelper
    include ActionView::Helpers::TagHelper

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

    attr_reader :view_context, :document, :options

    # Renders links to alternate representations 
    # provided by export formats. Returns empty string if no links available.
    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

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

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
blacklight-6.25.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-6.24.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-6.23.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-6.22.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-6.21.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-6.20.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-6.19.2 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-6.19.1 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-6.19.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-6.18.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-6.17.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-6.16.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-6.15.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-6.14.1 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-6.14.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-6.13.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-6.12.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-6.11.2 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-6.11.1 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-6.11.0 app/presenters/blacklight/link_alternate_presenter.rb