Module: ResourcesHelper

Included in:
ScavengerHunt::ApplicationHelper
Defined in:
app/helpers/resources_helper.rb

Overview

View methods for displaying resources

Instance Method Summary collapse

Instance Method Details

#resource_content_uri(resource) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/helpers/resources_helper.rb', line 28

def resource_content_uri(resource)
  if resource.source_uri.present?
    resource.source_uri
  elsif resource.uploaded_resource.attached?
    # FIXME: Metamagic's, ahem, "magic" is hijacking `url_for` and causing
    # this to explode, so we call the helper method directly rather than
    # using the included one that has been overridden by, ahem, poorly
    # behaving libraries
    Rails.application.routes.url_helpers.url_for(resource.uploaded_resource)
  else
    nil
  end
end

#resource_group_listObject



9
10
11
12
13
14
15
16
17
# File 'app/helpers/resources_helper.rb', line 9

def resource_group_list
  if current_user.staff?
    current_organization.resource_groups
  else
    current_user.resource_groups
  end.by_default_and_name.map do |c|
    [c.title_with_default_annotation, c.id]
  end
end


19
20
21
22
23
24
25
26
# File 'app/helpers/resources_helper.rb', line 19

def resource_link(resource, options = {})
  link_options = options.delete(:link) || {}
  target = options.delete(:href) || resource_path(resource)

  link_to(target, link_options) do
    resource_link_target(resource, options.merge(alt: options[:alt] || "Image for resource ##{resource.id}"))
  end
end

Returns an HTML fragment that best depicts the resource (such as an image thumbnail, or an audio icon) based on the type of resource

Parameters:

  • target_resource (Resource)

    the Resource that is being displayed

  • representation_dom_id (String)

    identifies the DOM element which contains a description of the resource

  • options (Hash) (defaults to: {})

    passed on to to the helper code that builds a link (such as Rails' image_tag method)

Returns:

  • (String)

    an HTML fragment that best depicts the resource (such as an image thumbnail, or an audio icon) based on the type of resource



46
47
48
49
50
51
52
# File 'app/helpers/resources_helper.rb', line 46

def resource_link_target(resource, options = {})
  if resource.viewable?
    image_tag(resource_content_uri(resource), options)
  else
    "#{resource.title} (#{resource.resource_type})"
  end
end

#resource_status_list(resource, id: nil, title_tag: :h2) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/helpers/resources_helper.rb', line 54

def resource_status_list(resource, id: nil, title_tag: :h2)
  return unless resource.statuses.any?

  id ||= "resource-#{resource.id}"
  tags = []

  if resource.unrepresented?
    tags.push(tag_for('Undescribed', type: :neutral, hint: 'Status'))
  elsif resource.partially_complete?
    tags.push(tag_for('Partially Completed', type: :partial, hint: 'Status'))
  elsif resource.approved?
    tags.push(tag_for('Approved', type: :success, hint: 'Status'))
  end

  resource.meta.each do |metum|
    tags.push(metum_tag(metum, tag: :li))
  end

  tags.push(tag_for('Urgent', type: :error)) if resource.priority_flag?

  if resource.ordinality.present?
    tags.push(tag_for(resource.ordinality.to_s))
  end

  (
    (title_tag, class: 'sr-only', id: "tag-list-#{id}") { "Properties for resource ##{resource.id}" } +
      (:ul, aria: { labelledby: "tag-list-#{id}" }, class: 'tag-list') { tags.join.html_safe }
  ).html_safe
end

#scope_search_collectionObject



3
4
5
6
7
# File 'app/helpers/resources_helper.rb', line 3

def scope_search_collection
  Resource.ransackable_scopes.map do |scope_name|
    [scope_name.to_s.titleize.split(/\s+/).join(' & ').html_safe, scope_name]
  end
end