paper_trail_diff 0.16.0 → 0.17.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +581 -1
- data/README.md +42 -7
- data/docs/architecture.md +7 -5
- data/docs/performance.md +86 -38
- data/docs/reference.md +309 -92
- data/docs/testing.md +240 -32
- data/lib/paper_trail_diff/activity_belongs_to_event_applier.rb +2 -16
- data/lib/paper_trail_diff/activity_boundary.rb +55 -34
- data/lib/paper_trail_diff/activity_child_candidate_loader.rb +46 -57
- data/lib/paper_trail_diff/activity_collection_event_applier.rb +3 -13
- data/lib/paper_trail_diff/activity_collection_route_updater.rb +1 -1
- data/lib/paper_trail_diff/activity_event.rb +33 -21
- data/lib/paper_trail_diff/activity_event_record_resolver.rb +16 -116
- data/lib/paper_trail_diff/activity_event_route_finder.rb +29 -29
- data/lib/paper_trail_diff/activity_event_snapshot_refresher.rb +2 -7
- data/lib/paper_trail_diff/activity_history.rb +90 -49
- data/lib/paper_trail_diff/activity_range.rb +9 -35
- data/lib/paper_trail_diff/activity_root_snapshot_refresher.rb +74 -40
- data/lib/paper_trail_diff/activity_root_steps.rb +10 -13
- data/lib/paper_trail_diff/activity_scope_filter.rb +77 -0
- data/lib/paper_trail_diff/activity_snapshot_delta.rb +12 -15
- data/lib/paper_trail_diff/activity_snapshot_sequence.rb +37 -15
- data/lib/paper_trail_diff/activity_target_candidate_loader.rb +35 -43
- data/lib/paper_trail_diff/activity_timeline_builder.rb +194 -249
- data/lib/paper_trail_diff/activity_transaction_grouper.rb +3 -1
- data/lib/paper_trail_diff/activity_tree_filter.rb +54 -13
- data/lib/paper_trail_diff/activity_version_collector.rb +72 -138
- data/lib/paper_trail_diff/activity_view.rb +22 -11
- data/lib/paper_trail_diff/analysis.rb +19 -5
- data/lib/paper_trail_diff/analysis_batch.rb +16 -58
- data/lib/paper_trail_diff/approximate_paths.rb +4 -8
- data/lib/paper_trail_diff/association_activity_edge.rb +5 -24
- data/lib/paper_trail_diff/association_activity_selection.rb +10 -18
- data/lib/paper_trail_diff/association_discovery.rb +1 -6
- data/lib/paper_trail_diff/association_key.rb +9 -6
- data/lib/paper_trail_diff/association_traversal.rb +49 -6
- data/lib/paper_trail_diff/batch_boundary_resolver.rb +33 -42
- data/lib/paper_trail_diff/batch_preload.rb +2 -12
- data/lib/paper_trail_diff/batched_root_analyses.rb +72 -0
- data/lib/paper_trail_diff/batched_root_analyzer.rb +16 -14
- data/lib/paper_trail_diff/batched_root_versions.rb +25 -20
- data/lib/paper_trail_diff/branch_snapshot_refresher.rb +27 -31
- data/lib/paper_trail_diff/change_attribution.rb +329 -0
- data/lib/paper_trail_diff/change_row.rb +30 -10
- data/lib/paper_trail_diff/comparison_batch.rb +3 -4
- data/lib/paper_trail_diff/configuration.rb +1 -1
- data/lib/paper_trail_diff/deleted_analysis_batch.rb +22 -37
- data/lib/paper_trail_diff/deleted_root_filter.rb +3 -6
- data/lib/paper_trail_diff/deprecation.rb +4 -43
- data/lib/paper_trail_diff/diagnostics.rb +1 -2
- data/lib/paper_trail_diff/direct_record_state_loader.rb +8 -1
- data/lib/paper_trail_diff/endpoint.rb +78 -44
- data/lib/paper_trail_diff/errors.rb +2 -9
- data/lib/paper_trail_diff/fallback_paths.rb +2 -5
- data/lib/paper_trail_diff/habtm_transaction_owners.rb +5 -3
- data/lib/paper_trail_diff/historical_association_reifier.rb +3 -1
- data/lib/paper_trail_diff/historical_filter.rb +40 -15
- data/lib/paper_trail_diff/historical_snapshot_store.rb +80 -38
- data/lib/paper_trail_diff/instrumentation.rb +0 -8
- data/lib/paper_trail_diff/live_endpoint_batch_loader.rb +11 -7
- data/lib/paper_trail_diff/live_endpoint_provider.rb +7 -23
- data/lib/paper_trail_diff/live_graph_collector.rb +3 -3
- data/lib/paper_trail_diff/nested_comparator.rb +46 -19
- data/lib/paper_trail_diff/net_change.rb +276 -0
- data/lib/paper_trail_diff/own_window.rb +35 -0
- data/lib/paper_trail_diff/paper_trail_adapter.rb +40 -45
- data/lib/paper_trail_diff/prepared_activity_index.rb +197 -45
- data/lib/paper_trail_diff/prepared_child_candidates.rb +58 -0
- data/lib/paper_trail_diff/prepared_edge_loader.rb +43 -107
- data/lib/paper_trail_diff/prepared_history.rb +20 -31
- data/lib/paper_trail_diff/prepared_history_loader.rb +46 -9
- data/lib/paper_trail_diff/prepared_record_index.rb +137 -47
- data/lib/paper_trail_diff/prepared_snapshot_builder.rb +16 -0
- data/lib/paper_trail_diff/prepared_state_association_reader.rb +17 -19
- data/lib/paper_trail_diff/read_snapshot.rb +41 -19
- data/lib/paper_trail_diff/recorded_changes.rb +10 -11
- data/lib/paper_trail_diff/recorded_changes_reader.rb +60 -0
- data/lib/paper_trail_diff/recorded_class.rb +14 -19
- data/lib/paper_trail_diff/recorded_columns.rb +33 -0
- data/lib/paper_trail_diff/root_version_plan.rb +20 -22
- data/lib/paper_trail_diff/root_version_selection.rb +39 -29
- data/lib/paper_trail_diff/scope_condition.rb +7 -17
- data/lib/paper_trail_diff/scope_ranking.rb +16 -22
- data/lib/paper_trail_diff/scoped_analysis.rb +27 -4
- data/lib/paper_trail_diff/scoped_analysis_builder.rb +4 -3
- data/lib/paper_trail_diff/scoped_root_selection.rb +60 -41
- data/lib/paper_trail_diff/snapshot_normalizer.rb +18 -3
- data/lib/paper_trail_diff/stored_json_values.rb +52 -0
- data/lib/paper_trail_diff/stored_time_precision.rb +30 -18
- data/lib/paper_trail_diff/stored_values.rb +20 -0
- data/lib/paper_trail_diff/support.rb +185 -35
- data/lib/paper_trail_diff/time_of_day.rb +39 -0
- data/lib/paper_trail_diff/time_range.rb +37 -20
- data/lib/paper_trail_diff/time_version_range.rb +5 -10
- data/lib/paper_trail_diff/timeline_builder.rb +3 -3
- data/lib/paper_trail_diff/timeline_range.rb +18 -12
- data/lib/paper_trail_diff/timeline_snapshot_provider.rb +1 -1
- data/lib/paper_trail_diff/total_order.rb +55 -0
- data/lib/paper_trail_diff/transaction_cut.rb +192 -91
- data/lib/paper_trail_diff/traversal_preparer.rb +5 -8
- data/lib/paper_trail_diff/value_objects.rb +8 -4
- data/lib/paper_trail_diff/version.rb +1 -1
- data/lib/paper_trail_diff/version_association_candidate_scope.rb +27 -16
- data/lib/paper_trail_diff/version_boundary.rb +37 -21
- data/lib/paper_trail_diff/version_query.rb +25 -25
- data/lib/paper_trail_diff/version_range.rb +12 -16
- data/lib/paper_trail_diff/version_sequence_diagnostics.rb +32 -0
- data/lib/paper_trail_diff/version_table.rb +12 -8
- data/lib/paper_trail_diff.rb +28 -42
- data/sig/generated/paper_trail_diff/activity_belongs_to_event_applier.rbs +0 -8
- data/sig/generated/paper_trail_diff/activity_boundary.rbs +39 -17
- data/sig/generated/paper_trail_diff/activity_child_candidate_loader.rbs +19 -22
- data/sig/generated/paper_trail_diff/activity_collection_event_applier.rbs +0 -5
- data/sig/generated/paper_trail_diff/activity_event.rbs +25 -9
- data/sig/generated/paper_trail_diff/activity_event_record_resolver.rbs +6 -34
- data/sig/generated/paper_trail_diff/activity_event_route_finder.rbs +11 -4
- data/sig/generated/paper_trail_diff/activity_event_snapshot_refresher.rbs +0 -3
- data/sig/generated/paper_trail_diff/activity_history.rbs +54 -19
- data/sig/generated/paper_trail_diff/activity_range.rbs +6 -20
- data/sig/generated/paper_trail_diff/activity_root_snapshot_refresher.rbs +36 -2
- data/sig/generated/paper_trail_diff/activity_root_steps.rbs +6 -6
- data/sig/generated/paper_trail_diff/activity_scope_filter.rbs +51 -0
- data/sig/generated/paper_trail_diff/activity_snapshot_delta.rbs +5 -3
- data/sig/generated/paper_trail_diff/activity_snapshot_sequence.rbs +21 -3
- data/sig/generated/paper_trail_diff/activity_target_candidate_loader.rbs +19 -24
- data/sig/generated/paper_trail_diff/activity_timeline_builder.rbs +121 -127
- data/sig/generated/paper_trail_diff/activity_tree_filter.rbs +41 -8
- data/sig/generated/paper_trail_diff/activity_version_collector.rbs +42 -35
- data/sig/generated/paper_trail_diff/activity_view.rbs +16 -8
- data/sig/generated/paper_trail_diff/analysis.rbs +15 -6
- data/sig/generated/paper_trail_diff/analysis_batch.rbs +6 -24
- data/sig/generated/paper_trail_diff/association_activity_edge.rbs +0 -13
- data/sig/generated/paper_trail_diff/association_activity_selection.rbs +6 -9
- data/sig/generated/paper_trail_diff/association_discovery.rbs +0 -3
- data/sig/generated/paper_trail_diff/association_key.rbs +5 -0
- data/sig/generated/paper_trail_diff/association_traversal.rbs +26 -2
- data/sig/generated/paper_trail_diff/batch_boundary_resolver.rbs +17 -17
- data/sig/generated/paper_trail_diff/batch_preload.rbs +0 -4
- data/sig/generated/paper_trail_diff/batched_root_analyses.rbs +47 -0
- data/sig/generated/paper_trail_diff/batched_root_analyzer.rbs +9 -8
- data/sig/generated/paper_trail_diff/batched_root_versions.rbs +10 -6
- data/sig/generated/paper_trail_diff/branch_snapshot_refresher.rbs +11 -5
- data/sig/generated/paper_trail_diff/change_attribution.rbs +194 -0
- data/sig/generated/paper_trail_diff/change_row.rbs +22 -7
- data/sig/generated/paper_trail_diff/deleted_analysis_batch.rbs +11 -12
- data/sig/generated/paper_trail_diff/deleted_root_filter.rbs +0 -2
- data/sig/generated/paper_trail_diff/deprecation.rbs +4 -24
- data/sig/generated/paper_trail_diff/direct_record_state_loader.rbs +4 -0
- data/sig/generated/paper_trail_diff/endpoint.rbs +40 -16
- data/sig/generated/paper_trail_diff/errors.rbs +2 -11
- data/sig/generated/paper_trail_diff/habtm_transaction_owners.rbs +2 -1
- data/sig/generated/paper_trail_diff/historical_filter.rbs +20 -9
- data/sig/generated/paper_trail_diff/historical_snapshot_store.rbs +40 -10
- data/sig/generated/paper_trail_diff/instrumentation.rbs +0 -3
- data/sig/generated/paper_trail_diff/live_endpoint_batch_loader.rbs +3 -0
- data/sig/generated/paper_trail_diff/live_endpoint_provider.rbs +5 -8
- data/sig/generated/paper_trail_diff/live_graph_collector.rbs +2 -2
- data/sig/generated/paper_trail_diff/nested_comparator.rbs +20 -4
- data/sig/generated/paper_trail_diff/net_change.rbs +168 -0
- data/sig/generated/paper_trail_diff/own_window.rbs +22 -0
- data/sig/generated/paper_trail_diff/paper_trail_adapter.rbs +9 -6
- data/sig/generated/paper_trail_diff/prepared_activity_index.rbs +114 -14
- data/sig/generated/paper_trail_diff/prepared_child_candidates.rbs +39 -0
- data/sig/generated/paper_trail_diff/prepared_edge_loader.rbs +18 -35
- data/sig/generated/paper_trail_diff/prepared_history.rbs +9 -12
- data/sig/generated/paper_trail_diff/prepared_history_loader.rbs +28 -3
- data/sig/generated/paper_trail_diff/prepared_record_index.rbs +68 -14
- data/sig/generated/paper_trail_diff/prepared_snapshot_builder.rbs +12 -0
- data/sig/generated/paper_trail_diff/prepared_state_association_reader.rbs +6 -7
- data/sig/generated/paper_trail_diff/read_snapshot.rbs +27 -8
- data/sig/generated/paper_trail_diff/recorded_changes.rbs +4 -2
- data/sig/generated/paper_trail_diff/recorded_changes_reader.rbs +41 -0
- data/sig/generated/paper_trail_diff/recorded_class.rbs +7 -6
- data/sig/generated/paper_trail_diff/recorded_columns.rbs +21 -0
- data/sig/generated/paper_trail_diff/root_version_plan.rbs +8 -4
- data/sig/generated/paper_trail_diff/root_version_selection.rbs +27 -13
- data/sig/generated/paper_trail_diff/scope_condition.rbs +0 -2
- data/sig/generated/paper_trail_diff/scope_ranking.rbs +10 -10
- data/sig/generated/paper_trail_diff/scoped_analysis.rbs +19 -0
- data/sig/generated/paper_trail_diff/scoped_analysis_builder.rbs +2 -2
- data/sig/generated/paper_trail_diff/scoped_root_selection.rbs +37 -14
- data/sig/generated/paper_trail_diff/snapshot_normalizer.rbs +7 -0
- data/sig/generated/paper_trail_diff/stored_json_values.rbs +35 -0
- data/sig/generated/paper_trail_diff/stored_time_precision.rbs +19 -5
- data/sig/generated/paper_trail_diff/stored_values.rbs +13 -0
- data/sig/generated/paper_trail_diff/support.rbs +120 -23
- data/sig/generated/paper_trail_diff/time_of_day.rbs +21 -0
- data/sig/generated/paper_trail_diff/time_range.rbs +15 -8
- data/sig/generated/paper_trail_diff/time_version_range.rbs +2 -5
- data/sig/generated/paper_trail_diff/timeline_builder.rbs +2 -2
- data/sig/generated/paper_trail_diff/timeline_range.rbs +12 -7
- data/sig/generated/paper_trail_diff/total_order.rbs +32 -0
- data/sig/generated/paper_trail_diff/transaction_cut.rbs +111 -52
- data/sig/generated/paper_trail_diff/traversal_preparer.rbs +2 -2
- data/sig/generated/paper_trail_diff/value_objects.rbs +5 -1
- data/sig/generated/paper_trail_diff/version_association_candidate_scope.rbs +10 -9
- data/sig/generated/paper_trail_diff/version_boundary.rbs +15 -8
- data/sig/generated/paper_trail_diff/version_query.rbs +17 -10
- data/sig/generated/paper_trail_diff/version_range.rbs +6 -9
- data/sig/generated/paper_trail_diff/version_sequence_diagnostics.rbs +13 -0
- data/sig/generated/paper_trail_diff/version_table.rbs +3 -0
- data/sig/generated/paper_trail_diff.rbs +16 -16
- metadata +28 -6
- data/lib/paper_trail_diff/preloaded_endpoint_batch_loader.rb +0 -60
- data/sig/generated/paper_trail_diff/preloaded_endpoint_batch_loader.rbs +0 -32
|
@@ -6,17 +6,15 @@ module PaperTrailDiff
|
|
|
6
6
|
class PreparedHistory
|
|
7
7
|
attr_reader :activity_index #: PreparedActivityIndex?
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
#: (PreparedRecordIndex, ?activity_index: PreparedActivityIndex?) -> void
|
|
18
|
-
def initialize(records, activity_index: nil)
|
|
9
|
+
# The transactions that crossed the history's start, keyed as
|
|
10
|
+
# ActivityTransactionGroups keys a version's; see
|
|
11
|
+
# PreparedHistoryLoader#reload.
|
|
12
|
+
attr_reader :opening_transactions #: Array[Array[untyped]]
|
|
13
|
+
|
|
14
|
+
#: (PreparedRecordIndex, ?activity_index: PreparedActivityIndex?, ?opening_transactions: Array[Array[untyped]]) -> void
|
|
15
|
+
def initialize(records, activity_index: nil, opening_transactions: [])
|
|
19
16
|
@records = records
|
|
17
|
+
@opening_transactions = opening_transactions
|
|
20
18
|
@conditions = ScopeCondition::Cache.new
|
|
21
19
|
@state_reader = PreparedStateAssociationReader.new(records, method(:edge), @conditions)
|
|
22
20
|
@activity_index = activity_index
|
|
@@ -25,6 +23,9 @@ module PaperTrailDiff
|
|
|
25
23
|
@habtm = {} #: Hash[Array[String], Hash[String, Array[untyped]]]
|
|
26
24
|
end
|
|
27
25
|
|
|
26
|
+
#: () -> Array[untyped]
|
|
27
|
+
def versions = @records.versions
|
|
28
|
+
|
|
28
29
|
# Returns every group the edge now holds.
|
|
29
30
|
#: (untyped, untyped, Hash[String, Hash[Symbol, untyped]]) -> Hash[String, Hash[Symbol, untyped]]
|
|
30
31
|
def merge_edge(owner_class, reflection, groups)
|
|
@@ -97,23 +98,17 @@ module PaperTrailDiff
|
|
|
97
98
|
|
|
98
99
|
#: (untyped, untyped, untyped, untyped, bool) -> Array[untyped]?
|
|
99
100
|
def direct_records(record, reflection, boundary, habtm_boundary, ranked)
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
belongs_to_records(record, reflection, boundary, ranked)
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
#: (untyped, untyped, untyped, untyped, bool) -> Array[untyped]
|
|
110
|
-
def resolve_children(record, reflection, boundary, _habtm_boundary, ranked)
|
|
111
|
-
child_records(record, reflection, boundary, ranked)
|
|
101
|
+
case reflection.macro
|
|
102
|
+
when :belongs_to then belongs_to_records(record, reflection, boundary, ranked)
|
|
103
|
+
when :has_one, :has_many then child_records(record, reflection, boundary, ranked)
|
|
104
|
+
when :has_and_belongs_to_many
|
|
105
|
+
habtm_records(record, reflection, boundary, habtm_boundary, ranked)
|
|
106
|
+
end
|
|
112
107
|
end
|
|
113
108
|
|
|
114
109
|
#: (untyped, untyped, untyped, bool) -> Array[untyped]?
|
|
115
110
|
def belongs_to_records(record, reflection, boundary, ranked)
|
|
116
|
-
foreign_key = scalar_foreign_key(reflection)
|
|
111
|
+
foreign_key = AssociationKey.scalar_foreign_key(reflection)
|
|
117
112
|
return unless foreign_key
|
|
118
113
|
|
|
119
114
|
id = record.public_send(foreign_key)
|
|
@@ -202,7 +197,7 @@ module PaperTrailDiff
|
|
|
202
197
|
|
|
203
198
|
#: (untyped, untyped, untyped) -> bool
|
|
204
199
|
def member_of?(child, reflection, owner)
|
|
205
|
-
foreign_key = scalar_foreign_key(reflection)
|
|
200
|
+
foreign_key = AssociationKey.scalar_foreign_key(reflection)
|
|
206
201
|
return false unless foreign_key
|
|
207
202
|
return false unless child.public_send(foreign_key).to_s == owner.id.to_s
|
|
208
203
|
return false unless AssociationTraversal.sti_member?(reflection.klass, child.attributes)
|
|
@@ -211,12 +206,6 @@ module PaperTrailDiff
|
|
|
211
206
|
owner_types(reflection, owner).include?(child.public_send(reflection.type).to_s)
|
|
212
207
|
end
|
|
213
208
|
|
|
214
|
-
#: (untyped) -> String?
|
|
215
|
-
def scalar_foreign_key(reflection)
|
|
216
|
-
key = reflection.foreign_key
|
|
217
|
-
key.to_s unless key.is_a?(Array)
|
|
218
|
-
end
|
|
219
|
-
|
|
220
209
|
#: (untyped, untyped) -> Array[String]
|
|
221
210
|
def owner_types(reflection, owner)
|
|
222
211
|
types = [owner.class.name, owner.class.base_class.name, reflection.active_record.name]
|
|
@@ -230,7 +219,7 @@ module PaperTrailDiff
|
|
|
230
219
|
|
|
231
220
|
#: (Array[untyped]) -> Array[untyped]
|
|
232
221
|
def unique_records(records)
|
|
233
|
-
records.uniq { |record|
|
|
222
|
+
records.uniq { |record| Support.record_identity(record.class, record.id) }
|
|
234
223
|
end
|
|
235
224
|
|
|
236
225
|
#: (untyped, untyped) -> Array[String]
|
|
@@ -5,7 +5,10 @@ module PaperTrailDiff
|
|
|
5
5
|
# Expands the explicit association tree into a request-scoped PreparedHistory.
|
|
6
6
|
class PreparedHistoryLoader
|
|
7
7
|
# Without `end_at`, the activity range closes on the last root version.
|
|
8
|
-
|
|
8
|
+
# A `window` is a time range; a range of versions starts at the state its
|
|
9
|
+
# first version names. `opening` names the transactions that crossed the
|
|
10
|
+
# window's start when this history is read again from where they began.
|
|
11
|
+
#: (untyped, root_ids: Array[untyped], root_versions: Array[untyped], tree: AssociationTree, traversal: AssociationTraversal, start_at: untyped, end_at: untyped, live_records: Array[untyped], ?window: bool, ?opening: Array[Array[untyped]]) -> void
|
|
9
12
|
def initialize( # rubocop:disable Metrics/ParameterLists
|
|
10
13
|
record,
|
|
11
14
|
root_ids:,
|
|
@@ -14,30 +17,42 @@ module PaperTrailDiff
|
|
|
14
17
|
traversal:,
|
|
15
18
|
start_at:,
|
|
16
19
|
end_at:,
|
|
17
|
-
live_records
|
|
20
|
+
live_records:,
|
|
21
|
+
window: false,
|
|
22
|
+
opening: []
|
|
18
23
|
)
|
|
24
|
+
@window = window
|
|
19
25
|
@record = record
|
|
20
26
|
@root_ids = root_ids
|
|
21
27
|
@root_versions = root_versions
|
|
22
28
|
@tree = tree
|
|
23
29
|
@traversal = traversal
|
|
30
|
+
@end_at = end_at
|
|
31
|
+
@live_records = live_records
|
|
24
32
|
start_at = VersionBoundary.transaction_start(root_versions, start_at)
|
|
25
33
|
@records = PreparedRecordIndex.new(start_at, end_at: end_at, live_records: live_records)
|
|
26
34
|
@activity = PreparedActivityIndex.new(@records, root_versions, start_at: start_at)
|
|
27
|
-
@history = PreparedHistory.new(@records, activity_index: @activity
|
|
28
|
-
|
|
29
|
-
@
|
|
35
|
+
@history = PreparedHistory.new(@records, activity_index: @activity,
|
|
36
|
+
opening_transactions: opening)
|
|
37
|
+
@edges = PreparedEdgeLoader.new(@records, root_versions, children: @activity.children)
|
|
38
|
+
@prepared = {} #: Hash[untyped, Set[String]]
|
|
30
39
|
end
|
|
31
40
|
|
|
32
41
|
#: () -> PreparedHistory
|
|
33
42
|
def call
|
|
34
43
|
@records.seed(@record.class, @root_ids, @root_versions)
|
|
35
44
|
load_node(@record.class, @root_ids, @tree, path: '')
|
|
36
|
-
@
|
|
45
|
+
opening = @records.opening if @window
|
|
46
|
+
return @history unless opening
|
|
47
|
+
|
|
48
|
+
reload(*opening)
|
|
37
49
|
end
|
|
38
50
|
|
|
39
51
|
private
|
|
40
52
|
|
|
53
|
+
# @rbs @window: bool
|
|
54
|
+
# @rbs @end_at: untyped
|
|
55
|
+
# @rbs @live_records: Array[untyped]
|
|
41
56
|
# @rbs @record: untyped
|
|
42
57
|
# @rbs @root_ids: Array[untyped]
|
|
43
58
|
# @rbs @root_versions: Array[untyped]
|
|
@@ -47,7 +62,29 @@ module PaperTrailDiff
|
|
|
47
62
|
# @rbs @activity: PreparedActivityIndex
|
|
48
63
|
# @rbs @history: PreparedHistory
|
|
49
64
|
# @rbs @edges: PreparedEdgeLoader
|
|
50
|
-
# @rbs @prepared: Hash[untyped,
|
|
65
|
+
# @rbs @prepared: Hash[untyped, Set[String]]
|
|
66
|
+
|
|
67
|
+
# A transaction that crosses a window's start and saved a version of the
|
|
68
|
+
# root moves the window's edge back to where it began (see
|
|
69
|
+
# TransactionCut); one that saved only associated records moves nothing.
|
|
70
|
+
# A boundary in such a transaction still reads each record as it stood
|
|
71
|
+
# before the transaction began (see PreparedRecordSeries#boundary_index),
|
|
72
|
+
# and a history read from the window's start holds neither the versions
|
|
73
|
+
# it saved before the start nor the records only those versions name, such
|
|
74
|
+
# as one it destroyed there. Without them the window would start after
|
|
75
|
+
# changes that the window before it left to this one, by ending its step
|
|
76
|
+
# at one of the transaction's events here (see ActivityTreeFilter), and
|
|
77
|
+
# neither would report them. So the history is read again from where the
|
|
78
|
+
# earliest such transaction began; the window's events still start at its
|
|
79
|
+
# start.
|
|
80
|
+
#: (Array[Array[untyped]], untyped) -> PreparedHistory
|
|
81
|
+
def reload(keys, earliest)
|
|
82
|
+
PreparedHistoryLoader.new(
|
|
83
|
+
@record, root_ids: @root_ids, root_versions: @root_versions, tree: @tree,
|
|
84
|
+
traversal: @traversal, start_at: earliest, end_at: @end_at,
|
|
85
|
+
live_records: @live_records, opening: keys
|
|
86
|
+
).call
|
|
87
|
+
end
|
|
51
88
|
|
|
52
89
|
#: (untyped, Array[untyped], AssociationTree, path: String) -> void
|
|
53
90
|
def load_node(model_class, ids, tree, path:)
|
|
@@ -102,13 +139,13 @@ module PaperTrailDiff
|
|
|
102
139
|
# the edge; a subclass that inherits it reads the same records.
|
|
103
140
|
#: (Array[untyped], untyped) -> Array[untyped]
|
|
104
141
|
def unprepared_ids(ids, reflection)
|
|
105
|
-
prepared = @prepared.fetch(reflection
|
|
142
|
+
prepared = @prepared.fetch(reflection) { Set.new }
|
|
106
143
|
ids.reject { |id| prepared.include?(id.to_s) }
|
|
107
144
|
end
|
|
108
145
|
|
|
109
146
|
#: (Array[untyped], untyped) -> void
|
|
110
147
|
def mark_prepared(ids, reflection)
|
|
111
|
-
@prepared[reflection]
|
|
148
|
+
(@prepared[reflection] ||= Set.new).merge(ids.map(&:to_s))
|
|
112
149
|
end
|
|
113
150
|
|
|
114
151
|
#: (untyped, untyped, Hash[String, Hash[Symbol, untyped]]) -> void
|
|
@@ -45,8 +45,7 @@ module PaperTrailDiff
|
|
|
45
45
|
# change.
|
|
46
46
|
#: (untyped, Hash[untyped, untyped]) -> Hash[untyped, untyped]
|
|
47
47
|
def stored_attributes(model_class, attributes)
|
|
48
|
-
|
|
49
|
-
Support.immutable_copy(StoredTimePrecision.apply(attributes, digits))
|
|
48
|
+
Support.immutable_copy(StoredValues.apply(model_class, attributes))
|
|
50
49
|
end
|
|
51
50
|
end
|
|
52
51
|
|
|
@@ -65,7 +64,7 @@ module PaperTrailDiff
|
|
|
65
64
|
attributes = version.object_deserialized
|
|
66
65
|
return unless attributes.is_a?(Hash)
|
|
67
66
|
|
|
68
|
-
attributes =
|
|
67
|
+
attributes = Support.decodable(attributes)
|
|
69
68
|
model_class = reification_class(version, attributes)
|
|
70
69
|
return unless direct_attributes?(model_class, attributes)
|
|
71
70
|
|
|
@@ -80,11 +79,6 @@ module PaperTrailDiff
|
|
|
80
79
|
# @rbs @serializers: Hash[untyped, untyped]
|
|
81
80
|
# @rbs @attribute_names: Hash[untyped, Array[String]]
|
|
82
81
|
|
|
83
|
-
#: (Hash[untyped, untyped]) -> Hash[String, untyped]
|
|
84
|
-
def stringify_keys(attributes)
|
|
85
|
-
attributes.to_h { |name, value| [name.to_s, value] }
|
|
86
|
-
end
|
|
87
|
-
|
|
88
82
|
#: (untyped, Hash[String, untyped]) -> untyped
|
|
89
83
|
def reification_class(version, attributes)
|
|
90
84
|
model_class = Endpoint.model_class(version)
|
|
@@ -116,14 +110,19 @@ module PaperTrailDiff
|
|
|
116
110
|
class PreparedRecordSeries
|
|
117
111
|
attr_reader :versions #: Array[untyped]
|
|
118
112
|
|
|
119
|
-
|
|
120
|
-
|
|
113
|
+
# The live fallback is a state, or a current record whose state is read
|
|
114
|
+
# the first time a read reaches past the last version. Most reads stop at
|
|
115
|
+
# a version, and reading a row's state casts every column it has.
|
|
116
|
+
#: (versions: Array[untyped], live: PreparedRecordState?, ?live_record: untyped, ?state_loader: PreparedVersionStateLoader) -> void
|
|
117
|
+
def initialize(versions:, live:, live_record: nil, state_loader: PreparedVersionStateLoader.new)
|
|
121
118
|
@versions = Support.chronological_sort(versions).freeze
|
|
119
|
+
@times = @versions.map(&:created_at).freeze
|
|
122
120
|
@version_positions = @versions.each_with_index.to_h do |version, index|
|
|
123
121
|
[version.id.to_s, index]
|
|
124
122
|
end.freeze
|
|
125
123
|
@transaction_positions = build_transaction_positions
|
|
126
124
|
@live = live
|
|
125
|
+
@live_record = live_record
|
|
127
126
|
@state_loader = state_loader
|
|
128
127
|
@states = {} #: Hash[untyped, PreparedRecordState?]
|
|
129
128
|
end
|
|
@@ -153,7 +152,7 @@ module PaperTrailDiff
|
|
|
153
152
|
index = boundary_index(boundary)
|
|
154
153
|
return state_for(versions.fetch(index)) if index
|
|
155
154
|
|
|
156
|
-
|
|
155
|
+
live
|
|
157
156
|
end
|
|
158
157
|
|
|
159
158
|
# A handled nil is known absence, not permission to query the current row.
|
|
@@ -174,24 +173,23 @@ module PaperTrailDiff
|
|
|
174
173
|
#: () -> Array[PreparedRecordState]
|
|
175
174
|
def states
|
|
176
175
|
recorded = versions.filter_map { |version| state_for(version) }
|
|
177
|
-
|
|
178
|
-
|
|
176
|
+
current = live
|
|
177
|
+
current ? [*recorded, current] : recorded
|
|
179
178
|
end
|
|
180
179
|
|
|
181
|
-
# The classes the record had at any point, or either side of one version
|
|
180
|
+
# The classes the record had at any point, or either side of one version:
|
|
181
|
+
# a create has no state before it and a destroy none after.
|
|
182
182
|
#: (?untyped) -> Array[untyped]
|
|
183
183
|
def model_classes(version = nil)
|
|
184
184
|
return states.map(&:model_class) unless version
|
|
185
185
|
|
|
186
|
-
|
|
186
|
+
states_across(version, version).map(&:model_class)
|
|
187
187
|
end
|
|
188
188
|
|
|
189
|
-
# The states
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
index = @version_positions.fetch(version.id.to_s)
|
|
194
|
-
[state_for(version), state_after(index)].compact
|
|
189
|
+
# The states before `first` and after `last`, where they exist.
|
|
190
|
+
#: (untyped, untyped) -> Array[PreparedRecordState]
|
|
191
|
+
def states_across(first, last)
|
|
192
|
+
[state_for(first), state_after(@version_positions.fetch(last.id.to_s))].compact
|
|
195
193
|
end
|
|
196
194
|
|
|
197
195
|
#: (untyped) -> [PreparedRecordState, PreparedRecordState]?
|
|
@@ -208,7 +206,9 @@ module PaperTrailDiff
|
|
|
208
206
|
|
|
209
207
|
private
|
|
210
208
|
|
|
209
|
+
# @rbs @times: Array[untyped]
|
|
211
210
|
# @rbs @live: PreparedRecordState?
|
|
211
|
+
# @rbs @live_record: untyped
|
|
212
212
|
# @rbs @state_loader: PreparedVersionStateLoader
|
|
213
213
|
# @rbs @states: Hash[untyped, PreparedRecordState?]
|
|
214
214
|
# @rbs @version_positions: Hash[String, Integer]
|
|
@@ -217,7 +217,16 @@ module PaperTrailDiff
|
|
|
217
217
|
#: (Integer) -> PreparedRecordState?
|
|
218
218
|
def state_after(index)
|
|
219
219
|
successor = versions[index + 1]
|
|
220
|
-
successor ? state_for(successor) :
|
|
220
|
+
successor ? state_for(successor) : live
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
#: () -> PreparedRecordState?
|
|
224
|
+
def live
|
|
225
|
+
record = @live_record
|
|
226
|
+
return @live unless record
|
|
227
|
+
|
|
228
|
+
@live_record = nil
|
|
229
|
+
@live = PreparedRecordState.new(record)
|
|
221
230
|
end
|
|
222
231
|
|
|
223
232
|
# A version is eligible when it is at or after the boundary, or shares the
|
|
@@ -228,11 +237,14 @@ module PaperTrailDiff
|
|
|
228
237
|
# "At or after" is decided by the version order rather than the timestamp
|
|
229
238
|
# alone. A version written in the same instant as the boundary but before
|
|
230
239
|
# it already took effect there; reading its pre-change state would put the
|
|
231
|
-
# boundary on the wrong side of a change it follows.
|
|
240
|
+
# boundary on the wrong side of a change it follows. Each time is read
|
|
241
|
+
# once, since every record is placed against every boundary.
|
|
232
242
|
#: (untyped) -> Integer?
|
|
233
243
|
def boundary_index(boundary)
|
|
234
|
-
|
|
235
|
-
|
|
244
|
+
boundary_time = boundary.created_at
|
|
245
|
+
chronological = (0...versions.length).bsearch do |index|
|
|
246
|
+
VersionBoundary.at_or_after?(versions.fetch(index), boundary,
|
|
247
|
+
time: @times.fetch(index), boundary_time: boundary_time)
|
|
236
248
|
end
|
|
237
249
|
transactional = transaction_index(boundary)
|
|
238
250
|
return chronological unless transactional
|
|
@@ -267,7 +279,8 @@ module PaperTrailDiff
|
|
|
267
279
|
prepared = @state_loader.call(version)
|
|
268
280
|
return @states[version.id] = prepared if prepared
|
|
269
281
|
|
|
270
|
-
record =
|
|
282
|
+
record = Support.reify(
|
|
283
|
+
version,
|
|
271
284
|
dup: true,
|
|
272
285
|
has_many: false,
|
|
273
286
|
has_one: false,
|
|
@@ -312,6 +325,15 @@ module PaperTrailDiff
|
|
|
312
325
|
ids.each { |id| add_series(model_class, id, grouped, live) }
|
|
313
326
|
end
|
|
314
327
|
|
|
328
|
+
# The transactions the versions held from the start onward belong to that
|
|
329
|
+
# began before it, and where the earliest of them began; see
|
|
330
|
+
# PreparedHistoryLoader. Nothing when none did.
|
|
331
|
+
#: () -> [Array[Array[untyped]], untyped]?
|
|
332
|
+
def opening
|
|
333
|
+
start = @start_time
|
|
334
|
+
start && OpeningTransactions.new(@series, start).call
|
|
335
|
+
end
|
|
336
|
+
|
|
315
337
|
#: (untyped, untyped, untyped) -> untyped
|
|
316
338
|
def record_before(model_class, id, boundary)
|
|
317
339
|
@series.fetch(identity(model_class, id)).record_before(boundary)
|
|
@@ -330,8 +352,7 @@ module PaperTrailDiff
|
|
|
330
352
|
state = series.state_before(boundary)
|
|
331
353
|
return [true, nil] unless state
|
|
332
354
|
|
|
333
|
-
|
|
334
|
-
prepared = @direct_states[state]
|
|
355
|
+
prepared = direct_state(state)
|
|
335
356
|
[!prepared.nil?, prepared]
|
|
336
357
|
end
|
|
337
358
|
|
|
@@ -352,7 +373,7 @@ module PaperTrailDiff
|
|
|
352
373
|
return [false, nil] unless handled
|
|
353
374
|
return [true, state] unless direct && state
|
|
354
375
|
|
|
355
|
-
prepared =
|
|
376
|
+
prepared = direct_state(state)
|
|
356
377
|
[!prepared.nil?, prepared]
|
|
357
378
|
end
|
|
358
379
|
|
|
@@ -366,7 +387,7 @@ module PaperTrailDiff
|
|
|
366
387
|
plain_reader = generated_reader?(model_class, name)
|
|
367
388
|
ids.flat_map do |id|
|
|
368
389
|
@series.fetch(identity(model_class, id)).states.map do |state|
|
|
369
|
-
direct =
|
|
390
|
+
direct = direct_state(state) if plain_reader
|
|
370
391
|
direct ? direct.attributes[name] : state.instantiate.public_send(name)
|
|
371
392
|
end
|
|
372
393
|
end
|
|
@@ -377,14 +398,22 @@ module PaperTrailDiff
|
|
|
377
398
|
@series.fetch(identity(model_class, id)).versions
|
|
378
399
|
end
|
|
379
400
|
|
|
401
|
+
#: () -> Array[untyped]
|
|
402
|
+
def versions
|
|
403
|
+
@series.each_value.flat_map(&:versions)
|
|
404
|
+
end
|
|
405
|
+
|
|
380
406
|
#: (untyped, untyped, ?untyped) -> Array[untyped]
|
|
381
407
|
def model_classes(model_class, id, version = nil)
|
|
382
408
|
@series.fetch(identity(model_class, id)).model_classes(version)
|
|
383
409
|
end
|
|
384
410
|
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
411
|
+
# The attributes before `first` and after `last`, where they exist, typed
|
|
412
|
+
# as a reified record's are.
|
|
413
|
+
#: (untyped, untyped, untyped, untyped) -> Array[Hash[untyped, untyped]]
|
|
414
|
+
def typed_states_across(model_class, id, first, last)
|
|
415
|
+
@series.fetch(identity(model_class, id)).states_across(first, last)
|
|
416
|
+
.map { |state| typed_attributes(state) }
|
|
388
417
|
end
|
|
389
418
|
|
|
390
419
|
#: (untyped, untyped) -> bool
|
|
@@ -398,13 +427,19 @@ module PaperTrailDiff
|
|
|
398
427
|
# instantiating it, as reify does.
|
|
399
428
|
#: (PreparedRecordState) -> Hash[untyped, untyped]
|
|
400
429
|
def typed_attributes(state)
|
|
401
|
-
|
|
402
|
-
typed = @direct_states[state]
|
|
430
|
+
typed = direct_state(state)
|
|
403
431
|
return typed.attributes if typed
|
|
404
432
|
|
|
405
433
|
PreparedRecordState.new(state.instantiate).attributes
|
|
406
434
|
end
|
|
407
435
|
|
|
436
|
+
# A state completed as a record of its class would hold it, or nil where
|
|
437
|
+
# only instantiating can tell; each state is completed once.
|
|
438
|
+
#: (PreparedRecordState) -> PreparedRecordState?
|
|
439
|
+
def direct_state(state)
|
|
440
|
+
@direct_states.fetch(state) { @direct_states[state] = @direct_loader.complete(state) }
|
|
441
|
+
end
|
|
442
|
+
|
|
408
443
|
#: (untyped, String) -> bool
|
|
409
444
|
def generated_reader?(model, name)
|
|
410
445
|
model.define_attribute_methods
|
|
@@ -427,7 +462,7 @@ module PaperTrailDiff
|
|
|
427
462
|
|
|
428
463
|
#: (untyped, untyped) -> Array[String]
|
|
429
464
|
def identity(model_class, id)
|
|
430
|
-
|
|
465
|
+
Support.record_identity(model_class, id)
|
|
431
466
|
end
|
|
432
467
|
|
|
433
468
|
#: (untyped, Array[untyped]) -> Array[untyped]
|
|
@@ -439,8 +474,7 @@ module PaperTrailDiff
|
|
|
439
474
|
def add_series(model_class, id, versions, live)
|
|
440
475
|
record = live[id.to_s]
|
|
441
476
|
@series[identity(model_class, id)] = PreparedRecordSeries.new(
|
|
442
|
-
versions: versions.fetch(id.to_s, []),
|
|
443
|
-
live: record && PreparedRecordState.new(record),
|
|
477
|
+
versions: versions.fetch(id.to_s, []), live: nil, live_record: record,
|
|
444
478
|
state_loader: @state_loader
|
|
445
479
|
)
|
|
446
480
|
end
|
|
@@ -448,16 +482,15 @@ module PaperTrailDiff
|
|
|
448
482
|
# A PaperTrail version is a pre-change snapshot, so the state at the last
|
|
449
483
|
# selected boundary can only live in the next version after it. One
|
|
450
484
|
# trailing version per identity is retained, rather than every version
|
|
451
|
-
# recorded between the requested range and the present.
|
|
485
|
+
# recorded between the requested range and the present. Each record's
|
|
486
|
+
# versions are read in one statement, which takes 500 records.
|
|
452
487
|
#: (untyped, Array[untyped]) -> Array[untyped]
|
|
453
488
|
def versions_for(model_class, ids)
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
scope = scope.where(window_condition(version_class)) if @end_time
|
|
460
|
-
scope.order(:id).to_a
|
|
489
|
+
ids.each_slice(Support::IDS_PER_STATEMENT).flat_map do |slice|
|
|
490
|
+
scope = VersionQuery.for_items(model_class, slice).where(created_at: @start_time..)
|
|
491
|
+
scope = scope.where(window_condition(model_class.paper_trail.version_class)) if @end_time
|
|
492
|
+
scope.order(:id).to_a
|
|
493
|
+
end
|
|
461
494
|
end
|
|
462
495
|
|
|
463
496
|
# In range, or the earliest version after it, resolved in the same query
|
|
@@ -485,7 +518,64 @@ module PaperTrailDiff
|
|
|
485
518
|
primary_key = model_class.primary_key
|
|
486
519
|
return [] if primary_key.is_a?(Array)
|
|
487
520
|
|
|
488
|
-
|
|
521
|
+
ids.each_slice(Support::IDS_PER_STATEMENT).flat_map do |slice|
|
|
522
|
+
model_class.base_class.unscoped.where(primary_key => slice).to_a
|
|
523
|
+
end
|
|
524
|
+
end
|
|
525
|
+
end
|
|
526
|
+
|
|
527
|
+
# Finds the transactions that crossed a history's start: those the versions
|
|
528
|
+
# held from the start onward belong to that also saved versions before it,
|
|
529
|
+
# of any record. One statement per 500 transactions.
|
|
530
|
+
class OpeningTransactions
|
|
531
|
+
#: (Hash[Array[String], PreparedRecordSeries], untyped) -> void
|
|
532
|
+
def initialize(series, start)
|
|
533
|
+
@series = series
|
|
534
|
+
@start = start
|
|
535
|
+
end
|
|
536
|
+
|
|
537
|
+
# Their keys, as ActivityTransactionGroups keys a version's, and when the
|
|
538
|
+
# earliest began; nil when none crossed the start.
|
|
539
|
+
#: () -> [Array[Array[untyped]], untyped]?
|
|
540
|
+
def call
|
|
541
|
+
began = transactions.flat_map do |version_class, ids|
|
|
542
|
+
earliest(version_class, ids).map { |id, at| [[version_class.name, id], at] }
|
|
543
|
+
end
|
|
544
|
+
return if began.empty?
|
|
545
|
+
|
|
546
|
+
[began.map(&:first), began.map(&:last).min]
|
|
547
|
+
end
|
|
548
|
+
|
|
549
|
+
private
|
|
550
|
+
|
|
551
|
+
# When each of these transactions saved its first version before the
|
|
552
|
+
# start, for those that saved one there.
|
|
553
|
+
#: (untyped, Array[untyped]) -> Array[[untyped, untyped]]
|
|
554
|
+
def earliest(version_class, transaction_ids)
|
|
555
|
+
before = version_class.arel_table[:created_at].lt(@start)
|
|
556
|
+
transaction_ids.each_slice(Support::IDS_PER_STATEMENT).flat_map do |slice|
|
|
557
|
+
version_class.where(transaction_id: slice).where(before).group(:transaction_id)
|
|
558
|
+
.minimum(:created_at).to_a
|
|
559
|
+
end
|
|
560
|
+
end
|
|
561
|
+
|
|
562
|
+
# @rbs @series: Hash[Array[String], PreparedRecordSeries]
|
|
563
|
+
# @rbs @start: untyped
|
|
564
|
+
|
|
565
|
+
# The transactions of the versions held from the start onward, by version
|
|
566
|
+
# class, since each version table numbers its own.
|
|
567
|
+
#: () -> Hash[untyped, Array[untyped]]
|
|
568
|
+
def transactions
|
|
569
|
+
found = {} #: Hash[untyped, Array[untyped]]
|
|
570
|
+
@series.each_value do |series|
|
|
571
|
+
series.versions.each do |version|
|
|
572
|
+
transaction_id = VersionBoundary.transaction_id(version)
|
|
573
|
+
next unless transaction_id && version.created_at >= @start
|
|
574
|
+
|
|
575
|
+
(found[version.class] ||= []) << transaction_id
|
|
576
|
+
end
|
|
577
|
+
end
|
|
578
|
+
found.transform_values(&:uniq)
|
|
489
579
|
end
|
|
490
580
|
end
|
|
491
581
|
end
|
|
@@ -20,6 +20,22 @@ module PaperTrailDiff
|
|
|
20
20
|
build(state, boundary, tree, normalizer, history, path: '')
|
|
21
21
|
end
|
|
22
22
|
|
|
23
|
+
# The state `call` builds a version's snapshot from, or nil when the
|
|
24
|
+
# version stored none or must be reified.
|
|
25
|
+
#: (untyped) -> PreparedRecordState?
|
|
26
|
+
def state(version)
|
|
27
|
+
@loader.call(version) if version.object
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# The current row of a root, given as its state, with its associations as
|
|
31
|
+
# `boundary` reads them. Its attributes are the record's own, as the record
|
|
32
|
+
# path normalizes them, so its model need not be plain; its members are
|
|
33
|
+
# read as a version's are.
|
|
34
|
+
#: (PreparedRecordState, untyped, tree: AssociationTree, normalizer: SnapshotNormalizer, history: PreparedHistory?) -> [bool, RecordSnapshot?]
|
|
35
|
+
def live(state, boundary, tree:, normalizer:, history:)
|
|
36
|
+
build(state, boundary, tree, normalizer, history, path: '')
|
|
37
|
+
end
|
|
38
|
+
|
|
23
39
|
private
|
|
24
40
|
|
|
25
41
|
# @rbs @traversal: AssociationTraversal
|
|
@@ -52,7 +52,8 @@ module PaperTrailDiff
|
|
|
52
52
|
id = owner.attributes[reflection.foreign_key.to_s]
|
|
53
53
|
return [true, []] if id.nil?
|
|
54
54
|
|
|
55
|
-
|
|
55
|
+
type = owner.attributes[reflection.foreign_type]
|
|
56
|
+
model_class = AssociationTraversal.stored_target(reflection, type)
|
|
56
57
|
return [false, []] unless model_class &&
|
|
57
58
|
@conditions.call(reflection, model_class, ranked: ranked)
|
|
58
59
|
|
|
@@ -60,13 +61,6 @@ module PaperTrailDiff
|
|
|
60
61
|
[handled, state && in_scope?(state, reflection, ranked, model_class) ? [state] : []]
|
|
61
62
|
end
|
|
62
63
|
|
|
63
|
-
#: (PreparedRecordState, untyped) -> untyped
|
|
64
|
-
def target_class(owner, reflection)
|
|
65
|
-
return reflection.klass unless reflection.polymorphic?
|
|
66
|
-
|
|
67
|
-
AssociationTraversal.named_class(owner.attributes[reflection.foreign_type].to_s)
|
|
68
|
-
end
|
|
69
|
-
|
|
70
64
|
# The members of each join record the owner had at the boundary, as
|
|
71
65
|
# PreparedHistory#through_records reads them from records. Under
|
|
72
66
|
# `source_type:`, only join records of that type count. A has_many
|
|
@@ -118,35 +112,39 @@ module PaperTrailDiff
|
|
|
118
112
|
[model.base_class.name, *Array(model.primary_key).map { |key| state.attributes[key].to_s }]
|
|
119
113
|
end
|
|
120
114
|
|
|
115
|
+
# The scope's condition and the owner's id are read once for all the
|
|
116
|
+
# owner's children, which are read at every boundary.
|
|
121
117
|
#: (PreparedRecordState, untyped, untyped, bool) -> [bool, Array[PreparedRecordState]]
|
|
122
118
|
def children(owner, reflection, boundary, ranked)
|
|
119
|
+
condition = @conditions.call(reflection, ranked: ranked) #: ScopeCondition
|
|
120
|
+
owner_id = owner_id(owner)
|
|
123
121
|
selected = [] #: Array[PreparedRecordState]
|
|
124
122
|
@edges.call(owner.model_class, reflection).each_value do |group|
|
|
125
|
-
ids_for(group,
|
|
123
|
+
ids_for(group, owner_id).each do |id|
|
|
126
124
|
handled, child = @records.snapshot_state_before(group.fetch(:model), id, boundary)
|
|
127
125
|
return [false, []] unless handled
|
|
128
126
|
|
|
129
|
-
selected << child if child && member?(child, owner, reflection,
|
|
127
|
+
selected << child if child && member?(child, owner, owner_id, reflection, condition)
|
|
130
128
|
end
|
|
131
129
|
end
|
|
132
130
|
return [true, selected] unless ranked
|
|
133
131
|
|
|
134
|
-
condition = @conditions.call(reflection) #: ScopeCondition
|
|
135
132
|
[true, first_if_one(reflection, condition.rank(selected))]
|
|
136
133
|
end
|
|
137
134
|
|
|
138
|
-
#: (untyped,
|
|
139
|
-
def ids_for(group,
|
|
135
|
+
#: (untyped, String) -> Array[untyped]
|
|
136
|
+
def ids_for(group, owner_id)
|
|
140
137
|
owners = group[:owners]
|
|
141
138
|
empty = [] #: Array[untyped]
|
|
142
|
-
owners ? owners.fetch(owner_id
|
|
139
|
+
owners ? owners.fetch(owner_id, empty) : group.fetch(:ids)
|
|
143
140
|
end
|
|
144
141
|
|
|
145
|
-
#: (PreparedRecordState, PreparedRecordState, untyped,
|
|
146
|
-
def member?(child, owner, reflection,
|
|
147
|
-
|
|
148
|
-
return false unless
|
|
149
|
-
return false unless
|
|
142
|
+
#: (PreparedRecordState, PreparedRecordState, String, untyped, ScopeCondition) -> bool
|
|
143
|
+
def member?(child, owner, owner_id, reflection, condition)
|
|
144
|
+
attributes = child.attributes
|
|
145
|
+
return false unless attributes[reflection.foreign_key.to_s].to_s == owner_id
|
|
146
|
+
return false unless AssociationTraversal.sti_member?(reflection.klass, attributes)
|
|
147
|
+
return false unless condition.matches?(attributes)
|
|
150
148
|
return true unless reflection.options[:as]
|
|
151
149
|
|
|
152
150
|
matching_type?(child, owner.model_class, reflection)
|