Module: ArclightHelper

Defined in:
app/helpers/arclight_helper.rb

Overview

Generic Helpers used in Arclight

Instance Method Summary collapse

Instance Method Details

#collection_active?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'app/helpers/arclight_helper.rb', line 33

def collection_active?
  try(:search_state) && search_state.params_for_search.try(:[], 'f').try(:[], 'level_sim') == ['Collection']
end

#collection_active_classObject



37
38
39
# File 'app/helpers/arclight_helper.rb', line 37

def collection_active_class
  'active' if collection_active?
end

#collection_countObject



41
42
43
# File 'app/helpers/arclight_helper.rb', line 41

def collection_count
  facets_from_request.find { |f| f.name == 'collection_sim' }.try(:items).try(:count)
end

#collection_downloads(document, config = load_download_config) ⇒ Hash

Looks for ‘document.unitid` in the downloads configuration

Parameters:

  • `document` (SolrDocument)
  • `config` (Hash)

    metadata for downloadable files

Returns:

  • (Hash)

    with ‘:href` and `:size` keys



50
51
52
53
54
# File 'app/helpers/arclight_helper.rb', line 50

def collection_downloads(document, config = load_download_config)
  config = config[document.unitid] if config.present?
  return {} if config.blank?
  parse_collection_downloads(config)
end

#config_fieldObject

Defines custom helpers used for creating unique metadata blocks to render



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/helpers/arclight_helper.rb', line 118

Arclight::Engine.config.catalog_controller_field_accessors.each do |config_field|
  ##
  # Mimics what document_show_fields from Blacklight does
  # https://github.com/projectblacklight/blacklight/blob/dee8d794125306ec8d4ab834a6a45bcf9671c791/app/helpers/blacklight/configuration_helper_behavior.rb#L35-L38
  define_method(:"document_#{config_field}s") do |_document = nil|
    blacklight_config.send(:"#{config_field}s")
  end

  ##
  # Mimics what render_document_show_field_label from Blacklight does
  # https://github.com/projectblacklight/blacklight/blob/dee8d794125306ec8d4ab834a6a45bcf9671c791/app/helpers/blacklight/blacklight_helper_behavior.rb#L136-L156
  define_method(:"render_document_#{config_field}_label") do |*args|
    options = args.extract_options!
    document = args.first

    field = options[:field]

    t(:'blacklight.search.show.label', label: send(:"document_#{config_field}_label", document, field))
  end

  ##
  # Mimics what document_show_field_label from Blacklight does
  # https://github.com/projectblacklight/blacklight/blob/dee8d794125306ec8d4ab834a6a45bcf9671c791/app/helpers/blacklight/configuration_helper_behavior.rb#L67-L74
  define_method(:"document_#{config_field}_label") do |document, field|
    field_config = send(:"document_#{config_field}s", document)[field]
    field_config ||= Blacklight::Configuration::NullField.new(key: field)

    field_config.display_label('show')
  end

  ##
  # Mimics what should_render_show_field? from Blacklight does
  # https://github.com/projectblacklight/blacklight/blob/dee8d794125306ec8d4ab834a6a45bcf9671c791/app/helpers/blacklight/blacklight_helper_behavior.rb#L84-L92
  define_method(:"should_render_#{config_field}?") do |document, field_config|
    should_render_field?(field_config, document) && document_has_value?(document, field_config)
  end
end

#custom_show_content_classesObject

Classes used for customized show page in arclight



25
26
27
# File 'app/helpers/arclight_helper.rb', line 25

def custom_show_content_classes
  'col-md-12 show-document'
end

#display_size(size) ⇒ String

Show a human readable size, or if it’s already a string, show that

Returns:

  • (String)

    human readable siz



111
112
113
114
# File 'app/helpers/arclight_helper.rb', line 111

def display_size(size)
  size = number_to_human_size(size.to_i + 1) if size.is_a?(Numeric) || size =~ /^[0-9]+$/ # assumes bytes
  size.to_s
end

#fields_have_content?(document, field_accessor) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
# File 'app/helpers/arclight_helper.rb', line 69

def fields_have_content?(document, field_accessor)
  generic_document_fields(field_accessor).any? do |_, field|
    generic_should_render_field?(field_accessor, document, field)
  end
end

#generic_document_fields(config_field) ⇒ Object

Calls the method for a configured field



158
159
160
# File 'app/helpers/arclight_helper.rb', line 158

def generic_document_fields(config_field)
  send(:"document_#{config_field}s")
end

#generic_render_document_field_label(config_field, document, field: field_name) ⇒ Object

Calls the method for a configured field



170
171
172
# File 'app/helpers/arclight_helper.rb', line 170

def generic_render_document_field_label(config_field, document, field: field_name)
  send(:"render_document_#{config_field}_label", document, field: field)
end

#generic_should_render_field?(config_field, document, field) ⇒ Boolean

Calls the method for a configured field

Returns:

  • (Boolean)


164
165
166
# File 'app/helpers/arclight_helper.rb', line 164

def generic_should_render_field?(config_field, document, field)
  send(:"should_render_#{config_field}?", document, field)
end

#hierarchy_component_context?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'app/helpers/arclight_helper.rb', line 85

def hierarchy_component_context?
  params[:hierarchy_context] == 'component'
end

#load_download_config(filename = Rails.root.join('config', 'downloads.yml')) ⇒ Hash

Returns loaded from config/downloads.yml, or ‘{}` if missing file.

Returns:

  • (Hash)

    loaded from config/downloads.yml, or ‘{}` if missing file



90
91
92
93
94
# File 'app/helpers/arclight_helper.rb', line 90

def load_download_config(filename = Rails.root.join('config', 'downloads.yml'))
  YAML.safe_load(File.read(filename))
rescue Errno::ENOENT
  {}
end

#normalize_id(id) ⇒ Object



29
30
31
# File 'app/helpers/arclight_helper.rb', line 29

def normalize_id(id)
  Arclight::NormalizedId.new(id).to_s
end

#on_repositories_index?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'app/helpers/arclight_helper.rb', line 60

def on_repositories_index?
  controller_name == 'repositories' && action_name == 'index'
end

#on_repositories_show?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'app/helpers/arclight_helper.rb', line 56

def on_repositories_show?
  controller_name == 'repositories' && action_name == 'show'
end

Parameters:

  • (SolrDocument)


8
9
10
11
12
# File 'app/helpers/arclight_helper.rb', line 8

def parents_to_links(document)
  safe_join(Arclight::Parents.from_solr_document(document).as_parents.map do |parent|
    link_to parent.label, solr_document_path(parent.global_id)
  end, t('arclight.breadcrumb_separator'))
end

#parse_collection_downloads(config, results = {}) ⇒ Hash

Returns the downloads for the given configuration using Hash symbols.

Examples:

‘{ pdf: { href: ’http://…‘, size: ’123 KB’ } }‘

Returns:

  • (Hash)

    the downloads for the given configuration using Hash symbols



98
99
100
101
102
103
104
105
106
107
# File 'app/helpers/arclight_helper.rb', line 98

def parse_collection_downloads(config, results = {})
  %w[pdf ead].each do |type|
    next if config[type].blank?
    results[type.to_sym] = {
      href: config[type]['href'],
      size: display_size(config[type]['size'])
    }
  end
  results
end

#repositories_active_classObject

the Repositories menu item is only active on the Repositories index page



65
66
67
# File 'app/helpers/arclight_helper.rb', line 65

def repositories_active_class
  'active' if on_repositories_index?
end

#repository_collections_path(repository) ⇒ Object



14
15
16
17
18
19
20
21
# File 'app/helpers/arclight_helper.rb', line 14

def repository_collections_path(repository)
  search_action_url(
    f: {
      repository_sim: [repository.name],
      level_sim: ['Collection']
    }
  )
end

#repository_faceted_onRepository

If we have a facet on the repository, then return the Repository object for it

Returns:

  • (Repository)


78
79
80
81
82
83
# File 'app/helpers/arclight_helper.rb', line 78

def repository_faceted_on
  return unless try(:search_state)
  repos = facets_from_request.find { |f| f.name == 'repository_sim' }.try(:items)
  faceted = repos && repos.length == 1 && repos.first.value
  Arclight::Repository.find_by(name: repos.first.value) if faceted
end