Class: Hyrax::QaSelectService

Inherits:
Object
  • Object
show all
Defined in:
app/services/hyrax/qa_select_service.rb

Overview

This is an abstract class to provide select options from a questioning authority backed authority

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(authority_name) ⇒ QaSelectService

Returns a new instance of QaSelectService.



8
9
10
# File 'app/services/hyrax/qa_select_service.rb', line 8

def initialize(authority_name)
  @authority = Qa::Authorities::Local.subauthority_for(authority_name)
end

Instance Attribute Details

#authorityObject (readonly)

Returns the value of attribute authority.



6
7
8
# File 'app/services/hyrax/qa_select_service.rb', line 6

def authority
  @authority
end

Instance Method Details

#active?(id) ⇒ Boolean

Returns whether the key is active.

Returns:

  • (Boolean)

    whether the key is active

Raises:

  • (KeyError)

    when the key has no ‘active:` status



29
30
31
# File 'app/services/hyrax/qa_select_service.rb', line 29

def active?(id)
  authority.find(id).fetch('active')
end

#active_elementsEnumerable<Hash>

Returns:

  • (Enumerable<Hash>)

Raises:

  • (KeyError)

    when no ‘term’ value is present for the id



50
51
52
# File 'app/services/hyrax/qa_select_service.rb', line 50

def active_elements
  authority.all.select { |e| e.fetch('active') }
end

#include_current_value(value, _index, render_options, html_options) ⇒ Object

TODO:

find a better home for this! This was initially inlined to the service from a helper module in github.com/samvera/curation_concerns/pull/986. It seems odd that this service knows about HTML rendering details. Maybe a factory is an appropriate next step?

Note:

this was extracted from LicenseService for more general use.

A helper for adding the current value to a form dropdown when



63
64
65
66
67
68
69
# File 'app/services/hyrax/qa_select_service.rb', line 63

def include_current_value(value, _index, render_options, html_options)
  unless value.blank? || active?(value)
    html_options[:class] += ' force-select'
    render_options += [[label(value) { value }, value]]
  end
  [render_options, html_options]
end

#label(id) { ... } ⇒ String

Returns the label for the authority.

Parameters:

  • id (String)

Yields:

  • when no ‘term’ value is present for the id

Yield Returns:

  • (String)

    an alternate label to return

Returns:

  • (String)

    the label for the authority

Raises:

  • (KeyError)

    when no ‘term’ value is present for the id



42
43
44
# File 'app/services/hyrax/qa_select_service.rb', line 42

def label(id, &block)
  authority.find(id).fetch('term', &block)
end

#select_active_optionsArray<String, #to_s>

Returns:

  • (Array<String, #to_s>)


21
22
23
# File 'app/services/hyrax/qa_select_service.rb', line 21

def select_active_options
  active_elements.map { |e| [e[:label], e[:id]] }
end

#select_all_optionsArray<String, #to_s>

Returns:

  • (Array<String, #to_s>)


14
15
16
17
18
# File 'app/services/hyrax/qa_select_service.rb', line 14

def select_all_options
  authority.all.map do |element|
    [element[:label], element[:id]]
  end
end