Class: Hyrax::WorkShowPresenter

Inherits:
Object
  • Object
show all
Includes:
ModelProxy, PresentsAttributes
Defined in:
app/presenters/hyrax/work_show_presenter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PresentsAttributes

#attribute_to_html, #display_microdata?, #microdata_type_to_html, #permission_badge, #permission_badge_class

Methods included from ModelProxy

#persisted?, #to_model

Constructor Details

#initialize(solr_document, current_ability, request = nil) ⇒ WorkShowPresenter

Returns a new instance of WorkShowPresenter.

Parameters:

  • solr_document (SolrDocument)
  • current_ability (Ability)
  • request (ActionDispatch::Request) (defaults to: nil)

    the http request context. Used so the GraphExporter knows what URLs to draw.



23
24
25
26
27
28
# File 'app/presenters/hyrax/work_show_presenter.rb', line 23

def initialize(solr_document, current_ability, request = nil)
  @solr_document = Hyrax::SolrDocument::OrderedMembers.decorate(solr_document)
  @current_ability = current_ability
  @request = request
  define_dynamic_methods if @solr_document.flexible?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (private)



282
283
284
285
# File 'app/presenters/hyrax/work_show_presenter.rb', line 282

def method_missing(method_name, *args, &block)
  return solr_document.public_send(method_name, *args, &block) if solr_document.respond_to?(method_name)
  super
end

Instance Attribute Details

#current_abilityObject

Returns the value of attribute current_ability.



11
12
13
# File 'app/presenters/hyrax/work_show_presenter.rb', line 11

def current_ability
  @current_ability
end

#member_presenter_factory=(value) ⇒ Object

Sets the attribute member_presenter_factory

Parameters:

  • value

    the value to set the attribute member_presenter_factory to.



10
11
12
# File 'app/presenters/hyrax/work_show_presenter.rb', line 10

def member_presenter_factory=(value)
  @member_presenter_factory = value
end

#requestObject

Returns the value of attribute request.



11
12
13
# File 'app/presenters/hyrax/work_show_presenter.rb', line 11

def request
  @request
end

#solr_documentObject

Returns the value of attribute solr_document.



11
12
13
# File 'app/presenters/hyrax/work_show_presenter.rb', line 11

def solr_document
  @solr_document
end

Instance Method Details

#date_modifiedObject



135
136
137
# File 'app/presenters/hyrax/work_show_presenter.rb', line 135

def date_modified
  solr_document.date_modified.try(:to_formatted_s, :standard)
end

#date_uploadedObject



139
140
141
# File 'app/presenters/hyrax/work_show_presenter.rb', line 139

def date_uploaded
  solr_document.date_uploaded.try(:to_formatted_s, :standard)
end

#define_dynamic_methodsObject

rubocop:disable Metrics/MethodLength



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/presenters/hyrax/work_show_presenter.rb', line 30

def define_dynamic_methods # rubocop:disable Metrics/MethodLength
  Hyrax::FlexibleSchema.default_properties.each do |prop|
    method_name = prop.to_s
    property_details = Hyrax::FlexibleSchema.current_version["properties"][method_name]
    next unless property_details

    index_keys = property_details["indexing"]
    next unless index_keys

    multi_value = property_details['multiple'] || (property_details['data_type'] == 'array')

    next if self.class.method_defined?(method_name) && solr_document.respond_to?(method_name)
    # Define the method on the SolrDocument class
    Hyrax::SolrDocument::OrderedMembers.send(:define_method, method_name) do
      index_keys.each do |index_key|
        value = self[index_key]
        return value if value.present?
      end
      multi_value ? [] : ""
    end

    # Define the method on the Presenter class
    self.class.send(:define_method, method_name) do
      @solr_document.send(method_name)
    end
  end
end

#display_feature_link?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'app/presenters/hyrax/work_show_presenter.rb', line 190

def display_feature_link?
  work_featurable? && FeaturedWork.can_create_another? && !featured?
end

#display_unfeature_link?Boolean

Returns:

  • (Boolean)


194
195
196
# File 'app/presenters/hyrax/work_show_presenter.rb', line 194

def display_unfeature_link?
  work_featurable? && featured?
end

#download_urlString

Returns a download URL, if work has representative media, or a blank string.

Returns:

  • (String)

    a download URL, if work has representative media, or a blank string



79
80
81
82
# File 'app/presenters/hyrax/work_show_presenter.rb', line 79

def download_url
  return '' if representative_presenter.nil?
  Hyrax::Engine.routes.url_helpers.download_url(representative_presenter, host: request.host)
end

#editor?Boolean

Deprecated.

use ‘::Ability.can?(:edit, presenter)`. Hyrax views calling presenter #editor methods will continue to call them until Hyrax 4.0.0. The deprecation time horizon for the presenter methods themselves is 5.0.0.

Returns:

  • (Boolean)


164
165
166
# File 'app/presenters/hyrax/work_show_presenter.rb', line 164

def editor?
  current_ability.can?(:edit, self)
end

#export_as_jsonldObject



151
152
153
# File 'app/presenters/hyrax/work_show_presenter.rb', line 151

def export_as_jsonld
  graph.dump(:jsonld, standard_prefixes: true)
end

#export_as_ntObject



147
148
149
# File 'app/presenters/hyrax/work_show_presenter.rb', line 147

def export_as_nt
  graph.dump(:ntriples)
end

#export_as_ttlObject



155
156
157
# File 'app/presenters/hyrax/work_show_presenter.rb', line 155

def export_as_ttl
  graph.dump(:ttl)
end

#grouped_presenters(filtered_by: nil, except: nil) ⇒ Array

Returns presenters grouped by model name, used to show the parents of this object.

Returns:

  • (Array)

    presenters grouped by model name, used to show the parents of this object



177
178
179
180
181
182
183
184
# File 'app/presenters/hyrax/work_show_presenter.rb', line 177

def grouped_presenters(filtered_by: nil, except: nil)
  # TODO: we probably need to retain collection_presenters (as parent_presenters)
  #       and join this with member_of_collection_presenters
  grouped = member_of_collection_presenters.group_by(&:model_name).transform_keys(&:human)
  grouped.select! { |obj| obj.casecmp(filtered_by).zero? } unless filtered_by.nil?
  grouped.reject! { |obj| except.map(&:downcase).include? obj.downcase } unless except.nil?
  grouped
end

#iiif_viewerSymbol

Returns the name of the IIIF viewer partial to render.

Examples:

A work presenter with a custom iiif viewer

module Hyrax
  class GenericWorkPresenter < Hyrax::WorkShowPresenter
    def iiif_viewer
      :my_iiif_viewer
    end
  end
end

Custom iiif viewer partial at app/views/hyrax/base/iiif_viewers/_my_iiif_viewer.html.erb
<h3>My IIIF Viewer!</h3>
<a href=<%= main_app.polymorphic_url([main_app, :manifest, presenter], { locale: nil }) %>>Manifest</a>

Returns:

  • (Symbol)

    the name of the IIIF viewer partial to render



109
110
111
# File 'app/presenters/hyrax/work_show_presenter.rb', line 109

def iiif_viewer
  :universal_viewer
end

#iiif_viewer?Boolean Also known as: universal_viewer?

Returns render a IIIF viewer.

Returns:

  • (Boolean)

    render a IIIF viewer



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

def iiif_viewer?
  Hyrax.config.iiif_image_server? &&
    representative_id.present? &&
    representative_presenter.present? &&
    representative_presenter.image? &&
    members_include_viewable_image?
end

#inspect_workObject



74
75
76
# File 'app/presenters/hyrax/work_show_presenter.rb', line 74

def inspect_work
  @inspect_workflow ||= InspectWorkPresenter.new(solr_document, current_ability)
end


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

def link_name
  current_ability.can?(:read, id) ? to_s : 'Private'
end

#list_of_item_ids_to_displayArray

Returns list to display with Kaminari pagination.

Returns:

  • (Array)

    list to display with Kaminari pagination



209
210
211
# File 'app/presenters/hyrax/work_show_presenter.rb', line 209

def list_of_item_ids_to_display
  paginated_item_list(page_array: authorized_item_ids)
end

#manifest_metadataArray

IIIF metadata for inclusion in the manifest

Called by the `iiif_manifest` gem to add 

Returns:

  • (Array)

    array of metadata hashes



236
237
238
239
240
241
242
243
# File 'app/presenters/hyrax/work_show_presenter.rb', line 236

def 
  Hyrax.config..each_with_object([]) do |field, |
     << {
      'label' => I18n.t("simple_form.labels.defaults.#{field}"),
      'value' => Array.wrap(send(field).map { |f| Loofah.fragment(f.to_s).scrub!(:whitewash).to_s })
    }
  end
end

#manifest_urlObject



218
219
220
# File 'app/presenters/hyrax/work_show_presenter.rb', line 218

def manifest_url
  manifest_helper.polymorphic_url([:manifest, self])
end

#member_countInteger

Returns:

  • (Integer)


247
248
249
# File 'app/presenters/hyrax/work_show_presenter.rb', line 247

def member_count
  @member_count ||= member_presenters.count
end

#member_of_collection_presentersArray<CollectionPresenter>

Get presenters for the collections this work is a member of via the member_of_collections association.

Returns:



129
130
131
132
133
# File 'app/presenters/hyrax/work_show_presenter.rb', line 129

def member_of_collection_presenters
  PresenterFactory.build_for(ids: member_of_authorized_parent_collections,
                             presenter_class: collection_presenter_class,
                             presenter_args: presenter_factory_arguments)
end

#modelObject



202
203
204
# File 'app/presenters/hyrax/work_show_presenter.rb', line 202

def model
  solr_document.to_model
end

#page_titleObject



66
67
68
# File 'app/presenters/hyrax/work_show_presenter.rb', line 66

def page_title
  "#{human_readable_type} | #{title.first} | ID: #{id} | #{I18n.t('hyrax.product_name')}"
end

#presenter_typesObject



172
173
174
# File 'app/presenters/hyrax/work_show_presenter.rb', line 172

def presenter_types
  Hyrax.config.registered_curation_concern_types.map(&:underscore) + ["collection"]
end

#representative_presenterObject

Returns FileSetPresenter presenter for the representative FileSets.

Returns:

  • FileSetPresenter presenter for the representative FileSets



114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/presenters/hyrax/work_show_presenter.rb', line 114

def representative_presenter
  return nil if representative_id.blank?
  @representative_presenter ||=
    begin
      result = member_presenters([representative_id]).first
      return nil if result.try(:id) == id
      result.try(:representative_presenter) || result
    rescue Hyrax::ObjectNotFoundError
      Hyrax.logger.warn "Unable to find representative_id #{representative_id} for work #{id}"
      return nil
    end
end

#schema_versionObject



62
63
64
# File 'app/presenters/hyrax/work_show_presenter.rb', line 62

def schema_version
  solr_document[:schema_version_ssi]
end

#sequence_renderingArray

IIIF rendering linking property for inclusion in the manifest

Called by the `iiif_manifest` gem to add a 'rendering' (eg. a link a download for the resource)

Returns:

  • (Array)

    array of rendering hashes



226
227
228
229
230
# File 'app/presenters/hyrax/work_show_presenter.rb', line 226

def sequence_rendering
  solr_document.rendering_ids.each_with_object([]) do |file_set_id, renderings|
    renderings << manifest_helper.build_rendering(file_set_id)
  end.flatten
end

#show_deposit_for?(collections:) ⇒ Boolean

Given a set of collections, which the caller asserts the current ability can deposit to, decide whether to display actions to add this work to a collection.

By default, this returns ‘true` if any collections are passed in OR the current ability can create a collection.

Parameters:

  • collections (Enumerable<::Collection>, nil)

    list of collections to which the current ability can deposit

Returns:

  • (Boolean)

    a flag indicating whether to display collection deposit options.



264
265
266
267
# File 'app/presenters/hyrax/work_show_presenter.rb', line 264

def show_deposit_for?(collections:)
  collections.present? ||
    current_ability.can?(:create_any, Hyrax.config.collection_class)
end

#stats_pathObject



198
199
200
# File 'app/presenters/hyrax/work_show_presenter.rb', line 198

def stats_path
  Hyrax::Engine.routes.url_helpers.stats_work_path(self, locale: I18n.locale)
end

#total_pagesInteger

Returns total number of pages of viewable items.

Returns:

  • (Integer)

    total number of pages of viewable items



214
215
216
# File 'app/presenters/hyrax/work_show_presenter.rb', line 214

def total_pages
  (total_items.to_f / rows_from_params.to_f).ceil
end

#tweeterObject



168
169
170
# File 'app/presenters/hyrax/work_show_presenter.rb', line 168

def tweeter
  TwitterPresenter.twitter_handle_for(user_key: depositor)
end

#valid_child_concernsArray<Class>

Returns:

  • (Array<Class>)


271
272
273
# File 'app/presenters/hyrax/work_show_presenter.rb', line 271

def valid_child_concerns
  Hyrax::ChildTypes.for(parent: solr_document.hydra_model).to_a
end

#valkyrie_presenter?Boolean

Returns:

  • (Boolean)


276
277
278
# File 'app/presenters/hyrax/work_show_presenter.rb', line 276

def valkyrie_presenter?
  solr_document.hydra_model < Valkyrie::Resource
end

#work_featurable?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'app/presenters/hyrax/work_show_presenter.rb', line 186

def work_featurable?
  user_can_feature_works? && solr_document.public?
end

#workflowObject



70
71
72
# File 'app/presenters/hyrax/work_show_presenter.rb', line 70

def workflow
  @workflow ||= WorkflowPresenter.new(solr_document, current_ability)
end