Class: PaperTrailDiff::ActivityCollectionRecordUpdater
- Inherits:
-
Object
- Object
- PaperTrailDiff::ActivityCollectionRecordUpdater
- Defined in:
- lib/paper_trail_diff/activity_collection_record_updater.rb,
sig/generated/paper_trail_diff/activity_collection_record_updater.rbs
Overview
Applies one event to the leaf records of a collection association snapshot.
Instance Method Summary collapse
-
#call(association, version, replacement) ⇒ [ Array[RecordSnapshot], RecordSnapshot?, RecordSnapshot?, bool ]
: (AssociationSnapshot, untyped, RecordSnapshot?) -> [Array[RecordSnapshot], RecordSnapshot?, RecordSnapshot?, bool].
-
#event_child(association, change, reflection, owner) ⇒ RecordSnapshot?
: (AssociationSnapshot, ActivityCollectionRouteChange, untyped, RecordSnapshot) -> RecordSnapshot?.
-
#initialize(relationship) ⇒ ActivityCollectionRecordUpdater
constructor
: (ActivityRelationship) -> void.
- #replacement_records(records, position, replacement) ⇒ Array[RecordSnapshot]
-
#replacement_type(version, replacement) ⇒ String
: (untyped, RecordSnapshot?) -> String.
Constructor Details
#initialize(relationship) ⇒ ActivityCollectionRecordUpdater
: (ActivityRelationship) -> void
8 9 10 |
# File 'lib/paper_trail_diff/activity_collection_record_updater.rb', line 8 def initialize(relationship) @relationship = relationship end |
Instance Method Details
#call(association, version, replacement) ⇒ [ Array[RecordSnapshot], RecordSnapshot?, RecordSnapshot?, bool ]
: (AssociationSnapshot, untyped, RecordSnapshot?) -> [Array[RecordSnapshot], RecordSnapshot?, RecordSnapshot?, bool]
34 35 36 37 38 39 40 41 42 |
# File 'lib/paper_trail_diff/activity_collection_record_updater.rb', line 34 def call(association, version, replacement) records = association.records position = association.position(replacement_type(version, replacement), version.item_id) return [records, nil, nil, true] unless replacement || position before = records.fetch(position) if position updated = replacement_records(records, position, replacement) [updated, before, replacement, !before.nil? && !replacement.nil?] end |
#event_child(association, change, reflection, owner) ⇒ RecordSnapshot?
: (AssociationSnapshot, ActivityCollectionRouteChange, untyped, RecordSnapshot) -> RecordSnapshot?
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/paper_trail_diff/activity_collection_record_updater.rb', line 13 def event_child(association, change, reflection, owner) version = change.version record = change.record replacement = change.replacement return unless record unless record.is_a?(ActivitySnapshotDelta) return replacement if @relationship.member_of_owner?(record, reflection, owner) return end unless record.relationship_changed?(reflection) return replacement if association.position_for_id(version.item_id) return end replacement if @relationship.member_of_owner?(record, reflection, owner) end |
#replacement_records(records, position, replacement) ⇒ Array[RecordSnapshot]
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/paper_trail_diff/activity_collection_record_updater.rb', line 56 def replacement_records(records, position, replacement) updated = records.dup if replacement position ? updated[position] = replacement : updated << replacement else position ||= raise(ConfigurationError, 'missing collection event identity') updated.delete_at(position) end updated.freeze end |
#replacement_type(version, replacement) ⇒ String
: (untyped, RecordSnapshot?) -> String
45 46 47 48 49 |
# File 'lib/paper_trail_diff/activity_collection_record_updater.rb', line 45 def replacement_type(version, replacement) return replacement.type.to_s if replacement Endpoint.model_class(version).name.to_s end |