Class: Hyrax::IiifManifestPresenter

Inherits:
Draper::Decorator
  • Object
show all
Defined in:
app/presenters/hyrax/iiif_manifest_presenter.rb

Overview

This presenter wraps objects in the interface required by IIIFManifiest. It will accept either a Work-like resource or a SolrDocument.

Examples:

with a work


monograph = Monograph.new
presenter = IiifManifestPresenter.new(monograph)
presenter.title # => []

monograph.title = ['Comet in Moominland']
presenter.title # => ['Comet in Moominland']

See Also:

Defined Under Namespace

Classes: DisplayImagePresenter, Factory, NullAbility

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ability#can?

Returns:

  • (#can?)


40
41
42
# File 'app/presenters/hyrax/iiif_manifest_presenter.rb', line 40

def ability
  @ability ||= NullAbility.new
end

#hostnameString

Returns:

  • (String)


52
53
54
# File 'app/presenters/hyrax/iiif_manifest_presenter.rb', line 52

def hostname
  @hostname || 'localhost'
end

Class Method Details

.for(model) ⇒ Object

Parameters:



31
32
33
34
35
# File 'app/presenters/hyrax/iiif_manifest_presenter.rb', line 31

def for(model)
  klass = model.file_set? ? DisplayImagePresenter : IiifManifestPresenter

  klass.new(model)
end

Instance Method Details

#descriptionString

Returns:

  • (String)


46
47
48
# File 'app/presenters/hyrax/iiif_manifest_presenter.rb', line 46

def description
  Array(super).first || ''
end

#file_set?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'app/presenters/hyrax/iiif_manifest_presenter.rb', line 58

def file_set?
  model.try(:file_set?) || Array(model[:has_model_ssim]).include?('FileSet')
end

#file_set_presentersArray<DisplayImagePresenter>

Returns:



64
65
66
# File 'app/presenters/hyrax/iiif_manifest_presenter.rb', line 64

def file_set_presenters
  member_presenters.select(&:file_set?)
end

#manifest_metadataArray<Hash{String => String}>

TODO:

should this use the simple_form i18n keys?! maybe the manifest needs its own?

IIIF metadata for inclusion in the manifest

Called by the `iiif_manifest` gem to add metadata

Returns:

  • (Array<Hash{String => String}>)

    array of metadata hashes



76
77
78
79
80
81
82
83
# File 'app/presenters/hyrax/iiif_manifest_presenter.rb', line 76

def 
  .map do |field_name|
    {
      'label' => I18n.t("simple_form.labels.defaults.#{field_name}"),
      'value' => Array(send(field_name)).map { |value| scrub(value.to_s) }
    }
  end
end

#manifest_urlString

Returns the URL where the manifest can be found.

Returns:

  • (String)

    the URL where the manifest can be found



87
88
89
90
91
# File 'app/presenters/hyrax/iiif_manifest_presenter.rb', line 87

def manifest_url
  return '' if id.blank?

  Rails.application.routes.url_helpers.polymorphic_url([:manifest, model], host: hostname)
end

#member_idsArray<#to_s>

Returns:

  • (Array<#to_s>)


95
96
97
98
99
100
101
102
# File 'app/presenters/hyrax/iiif_manifest_presenter.rb', line 95

def member_ids
  case model
  when Valkyrie::Resource
    Array(model.try(:member_ids))
  else
    Hyrax::SolrDocument::OrderedMembers.decorate(model).ordered_member_ids
  end
end

#member_presentersArray<IiifManifestPresenter>

Note:

cache member presenters to avoid querying repeatedly; we expect this presenter to live only as long as the request.

Note:

skips presenters for objects the current ‘@ability` cannot read. the default ability has all permissions.

Returns:



112
113
114
115
116
117
118
119
120
# File 'app/presenters/hyrax/iiif_manifest_presenter.rb', line 112

def member_presenters
  @member_presenters_cache ||= Factory.build_for(ids: member_ids, presenter_class: self.class).map do |presenter|
    next unless ability.can?(:read, presenter.model)

    presenter.hostname = hostname
    presenter.ability  = ability
    presenter
  end.compact
end

#sequence_renderingArray<Hash{String => String}>

Returns:

  • (Array<Hash{String => String}>)


124
125
126
127
128
129
130
131
132
133
# File 'app/presenters/hyrax/iiif_manifest_presenter.rb', line 124

def sequence_rendering
  Array(try(:rendering_ids)).map do |file_set_id|
    rendering = file_set_presenters.find { |p| p.id == file_set_id }
    next unless rendering

    { '@id' => Hyrax::Engine.routes.url_helpers.download_url(rendering.id, host: hostname),
      'format' => rendering.mime_type.presence || I18n.t("hyrax.manifest.unknown_mime_text"),
      'label' => I18n.t("hyrax.manifest.download_text") + (rendering.label || '') }
  end.flatten
end

#versionString

Note:

ideally, this value will be cheap to retrieve, and will reliably change any time the manifest JSON will change. the current implementation is more blunt than this, changing only when the work itself changes.

Returns a string tag suitable for cache keys for this manifiest.

Returns:

  • (String)

    a string tag suitable for cache keys for this manifiest



153
154
155
# File 'app/presenters/hyrax/iiif_manifest_presenter.rb', line 153

def version
  object.try(:modified_date)&.to_s || ''
end

#work?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'app/presenters/hyrax/iiif_manifest_presenter.rb', line 137

def work?
  object.try(:work?) || !file_set?
end

#work_presentersArray<IiifManifestPresenter>

Returns:



143
144
145
# File 'app/presenters/hyrax/iiif_manifest_presenter.rb', line 143

def work_presenters
  member_presenters.select(&:work?)
end