labimotion 2.3.2 → 2.4.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,66 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_support/core_ext/object/blank'
4
-
5
- module Labimotion
6
- # Resolves which release of a template a saved element, segment or dataset follows, as
7
- # the pair [klass_uuid, properties_release]. The submitted properties carry the klass_uuid
8
- # they were built on; it counts when it is the klass's current release or one of its
9
- # revisions, so a copy of an older record keeps that release instead of being relabelled.
10
- module KlassVersion
11
- # A new record without a known release, such as a new empty one, follows the current release.
12
- # A copy of a record from an instance with releases of its own names a klass_uuid this one
13
- # cannot resolve; it keeps the release the client sent only when that release names the same uuid.
14
- def self.for_create(klass, revisions, submitted_uuid, client_release: nil)
15
- uuid = string_uuid(submitted_uuid)
16
- release = release_of(klass, revisions, uuid)
17
- return [uuid, release] if release
18
- return [uuid, client_release] if uuid.present? && release_uuid(client_release) == uuid
19
-
20
- [klass.uuid, klass.properties_release]
21
- end
22
-
23
- # An existing record keeps its stored klass_uuid when none is submitted, then the uuid its
24
- # stored release names, and follows the current release only when it has neither. A klass_uuid
25
- # this instance cannot resolve (a record imported from elsewhere) keeps the release stored with it.
26
- def self.for_update(klass, revisions, submitted_uuid, record)
27
- uuid = string_uuid(submitted_uuid).presence || record.klass_uuid.presence
28
- if uuid.nil?
29
- stored_uuid = release_uuid(record.properties_release)
30
- return [stored_uuid, record.properties_release] if stored_uuid
31
-
32
- return [klass.uuid, klass.properties_release]
33
- end
34
-
35
- release = release_of(klass, revisions, uuid) || record.properties_release.presence
36
- [uuid, release || klass.properties_release]
37
- end
38
-
39
- # for_update when the record exists, for_create when it is nil.
40
- def self.for_record(klass, revisions, submitted_uuid, record, client_release: nil)
41
- return for_create(klass, revisions, submitted_uuid, client_release: client_release) if record.nil?
42
-
43
- for_update(klass, revisions, submitted_uuid, record)
44
- end
45
-
46
- # Revisions are soft-deleted, and deleting one does not change which release the records
47
- # built on it follow, so a deleted revision still answers here.
48
- def self.release_of(klass, revisions, uuid)
49
- return if uuid.blank?
50
- return klass.properties_release if uuid == klass.uuid
51
-
52
- revisions.with_deleted.where(uuid: uuid).order(id: :desc).pick(:properties_release)
53
- end
54
-
55
- # Every release hash carries the uuid of the release it describes; nil when it names none.
56
- def self.release_uuid(release)
57
- release.is_a?(Hash) ? string_uuid(release['uuid']).presence : nil
58
- end
59
-
60
- # properties is a free-form hash, so klass_uuid can arrive as an array or a number; either
61
- # would turn the revision lookup into a query matching more than the record's own release.
62
- def self.string_uuid(submitted_uuid)
63
- submitted_uuid.is_a?(String) ? submitted_uuid : nil
64
- end
65
- end
66
- end