Sha256: 374eb08f1ad24a5286612e37c4136649ecb2968e9b9d3720be5dac6ad58261a9

Contents?: true

Size: 1.11 KB

Versions: 96

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

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

96 entries across 95 versions & 2 rubygems

Version Path
blacklight-8.7.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-8.6.1 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-7.40.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-8.6.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-8.5.1 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-8.5.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-7.39.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-8.4.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-7.38.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/blacklight-8.3.0/app/presenters/blacklight/link_alternate_presenter.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/blacklight-7.37.0/app/presenters/blacklight/link_alternate_presenter.rb
blacklight-8.3.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-8.2.2 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-8.2.1 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-8.2.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-7.37.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-7.36.2 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-7.36.1 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-7.36.0 app/presenters/blacklight/link_alternate_presenter.rb
blacklight-7.35.0 app/presenters/blacklight/link_alternate_presenter.rb