Module: Hyrax::AuthorityService
- Included in:
- ResourceTypesService
- Defined in:
- app/services/hyrax/authority_service.rb
Overview
This module is intended for use via extend on a host module. The
macros define singleton methods on the host and are not available via
include. Hosts that need instance-level tolerant lookup should
subclass QaSelectService instead.
Shared behavior for authority-backed services. Module-level services
(such as ResourceTypesService) extend this module to gain a
tolerant label / active? / include_current_value API plus the
declarative macros described below.
The tolerant methods are intentionally forgiving of off-authority values:
label falls back to the id itself, active? returns false for ids that
aren't present in the authority at all, and include_current_value
preserves such values in edit forms so they are not silently dropped on
save.
Declarative macros
Hosts use the #authority_name and #microdata_namespace macros to declare the wrapped subauthority and the i18n namespace used for schema.org type lookup. These macros generate the rest of the host's API for free:
module Hyrax
module DisciplineService
extend Hyrax::AuthorityService
'discipline'
microdata_namespace 'type.'
end
end
authority_name provides authority, authority=, select_options,
and select_all_options (the latter is an alias of the former — both
names are in use in the wild). microdata_namespace provides
microdata_type.
Instance Method Summary collapse
-
#active?(id) ⇒ Boolean
Whether the id is an active entry.
-
#authority_name(subauthority_name) ⇒ Object
Declares which Qa::Authorities::Local subauthority this host wraps, generating
authority,authority=,select_options, andselect_all_optionsaccessors on the host. -
#include_current_value(value, _index, render_options, html_options) ⇒ Object
Preserves an off-authority value as a forced-select option in a Simple Form input so it is not silently dropped on save.
-
#label(id) { ... } ⇒ String
The label for the authority entry, falling back to the id itself when no matching term is found.
-
#microdata_namespace(namespace) ⇒ Object
Declares the Microdata i18n namespace this host uses for schema.org type lookup, generating a
microdata_type(id)method on the host.
Instance Method Details
#active?(id) ⇒ Boolean
Returns whether the id is an active entry. Returns false for ids that aren't present in the authority at all, so a caller can treat unknown ids as inactive (and preserve them in edit forms via #include_current_value).
88 89 90 91 92 |
# File 'app/services/hyrax/authority_service.rb', line 88 def active?(id) result = .find(id) return false if result.blank? result.fetch('active', true) end |
#authority_name(subauthority_name) ⇒ Object
Declares which Qa::Authorities::Local subauthority this host wraps,
generating authority, authority=, select_options, and
select_all_options accessors on the host.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/services/hyrax/authority_service.rb', line 46 def () define_singleton_method(:authority) do @authority ||= Qa::Authorities::Local.() end define_singleton_method(:authority=) do |value| @authority = value end define_singleton_method(:select_all_options) do .all.map { |element| [element[:label], element[:id]] } end singleton_class.alias_method(:select_options, :select_all_options) end |
#include_current_value(value, _index, render_options, html_options) ⇒ Object
Preserves an off-authority value as a forced-select option in a Simple
Form input so it is not silently dropped on save. Intended for use as a
Simple Form item_helper: callback or to augment a collection: array
in a partial.
98 99 100 101 102 103 104 105 |
# File 'app/services/hyrax/authority_service.rb', line 98 def include_current_value(value, _index, , ) force_select = [:class].is_a?(Array) ? [' force-select'] : ' force-select' unless value.blank? || active?(value) [:class] += force_select += [[label(value), value]] end [, ] end |
#label(id) { ... } ⇒ String
Returns the label for the authority entry, falling back to the id itself when no matching term is found.
79 80 81 82 |
# File 'app/services/hyrax/authority_service.rb', line 79 def label(id, &block) block ||= ->(_key) { id } .find(id).fetch('term', &block) end |
#microdata_namespace(namespace) ⇒ Object
Declares the Microdata i18n namespace this host uses for schema.org
type lookup, generating a microdata_type(id) method on the host.
66 67 68 69 70 71 |
# File 'app/services/hyrax/authority_service.rb', line 66 def microdata_namespace(namespace) define_singleton_method(:microdata_type) do |id| return Hyrax.config.microdata_default_type if id.nil? Microdata.fetch("#{namespace}#{id}", default: Hyrax.config.microdata_default_type) end end |