Change Sets
To understand Change Sets, see Dive into Valkyrie tutorial.
Change Sets and Hyrax
Hyrax automatically creates change sets for new/edit forms. See New/Edit Forms section for more information.
Custom Change Sets Use Case
We define 2 work types: PublicationResource and ReleaseResource. A release can be in one and only one publication. The publication uses attribute member_ids
to link to releases. The release uses attribute publication_id
as a link to the publication. To support this, we have method ReleaseResource #move_to_new_publication
which changes 3 resources: the release, the old publication, and the new publication.
To support this, a change set was defined for publications and releases.
class PublicationResourceChangeSet < Hyrax::ChangeSet
validates :title, presence: true
end
class ReleaseResourceChangeSet < Hyrax::ChangeSet
validates :title, presence: true
validates :publication_id, presence: true
end
The basic move process is…
- instantiate a change set for all 3 resources
- in the old publication change set, remove the release id from the old publication’s member_ids
- in the new publication change set, add the release id to the new publication’s member_ids
- in the release change set, set the release’s publication id to the new publication id
- validate all 3 change sets
Example…release_change_set.valid?
- if validations pass, use UpdateWork transaction to sync each change set and save the resources
Example…Hyrax::Transactions::UpdateWork.new.call(release_change_set).or do |failure| raise "Unable to save release #{change_sets[:self_change_set].title}; " \ "CAUSE: #{failure.first}" end
- The
UpdateWork
transaction callsvalid?
on the resource being updated, so why do I callvalid?
before executing the transactions?
Transactions are not reversible. When executing multiple transactions, if one passes and a later one fails, this may cause an invalid state. In an attempt to mitigate this,valid?
is called on all 3 resources before callingUpdateWork
transaction on each.