Mint and manage persistent identifiers (PIDs) for works in a Hyrax or Hyku application.
hyrax-pid separates three elements of PID minting:
- Minters — thin adapters for identifier service's API. Each implements
the same small interface (
mint,update,find,identifier_type) and returns a plainHyrax::Pid::Identifiervalue object, so callers never deal with a provider's raw response. A DataCite DOI minter is included; adapters for other services (Crossref, RAiD, …) may follow. - Metadata mapping — a configurable mapping from your work's attributes to the payload the service expects. Each field can be a symbol, callable, or literal, so it works with both fixed and flexible (M3) metadata schemas without hardcoding attribute names. Required fields fail loudly if they can't be resolved; optional ones are simply omitted.
- Registry and configuration —
Hyrax::Pid::Registrymaps a key such as:doito a lazily-built, configured minter, so credentials and sandbox vs. production settings are resolved at call time, not at load time.
It's a Rails engine for Hyrax 5. It plugs into Hyrax's own identifier framework
(Hyrax::Identifier::Dispatcher / Hyrax.config.identifier_registrars) under
the :doi key, alongside any other registrars, and adds an admin-only
"Mint DOI" button that can be switched on and off per tenant.
This is deliberately smaller than hyrax-doi: no actors, no Bolognese crosswalk, no assumptions about which attributes your works have. You tell it where the metadata comes from; it tells the service.
In your
application's Gemfile:
gem "hyrax-pid", github: "research-technologies/hyrax-pid"and bundle install
Hyku Knapsack: Add line to a file under bundler.d/ (e.g. bundler.d/hyrax_pid.rb), for bundler-inject to pick up.
The engine runs its own migration (a hyrax_pid_services table) as part of
bin/rails db:migrate; in Hyku that creates it in every tenant.
Service passwords are stored with Rails' Active Record encryption, which needs
keys. Generate them with bin/rails db:encryption:init, then either put them in
your Rails credentials as that command suggests, or set them as environment
variables:
ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY=...
ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT=...
Keep the keys safe and the same across deploys: if they change, stored credentials can't be read and have to be entered again.
Admins enter each service's credentials under Configuration → PID services in
the dashboard (/pid/admin/services). In Hyku these are per tenant. New
settings default to DataCite's test system; untick sandbox to mint real DOIs.
Create config/initializers/hyrax_pid.rb:
Rails.application.config.to_prepare do
Hyrax::Pid.configure do |config|
# How to read DataCite's required fields off a work. A Symbol is called as
# a method on the work, a callable is called with the work, anything else
# is used as a literal.
config.data_cite.field_mapping = {
title: :title,
creator: :creator,
publisher: "My Institution",
publication_year: ->(work) { Array(work.date_created).first.to_s[/\d{4}/] },
resource_type_general: "Other"
}
# Optional: replace the HTTP client, e.g. with a fake for demos and tests.
# config.data_cite.client = MyFakeClient.new
end
end:doi resolves to a DataCite minter built from the current tenant's saved
settings each time it's requested.
Works need a single-valued doi attribute. With flexible metadata, add a doi
property to your M3 profile; otherwise add one to your work models. Index it to
Solr too: the buttons read it from there (doi_ssi by default; set
config.data_cite.solr_field if yours differs).
The engine mounts itself at /pid, so no route changes are needed. Render the
buttons wherever you want them, typically in an override of
app/views/hyrax/base/_show_actions.html.erb:
<%= render 'hyrax/pid/doi_actions', presenter: presenter %>This renders two buttons (each partial can also be rendered on its own):
hyrax/pid/mint_button: Mint DOI, which creates a draft DOI, or Update DOI Metadata once the work has one.hyrax/pid/register_button: takes the DOI one step along the tenant's workflow (below). It only shows on public works (not under embargo or lease) whose DOI isn't findable yet, and asks for confirmation, since registered and findable DOIs can't be deleted.
Each DOI's last known state is kept in the hyrax_pid_registrations table, so
show pages don't call DataCite. DOIs minted before that table existed have no
recorded state and are treated as drafts.
Admins choose a workflow per tenant on the DataCite settings page:
| Workflow | Steps |
|---|---|
| Draft → Findable (default) | Register DOI makes a draft findable. |
| Draft → Registered → Findable | Register DOI makes a draft registered: it resolves, but DataCite leaves it out of its search and metadata feeds. Make DOI findable then lists it publicly. |
Either way, when a work with a findable DOI stops being public (made private, or
put under embargo), Hyrax::Pid::HideIdentifierJob sends DataCite's hide event, which
moves the DOI back to registered. Nothing happens automatically when the work is
made public again: an admin uses the button. The job is queued from a listener on
Hyrax's object.acl.updated event, and re-checks the saved work before calling
DataCite.
The buttons only appear for admins, once DataCite has been configured, and once the Mint DOI feature
(mint_doi, off by default) is turned on in the dashboard's Features page
(/admin/features). In Hyku
this is per tenant. It also stays hidden while Hyrax's read-only mode is on.
The feature switch is not access control: Flipflop lets anyone force a feature on for themselves with a cookie. Admin rights are what restrict minting.
Minting sends the work's page on the current host as the DOI's landing page
url, which DataCite needs before a DOI can be made findable.
Once a service is configured, Test connection on the PID services page checks the saved settings without minting anything. For DataCite that's two checks:
- The credentials: a
HEADrequest to the MDS API's DOI list (mds.datacite.org/doi, ormds.test.datacite.org/doiin sandbox mode), which answers 401 to wrong ones. Neither the REST API (which treats bad credentials as anonymous on reads) nor MDS's per-DOI paths (which answer "404 DOI not found" whatever the credentials) can tell. - The prefix: that it's listed on the repository's public record in the REST API.
A custom config.data_cite.client needs a
#check_credentials(prefix:) method for this to work.
work = Hyrax.query_service.find_by(id: id)
metadata = Hyrax::Pid.configuration.data_cite.metadata_mapper.build(work)
doi = Hyrax::Pid::Registry.minter_for(:doi).mint(metadata: metadata)
doi.value # => "10.1234/abcd-efgh"
doi.state # => :draftOr through Hyrax's identifier framework, which mints (or updates, if the work already has a DOI) and saves the work in one call:
Hyrax::Identifier::Dispatcher
.for(:doi, landing_page_url: ->(work) { main_app.polymorphic_url(work) })
.assign_for!(object: work, attribute: :doi)Bug reports and pull requests are welcome on GitHub at https://github.com/research-technologies/hyrax-pid.