-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem_change_set.rb
78 lines (61 loc) · 3.15 KB
/
item_change_set.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# frozen_string_literal: true
# ChangeSet for ItemResource
class ItemChangeSet < ChangeSet
include ModificationDetailsChangeSet
include LockableChangeSet
DERIVATIVE_TYPES = %w[iiif_manifest].freeze
# ChangeSet for Structural Metadata
class StructuralMetadataChangeSet < ChangeSet
VIEWING_DIRECTIONS = %w[right-to-left left-to-right top-to-bottom bottom-to-top].freeze
VIEWING_HINTS = %w[individuals paged].freeze
property :viewing_direction, multiple: false, required: false
property :viewing_hint, multiple: false, required: false
property :arranged_asset_ids, multiple: true, required: true
validates :viewing_direction, inclusion: VIEWING_DIRECTIONS, allow_nil: true
validates :viewing_hint, inclusion: VIEWING_HINTS, allow_nil: true
# Allow emptying of asset arrangement by submitting a blank array
def arranged_asset_ids=(values)
super(compact_value(values))
end
def viewing_direction=(value)
super(compact_value(value))
end
def viewing_hint=(value)
super(compact_value(value))
end
end
# ChangeSet for Item Derivatives
class ItemDerivativeChangeSet < DerivativeChangeSet
validates :type, inclusion: DERIVATIVE_TYPES
end
# Defining Fields
property :unique_identifier, multiple: false, required: false
property :human_readable_name, multiple: false, required: true
property :thumbnail_asset_id, multiple: false, required: true
property :internal_notes, multiple: true, required: false
property :descriptive_metadata, multiple: false, required: true, form: DescriptiveMetadataChangeSet
property :structural_metadata, multiple: false, required: true, form: StructuralMetadataChangeSet
# Letting derivatives be defined as a `collection` because derivatives are always set via the setter and not the
# `validate` method therefore we don't run into problems when deleting derivatives. More information about this
# can be found here: https://gitlab.library.upenn.edu/dld/digital-repository/apotheca/-/issues/202
collection :derivatives, multiple: true, form: ItemDerivativeChangeSet, populate_if_empty: DerivativeResource
property :published, multiple: false, required: false, default: false
property :first_published_at, multiple: false, required: false
property :last_published_at, multiple: false, required: false
property :asset_ids, multiple: true, required: false
# Validations
validates :human_readable_name, presence: true
validates :published, inclusion: [true, false]
validates :thumbnail_asset_id, presence: true, included_in: :asset_ids, unless: ->(item) { item.asset_ids.blank? }
validates :unique_identifier, presence: true, format: { with: %r{\Aark:/}, message: 'must be an ARK' }
validate :ensure_arranged_asset_ids_are_valid
# Ensuring arranged_asset_ids are also present in asset_ids.
def ensure_arranged_asset_ids_are_valid
return if structural_metadata.arranged_asset_ids.blank?
return if structural_metadata.arranged_asset_ids.all? { |a| asset_ids&.include?(a) }
errors.add(:'structural_metadata.arranged_asset_ids', 'are not all included in asset_ids')
end
def internal_notes=(values)
super(compact_value(values))
end
end