Module: Labimotion::TemplateDoiHelpers

Included in:
ReleaseTemplateDoi, ReserveTemplateDoi, UpdateTemplatePublicationMetadata
Defined in:
lib/labimotion/usecases/template_doi_helpers.rb

Overview

Shared helpers for LabIMotion template DOIs: the type <-> klass mapping, authorization, the public Template Hub URL and "is a new version reservable" check. Used by the template DOI usecases and APIs.

Constant Summary collapse

KLASS_BY_TYPE =
{
  'element' => ::Labimotion::ElementKlass,
  'segment' => ::Labimotion::SegmentKlass,
  'dataset' => ::Labimotion::DatasetKlass
}.freeze
GENERIC_ADMIN_KEY =

Maps a template record to the generic_admin profile key that grants admin rights over it. Keys are plural, matching the LabIMotion gem's authenticate_admin! and User#generic_admin.

{
  ::Labimotion::ElementKlass => ::Labimotion::Constants::Family::ELEMENT,
  ::Labimotion::SegmentKlass => ::Labimotion::Constants::Family::SEGMENT,
  ::Labimotion::DatasetKlass => ::Labimotion::Constants::Family::DATASET
}.freeze
HOST_ENV_KEYS =

Where the hub lives, most specific first. PUBLIC_URL is the instance's own public address (the host app's config/application.rb insists on it), so the link follows whichever instance is serving it rather than a hardcoded chemotion-repository.net. The DataCite-facing domains stay as fallbacks for deployments that only set those.

%w[PUBLIC_URL REPO_HOST DOI_DOMAIN_TEST DOI_DOMAIN].freeze
CONTRIBUTOR_TYPES =

The DataCite kernel-4 contributorType vocabulary. A template DOI may name any number of contributors — including none — each credited in one of these roles. The list is closed: DataCite rejects anything else.

%w[
  ContactPerson DataCollector DataCurator DataManager Distributor Editor
  HostingInstitution Producer ProjectLeader ProjectManager ProjectMember
  RegistrationAgency RegistrationAuthority RelatedPerson Researcher
  ResearchGroup RightsHolder Sponsor Supervisor WorkPackageLeader Other
].freeze
DEFAULT_CONTRIBUTOR_TYPE =
'Researcher'

Class Method Summary collapse

Class Method Details

.authorized?(record, user) ⇒ Boolean

A user may manage a template's DOI iff they hold the LabIMotion generic_admin right for that template kind — the same gate the gem's authenticate_admin! and the admin UI use. admin_ids/created_by do NOT apply: segment templates carry neither, so they were always denied.

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
# File 'lib/labimotion/usecases/template_doi_helpers.rb', line 87

def authorized?(record, user)
  return false unless record && user

  key = GENERIC_ADMIN_KEY[record.class]
  return false unless key

  admin = user.respond_to?(:generic_admin) ? user.generic_admin : nil
  admin.is_a?(Hash) && !!admin[key]
end

.klass_type_for(record) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/labimotion/usecases/template_doi_helpers.rb', line 55

def klass_type_for(record)
  case record
  when ::Labimotion::ElementKlass then 'element'
  when ::Labimotion::SegmentKlass then 'segment'
  when ::Labimotion::DatasetKlass then 'dataset'
  end
end

.new_version_available?(record) ⇒ Boolean

True once the template's latest DOI has been released (minted) and the template has since moved to a new major version — i.e. a DOI for the new version may now be reserved. False while there is no DOI, the latest is still unreleased, or the DOI version is unchanged (released = read-only, and minor template revisions keep the same DOI).

Returns:

  • (Boolean)


48
49
50
51
52
53
# File 'lib/labimotion/usecases/template_doi_helpers.rb', line 48

def new_version_available?(record)
  latest = ::Doi.labimotion_latest_doi(record)
  return false unless latest&.minted

  ::Doi.labimotion_doi_version(latest) != ::Doi.labimotion_version_segment(record)
end

.public_hostObject

The instance serving the hub, without its trailing slash.



78
79
80
81
# File 'lib/labimotion/usecases/template_doi_helpers.rb', line 78

def public_host
  host = HOST_ENV_KEYS.lazy.filter_map { |key| ENV[key].presence }.first
  (host || 'http://localhost:3000').chomp('/')
end

.template_url(record) ⇒ Object

Public URL that opens the template in the LabIMotion Template Hub's "Find a Template" page (the Finder), preselecting the template type, the template and its release version:

/home/genericHub?type=finder&kind=<type>&id=<identifier|uuid>&version=<v>

Used as the DOI's DataCite landing page and surfaced in the UI.



68
69
70
71
72
73
74
75
# File 'lib/labimotion/usecases/template_doi_helpers.rb', line 68

def template_url(record)
  type = klass_type_for(record)
  record_id = record.try(:identifier).presence || record.try(:uuid).presence || record.id
  query = "type=finder&kind=#{type}&id=#{record_id}"
  version = record.try(:version)
  query += "&version=#{version}" if version.present?
  "#{public_host}/home/genericHub?#{query}"
end