paper_trail_diff 0.17.0 → 0.18.0

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.
Files changed (215) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +663 -0
  3. data/QUICKSTART.md +12 -14
  4. data/README.md +32 -30
  5. data/docs/architecture.md +65 -29
  6. data/docs/performance.md +161 -46
  7. data/docs/reference.md +495 -394
  8. data/docs/testing.md +9 -15
  9. data/lib/paper_trail_diff/activity_belongs_to_event_applier.rb +42 -9
  10. data/lib/paper_trail_diff/activity_boundary.rb +15 -6
  11. data/lib/paper_trail_diff/activity_child_candidate_loader.rb +91 -18
  12. data/lib/paper_trail_diff/activity_collection_event_applier.rb +14 -15
  13. data/lib/paper_trail_diff/activity_collection_record_updater.rb +18 -8
  14. data/lib/paper_trail_diff/activity_collection_route_updater.rb +12 -25
  15. data/lib/paper_trail_diff/activity_event.rb +53 -34
  16. data/lib/paper_trail_diff/activity_event_record_normalizer.rb +12 -26
  17. data/lib/paper_trail_diff/activity_event_record_resolver.rb +45 -32
  18. data/lib/paper_trail_diff/activity_event_snapshot_refresher.rb +26 -33
  19. data/lib/paper_trail_diff/activity_history.rb +28 -34
  20. data/lib/paper_trail_diff/activity_relationship.rb +3 -26
  21. data/lib/paper_trail_diff/activity_root_snapshot_refresher.rb +14 -8
  22. data/lib/paper_trail_diff/activity_snapshot_delta.rb +2 -3
  23. data/lib/paper_trail_diff/activity_snapshot_sequence.rb +92 -25
  24. data/lib/paper_trail_diff/activity_timeline_builder.rb +128 -77
  25. data/lib/paper_trail_diff/activity_transaction_applier.rb +49 -0
  26. data/lib/paper_trail_diff/activity_tree_filter.rb +70 -57
  27. data/lib/paper_trail_diff/activity_tree_places.rb +76 -0
  28. data/lib/paper_trail_diff/activity_version_collector.rb +45 -32
  29. data/lib/paper_trail_diff/activity_view.rb +2 -0
  30. data/lib/paper_trail_diff/analysis.rb +6 -5
  31. data/lib/paper_trail_diff/analysis_batch.rb +21 -17
  32. data/lib/paper_trail_diff/association_activity_edge.rb +99 -85
  33. data/lib/paper_trail_diff/association_activity_selection.rb +46 -14
  34. data/lib/paper_trail_diff/association_discovery.rb +4 -4
  35. data/lib/paper_trail_diff/association_key.rb +10 -18
  36. data/lib/paper_trail_diff/association_traversal.rb +72 -92
  37. data/lib/paper_trail_diff/batch_boundary_resolver.rb +1 -7
  38. data/lib/paper_trail_diff/batch_preload.rb +2 -3
  39. data/lib/paper_trail_diff/batched_root_analyses.rb +60 -25
  40. data/lib/paper_trail_diff/batched_root_analyzer.rb +5 -5
  41. data/lib/paper_trail_diff/batched_root_versions.rb +7 -30
  42. data/lib/paper_trail_diff/branch_snapshot_refresher.rb +19 -14
  43. data/lib/paper_trail_diff/candidate_table.rb +160 -0
  44. data/lib/paper_trail_diff/change_attribution.rb +35 -28
  45. data/lib/paper_trail_diff/collection_identity_index.rb +68 -24
  46. data/lib/paper_trail_diff/collection_transition.rb +3 -3
  47. data/lib/paper_trail_diff/comparison_batch.rb +13 -6
  48. data/lib/paper_trail_diff/database_time.rb +85 -0
  49. data/lib/paper_trail_diff/deleted_analysis_batch.rb +22 -23
  50. data/lib/paper_trail_diff/deleted_root_filter.rb +10 -6
  51. data/lib/paper_trail_diff/diagnostics.rb +26 -86
  52. data/lib/paper_trail_diff/endpoint.rb +37 -5
  53. data/lib/paper_trail_diff/engine.rb +1 -1
  54. data/lib/paper_trail_diff/errors.rb +0 -6
  55. data/lib/paper_trail_diff/exact_column_type.rb +96 -0
  56. data/lib/paper_trail_diff/historical_snapshot_store.rb +88 -83
  57. data/lib/paper_trail_diff/instrumentation.rb +0 -8
  58. data/lib/paper_trail_diff/live_endpoint_batch_loader.rb +1 -4
  59. data/lib/paper_trail_diff/live_graph_collector.rb +1 -3
  60. data/lib/paper_trail_diff/nested_comparator.rb +42 -10
  61. data/lib/paper_trail_diff/paper_trail_adapter.rb +55 -98
  62. data/lib/paper_trail_diff/prepared_activity_index.rb +235 -179
  63. data/lib/paper_trail_diff/prepared_child_candidates.rb +13 -3
  64. data/lib/paper_trail_diff/prepared_edge_loader.rb +11 -49
  65. data/lib/paper_trail_diff/prepared_history.rb +27 -177
  66. data/lib/paper_trail_diff/prepared_history_loader.rb +39 -57
  67. data/lib/paper_trail_diff/prepared_record_index.rb +221 -147
  68. data/lib/paper_trail_diff/prepared_snapshot_builder.rb +47 -42
  69. data/lib/paper_trail_diff/prepared_state_association_reader.rb +85 -62
  70. data/lib/paper_trail_diff/recorded_changes.rb +34 -10
  71. data/lib/paper_trail_diff/recorded_owner.rb +67 -0
  72. data/lib/paper_trail_diff/root_version_selection.rb +6 -2
  73. data/lib/paper_trail_diff/scope_condition.rb +120 -80
  74. data/lib/paper_trail_diff/scope_ranking.rb +14 -32
  75. data/lib/paper_trail_diff/scoped_analysis.rb +11 -6
  76. data/lib/paper_trail_diff/scoped_analysis_builder.rb +17 -11
  77. data/lib/paper_trail_diff/scoped_root_selection.rb +91 -85
  78. data/lib/paper_trail_diff/selection_window.rb +31 -0
  79. data/lib/paper_trail_diff/snapshot.rb +60 -58
  80. data/lib/paper_trail_diff/snapshot_members.rb +115 -0
  81. data/lib/paper_trail_diff/snapshot_normalizer.rb +0 -11
  82. data/lib/paper_trail_diff/step.rb +1 -5
  83. data/lib/paper_trail_diff/stored_form.rb +146 -0
  84. data/lib/paper_trail_diff/stored_json_values.rb +8 -5
  85. data/lib/paper_trail_diff/stored_time_precision.rb +67 -33
  86. data/lib/paper_trail_diff/stored_values.rb +35 -3
  87. data/lib/paper_trail_diff/support.rb +185 -35
  88. data/lib/paper_trail_diff/time_of_day.rb +13 -2
  89. data/lib/paper_trail_diff/time_range.rb +85 -11
  90. data/lib/paper_trail_diff/time_version_range.rb +5 -1
  91. data/lib/paper_trail_diff/timeline_builder.rb +5 -11
  92. data/lib/paper_trail_diff/timeline_range.rb +56 -19
  93. data/lib/paper_trail_diff/timeline_snapshot_provider.rb +31 -7
  94. data/lib/paper_trail_diff/total_order.rb +19 -5
  95. data/lib/paper_trail_diff/transaction_anchors.rb +138 -0
  96. data/lib/paper_trail_diff/transaction_cut.rb +67 -327
  97. data/lib/paper_trail_diff/transaction_ends.rb +194 -0
  98. data/lib/paper_trail_diff/traversal_preparer.rb +12 -24
  99. data/lib/paper_trail_diff/{approximate_paths.rb → unreadable_paths.rb} +8 -9
  100. data/lib/paper_trail_diff/value_objects.rb +8 -6
  101. data/lib/paper_trail_diff/version.rb +1 -1
  102. data/lib/paper_trail_diff/version_association_candidate_scope.rb +28 -4
  103. data/lib/paper_trail_diff/version_boundary.rb +0 -29
  104. data/lib/paper_trail_diff/version_query.rb +49 -1
  105. data/lib/paper_trail_diff/version_scope_filter.rb +74 -5
  106. data/lib/paper_trail_diff/version_sequence_diagnostics.rb +4 -31
  107. data/lib/paper_trail_diff/version_table.rb +8 -1
  108. data/lib/paper_trail_diff.rb +56 -110
  109. data/sig/generated/paper_trail_diff/activity_belongs_to_event_applier.rbs +24 -2
  110. data/sig/generated/paper_trail_diff/activity_boundary.rbs +6 -0
  111. data/sig/generated/paper_trail_diff/activity_child_candidate_loader.rbs +46 -5
  112. data/sig/generated/paper_trail_diff/activity_collection_event_applier.rbs +7 -4
  113. data/sig/generated/paper_trail_diff/activity_collection_record_updater.rbs +11 -2
  114. data/sig/generated/paper_trail_diff/activity_collection_route_updater.rbs +8 -6
  115. data/sig/generated/paper_trail_diff/activity_event.rbs +27 -18
  116. data/sig/generated/paper_trail_diff/activity_event_record_normalizer.rbs +7 -11
  117. data/sig/generated/paper_trail_diff/activity_event_record_resolver.rbs +26 -8
  118. data/sig/generated/paper_trail_diff/activity_event_snapshot_refresher.rbs +10 -2
  119. data/sig/generated/paper_trail_diff/activity_history.rbs +23 -26
  120. data/sig/generated/paper_trail_diff/activity_relationship.rbs +0 -6
  121. data/sig/generated/paper_trail_diff/activity_root_snapshot_refresher.rbs +5 -0
  122. data/sig/generated/paper_trail_diff/activity_snapshot_sequence.rbs +53 -10
  123. data/sig/generated/paper_trail_diff/activity_timeline_builder.rbs +64 -38
  124. data/sig/generated/paper_trail_diff/activity_transaction_applier.rbs +28 -0
  125. data/sig/generated/paper_trail_diff/activity_tree_filter.rbs +46 -38
  126. data/sig/generated/paper_trail_diff/activity_tree_places.rbs +45 -0
  127. data/sig/generated/paper_trail_diff/activity_version_collector.rbs +21 -11
  128. data/sig/generated/paper_trail_diff/analysis.rbs +6 -5
  129. data/sig/generated/paper_trail_diff/analysis_batch.rbs +8 -6
  130. data/sig/generated/paper_trail_diff/association_activity_edge.rbs +58 -45
  131. data/sig/generated/paper_trail_diff/association_activity_selection.rbs +20 -3
  132. data/sig/generated/paper_trail_diff/association_discovery.rbs +2 -2
  133. data/sig/generated/paper_trail_diff/association_key.rbs +2 -11
  134. data/sig/generated/paper_trail_diff/association_traversal.rbs +43 -44
  135. data/sig/generated/paper_trail_diff/batch_boundary_resolver.rbs +0 -2
  136. data/sig/generated/paper_trail_diff/batched_root_analyses.rbs +40 -17
  137. data/sig/generated/paper_trail_diff/batched_root_analyzer.rbs +2 -2
  138. data/sig/generated/paper_trail_diff/batched_root_versions.rbs +2 -11
  139. data/sig/generated/paper_trail_diff/branch_snapshot_refresher.rbs +12 -8
  140. data/sig/generated/paper_trail_diff/candidate_table.rbs +97 -0
  141. data/sig/generated/paper_trail_diff/change_attribution.rbs +21 -19
  142. data/sig/generated/paper_trail_diff/collection_identity_index.rbs +33 -13
  143. data/sig/generated/paper_trail_diff/collection_transition.rbs +2 -2
  144. data/sig/generated/paper_trail_diff/comparison_batch.rbs +5 -0
  145. data/sig/generated/paper_trail_diff/database_time.rbs +48 -0
  146. data/sig/generated/paper_trail_diff/deleted_analysis_batch.rbs +13 -15
  147. data/sig/generated/paper_trail_diff/deleted_root_filter.rbs +4 -0
  148. data/sig/generated/paper_trail_diff/diagnostics.rbs +13 -31
  149. data/sig/generated/paper_trail_diff/endpoint.rbs +14 -1
  150. data/sig/generated/paper_trail_diff/errors.rbs +0 -8
  151. data/sig/generated/paper_trail_diff/exact_column_type.rbs +51 -0
  152. data/sig/generated/paper_trail_diff/historical_snapshot_store.rbs +51 -31
  153. data/sig/generated/paper_trail_diff/instrumentation.rbs +0 -5
  154. data/sig/generated/paper_trail_diff/nested_comparator.rbs +21 -0
  155. data/sig/generated/paper_trail_diff/paper_trail_adapter.rbs +18 -29
  156. data/sig/generated/paper_trail_diff/prepared_activity_index.rbs +149 -94
  157. data/sig/generated/paper_trail_diff/prepared_child_candidates.rbs +10 -2
  158. data/sig/generated/paper_trail_diff/prepared_edge_loader.rbs +4 -21
  159. data/sig/generated/paper_trail_diff/prepared_history.rbs +22 -79
  160. data/sig/generated/paper_trail_diff/prepared_history_loader.rbs +24 -32
  161. data/sig/generated/paper_trail_diff/prepared_record_index.rbs +137 -63
  162. data/sig/generated/paper_trail_diff/prepared_snapshot_builder.rbs +22 -13
  163. data/sig/generated/paper_trail_diff/prepared_state_association_reader.rbs +41 -28
  164. data/sig/generated/paper_trail_diff/recorded_changes.rbs +19 -3
  165. data/sig/generated/paper_trail_diff/recorded_owner.rbs +39 -0
  166. data/sig/generated/paper_trail_diff/root_version_selection.rbs +4 -1
  167. data/sig/generated/paper_trail_diff/scope_condition.rbs +65 -52
  168. data/sig/generated/paper_trail_diff/scope_ranking.rbs +10 -23
  169. data/sig/generated/paper_trail_diff/scoped_analysis.rbs +11 -6
  170. data/sig/generated/paper_trail_diff/scoped_analysis_builder.rbs +7 -2
  171. data/sig/generated/paper_trail_diff/scoped_root_selection.rbs +49 -43
  172. data/sig/generated/paper_trail_diff/selection_window.rbs +19 -0
  173. data/sig/generated/paper_trail_diff/snapshot.rbs +37 -38
  174. data/sig/generated/paper_trail_diff/snapshot_members.rbs +58 -0
  175. data/sig/generated/paper_trail_diff/snapshot_normalizer.rbs +0 -4
  176. data/sig/generated/paper_trail_diff/stored_form.rbs +63 -0
  177. data/sig/generated/paper_trail_diff/stored_json_values.rbs +0 -2
  178. data/sig/generated/paper_trail_diff/stored_time_precision.rbs +33 -20
  179. data/sig/generated/paper_trail_diff/stored_values.rbs +18 -2
  180. data/sig/generated/paper_trail_diff/support.rbs +96 -16
  181. data/sig/generated/paper_trail_diff/time_of_day.rbs +4 -0
  182. data/sig/generated/paper_trail_diff/time_range.rbs +49 -4
  183. data/sig/generated/paper_trail_diff/time_version_range.rbs +3 -0
  184. data/sig/generated/paper_trail_diff/timeline_builder.rbs +3 -6
  185. data/sig/generated/paper_trail_diff/timeline_range.rbs +29 -8
  186. data/sig/generated/paper_trail_diff/timeline_snapshot_provider.rbs +17 -4
  187. data/sig/generated/paper_trail_diff/total_order.rbs +9 -3
  188. data/sig/generated/paper_trail_diff/transaction_anchors.rbs +80 -0
  189. data/sig/generated/paper_trail_diff/transaction_cut.rbs +43 -176
  190. data/sig/generated/paper_trail_diff/transaction_ends.rbs +124 -0
  191. data/sig/generated/paper_trail_diff/traversal_preparer.rbs +6 -11
  192. data/sig/generated/paper_trail_diff/{approximate_paths.rbs → unreadable_paths.rbs} +6 -7
  193. data/sig/generated/paper_trail_diff/value_objects.rbs +4 -2
  194. data/sig/generated/paper_trail_diff/version_association_candidate_scope.rbs +16 -2
  195. data/sig/generated/paper_trail_diff/version_boundary.rbs +0 -14
  196. data/sig/generated/paper_trail_diff/version_query.rbs +26 -0
  197. data/sig/generated/paper_trail_diff/version_scope_filter.rbs +28 -2
  198. data/sig/generated/paper_trail_diff/version_sequence_diagnostics.rbs +4 -15
  199. data/sig/generated/paper_trail_diff/version_table.rbs +4 -0
  200. data/sig/generated/paper_trail_diff.rbs +22 -23
  201. metadata +28 -20
  202. data/lib/paper_trail_diff/activity_target_candidate_loader.rb +0 -123
  203. data/lib/paper_trail_diff/fallback_paths.rb +0 -35
  204. data/lib/paper_trail_diff/habtm_transaction_owners.rb +0 -70
  205. data/lib/paper_trail_diff/historical_association_reifier.rb +0 -161
  206. data/lib/paper_trail_diff/historical_filter.rb +0 -210
  207. data/lib/paper_trail_diff/own_window.rb +0 -35
  208. data/lib/paper_trail_diff/prepared_association_reifier.rb +0 -51
  209. data/sig/generated/paper_trail_diff/activity_target_candidate_loader.rbs +0 -74
  210. data/sig/generated/paper_trail_diff/fallback_paths.rbs +0 -22
  211. data/sig/generated/paper_trail_diff/habtm_transaction_owners.rbs +0 -41
  212. data/sig/generated/paper_trail_diff/historical_association_reifier.rbs +0 -58
  213. data/sig/generated/paper_trail_diff/historical_filter.rbs +0 -96
  214. data/sig/generated/paper_trail_diff/own_window.rbs +0 -22
  215. data/sig/generated/paper_trail_diff/prepared_association_reifier.rbs +0 -27
@@ -6,26 +6,35 @@ module PaperTrailDiff
6
6
  class PreparedHistory
7
7
  attr_reader :activity_index #: PreparedActivityIndex?
8
8
 
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: [])
9
+ #: (PreparedRecordIndex, ?activity_index: PreparedActivityIndex?) -> void
10
+ def initialize(records, activity_index: nil)
16
11
  @records = records
17
- @opening_transactions = opening_transactions
18
- @conditions = ScopeCondition::Cache.new
19
- @state_reader = PreparedStateAssociationReader.new(records, method(:edge), @conditions)
12
+ @state_reader = PreparedStateAssociationReader.new(records, method(:edge),
13
+ ScopeCondition::Cache.new)
20
14
  @activity_index = activity_index
21
15
  @edges = {} #: Hash[Array[String], Hash[String, Hash[Symbol, untyped]]]
22
- @unsupported = {} #: Hash[Array[String], bool]
23
- @habtm = {} #: Hash[Array[String], Hash[String, Array[untyped]]]
24
16
  end
25
17
 
18
+ # When the history was read from.
19
+ #: () -> untyped
20
+ def start_time = @records.start_time
21
+
26
22
  #: () -> Array[untyped]
27
23
  def versions = @records.versions
28
24
 
25
+ # See PreparedRecordIndex#reading.
26
+ #: (TransactionEnds) { () -> void } -> void
27
+ def reading(ends, &block)
28
+ @records.reading(ends, &block)
29
+ end
30
+
31
+ # See PreparedRecordIndex#transaction_ends.
32
+ #: (Array[untyped]) -> TransactionEnds
33
+ def transaction_ends(versions) = @records.transaction_ends(versions)
34
+
35
+ #: () -> TransactionEnds?
36
+ def reading_ends = @records.reading_ends
37
+
29
38
  # Returns every group the edge now holds.
30
39
  #: (untyped, untyped, Hash[String, Hash[Symbol, untyped]]) -> Hash[String, Hash[Symbol, untyped]]
31
40
  def merge_edge(owner_class, reflection, groups)
@@ -40,187 +49,28 @@ module PaperTrailDiff
40
49
  @edges.fetch(edge_key(owner_class, reflection), empty)
41
50
  end
42
51
 
43
- #: (untyped, untyped) -> void
44
- def mark_unsupported(owner_class, reflection)
45
- @unsupported[edge_key(owner_class, reflection)] = true
46
- end
47
-
48
- #: (untyped, untyped, Hash[String, Array[untyped]]) -> void
49
- def register_habtm(owner_class, reflection, memberships)
50
- @habtm[edge_key(owner_class, reflection)] = memberships.transform_keys(&:to_s)
51
- end
52
-
53
52
  #: (untyped, untyped, untyped) -> [Hash[untyped, untyped], Hash[untyped, untyped]]?
54
53
  def record_transition(model_class, id, version)
55
54
  @records.transition(model_class, id, version)
56
55
  end
57
56
 
58
- #: (untyped, untyped, untyped, ?direct: bool) -> [bool, untyped]
59
- def record_after(model_class, id, version, direct: false)
60
- @records.record_after(model_class, id, version, direct: direct)
57
+ #: (untyped, untyped, untyped) -> [bool, PreparedRecordState?]
58
+ def record_after(model_class, id, version)
59
+ @records.record_after(model_class, id, version)
61
60
  end
62
61
 
63
- #: (PreparedRecordState, untyped, untyped) -> [bool, Array[PreparedRecordState]]
62
+ # Who was in the owner's association at the boundary (see
63
+ # PreparedStateAssociationReader).
64
+ #: (PreparedRecordState, untyped, untyped) -> Array[PreparedRecordState]
64
65
  def snapshot_states(owner, reflection, boundary)
65
66
  @state_reader.call(owner, reflection, boundary)
66
67
  end
67
68
 
68
- # Unranked, as a part of a has_many through is read, it returns every row
69
- # the part's conditions admit, a record reached twice appearing twice.
70
- #: (untyped, untyped, untyped, habtm_boundary: untyped, ?ranked: bool) -> [bool, Array[untyped]]
71
- def resolve(record, reflection, boundary, habtm_boundary:, ranked: true)
72
- return [false, []] unless supported?(record.class, reflection, ranked)
73
-
74
- records = if reflection.options[:through]
75
- through_records(record, reflection, boundary, habtm_boundary, ranked)
76
- else
77
- direct_records(record, reflection, boundary, habtm_boundary, ranked)
78
- end
79
- records ? [true, records] : [false, []]
80
- end
81
-
82
69
  private
83
70
 
84
71
  # @rbs @records: PreparedRecordIndex
85
72
  # @rbs @state_reader: PreparedStateAssociationReader
86
73
  # @rbs @edges: Hash[Array[String], Hash[String, Hash[Symbol, untyped]]]
87
- # @rbs @unsupported: Hash[Array[String], bool]
88
- # @rbs @habtm: Hash[Array[String], Hash[String, Array[untyped]]]
89
- # @rbs @conditions: ScopeCondition::Cache
90
-
91
- # A polymorphic target's class is known only once a record names it, so
92
- # belongs_to_records reads its scope for each record instead.
93
- #: (untyped, untyped, bool) -> bool
94
- def supported?(owner_class, reflection, ranked)
95
- !@unsupported[edge_key(owner_class, reflection)] &&
96
- (reflection.polymorphic? || !@conditions.call(reflection, ranked: ranked).nil?)
97
- end
98
-
99
- #: (untyped, untyped, untyped, untyped, bool) -> Array[untyped]?
100
- def direct_records(record, reflection, boundary, habtm_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
107
- end
108
-
109
- #: (untyped, untyped, untyped, bool) -> Array[untyped]?
110
- def belongs_to_records(record, reflection, boundary, ranked)
111
- foreign_key = AssociationKey.scalar_foreign_key(reflection)
112
- return unless foreign_key
113
-
114
- id = record.public_send(foreign_key)
115
- return [] if id.nil?
116
-
117
- model_class = AssociationTraversal.belongs_to_target(reflection, record)
118
- return unless model_class && @conditions.call(reflection, model_class, ranked: ranked) &&
119
- @records.loaded?(model_class, id)
120
-
121
- target = Array(@records.record_before(model_class, id, boundary)).compact
122
- @conditions.select(reflection, target, model_class, ranked: ranked)
123
- end
124
-
125
- #: (untyped, untyped, untyped, bool) -> Array[untyped]
126
- def child_records(record, reflection, boundary, ranked)
127
- children = edge(record.class, reflection).values.flat_map do |group|
128
- model_class = group.fetch(:model)
129
- child_ids_for(group, record).filter_map do |id|
130
- child = @records.record_before(model_class, id, boundary)
131
- child if child && member_of?(child, reflection, record)
132
- end
133
- end
134
- @conditions.select(reflection, children, ranked: ranked)
135
- end
136
-
137
- #: (Hash[Symbol, untyped], untyped) -> Array[untyped]
138
- def child_ids_for(group, owner)
139
- owners = group[:owners]
140
- empty = [] #: Array[untyped]
141
- owners ? owners.fetch(owner.id.to_s, empty) : group.fetch(:ids)
142
- end
143
-
144
- # PT-AT records a checkpoint's membership as it is when the checkpoint is
145
- # saved, so a tag attached earlier in the checkpoint's own transaction is a
146
- # member there, unlike other associations, which a version reads as they
147
- # were before its transaction. A member created in that transaction has no
148
- # earlier state, so it is read as it was created, from its creation on.
149
- # The checkpoint's membership also stands for earlier points, since the
150
- # previous root version; the member did not exist at those yet.
151
- #: (untyped, untyped, untyped, untyped, bool) -> Array[untyped]?
152
- def habtm_records(record, reflection, boundary, habtm_boundary, ranked)
153
- transaction_id = transaction_id_for(habtm_boundary)
154
- return unless transaction_id
155
-
156
- empty = {} #: Hash[String, Array[untyped]]
157
- ids = @habtm.fetch(edge_key(record.class, reflection), empty)[transaction_id.to_s]
158
- return unless ids
159
-
160
- created = VersionBoundary.transaction_id(boundary).to_s == transaction_id.to_s
161
- members = ids.filter_map { |id| habtm_member(reflection.klass, id, boundary, created) }
162
- @conditions.select(reflection, members, ranked: ranked)
163
- end
164
-
165
- #: (untyped, untyped, untyped, bool) -> untyped
166
- def habtm_member(model_class, id, boundary, created)
167
- @records.record_before(model_class, id, boundary) ||
168
- (@records.record_created_in(model_class, id, boundary) if created)
169
- end
170
-
171
- # A has_many through reads its parts unranked, as its query does. Its own
172
- # scope applies to each row, and a ranked read then keeps a record reached
173
- # twice as one member.
174
- #: (untyped, untyped, untyped, untyped, bool) -> Array[untyped]?
175
- def through_records(record, reflection, boundary, habtm_boundary, ranked)
176
- read = lambda do |owner, part|
177
- resolve(owner, part, boundary, habtm_boundary: habtm_boundary,
178
- ranked: ranked && !reflection.collection?)
179
- end
180
- rows = through_rows(record, reflection, read)
181
- return unless rows
182
-
183
- rows = @conditions.select(reflection, rows, ranked: ranked)
184
- ranked ? unique_records(rows) : rows
185
- end
186
-
187
- # The source records of each join record, once for each join.
188
- #: (untyped, untyped, ^(untyped, untyped) -> [bool, Array[untyped]]) -> Array[untyped]?
189
- def through_rows(record, reflection, read)
190
- handled, joins = read.call(record, reflection.through_reflection)
191
- return unless handled
192
-
193
- joins = joins.select { |join| AssociationTraversal.source_type_join?(reflection, join) }
194
- resolved = joins.map { |join| read.call(join, reflection.source_reflection) }
195
- resolved.flat_map(&:last) if resolved.all?(&:first)
196
- end
197
-
198
- #: (untyped, untyped, untyped) -> bool
199
- def member_of?(child, reflection, owner)
200
- foreign_key = AssociationKey.scalar_foreign_key(reflection)
201
- return false unless foreign_key
202
- return false unless child.public_send(foreign_key).to_s == owner.id.to_s
203
- return false unless AssociationTraversal.sti_member?(reflection.klass, child.attributes)
204
- return true unless reflection.options[:as]
205
-
206
- owner_types(reflection, owner).include?(child.public_send(reflection.type).to_s)
207
- end
208
-
209
- #: (untyped, untyped) -> Array[String]
210
- def owner_types(reflection, owner)
211
- types = [owner.class.name, owner.class.base_class.name, reflection.active_record.name]
212
- types.map(&:to_s).uniq
213
- end
214
-
215
- #: (untyped) -> untyped
216
- def transaction_id_for(boundary)
217
- boundary.transaction_id
218
- end
219
-
220
- #: (Array[untyped]) -> Array[untyped]
221
- def unique_records(records)
222
- records.uniq { |record| Support.record_identity(record.class, record.id) }
223
- end
224
74
 
225
75
  #: (untyped, untyped) -> Array[String]
226
76
  def edge_key(owner_class, reflection)
@@ -6,9 +6,23 @@ module PaperTrailDiff
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
+ # first version names.
10
+ #
11
+ # A transaction takes effect where it saved its last version (see
12
+ # TransactionEnds), so one that crosses a window's start is reported in
13
+ # the window, with what it saved before the start, and one that crosses
14
+ # the end in a later window. A history read from the window's start holds
15
+ # neither the versions the first saved before it, nor the records only
16
+ # those versions name, such as one it destroyed there. So a window is
17
+ # read from where the earliest of them began, its `reach`, which its
18
+ # reader asks before reading (see AssociationTraversal#reach_before).
19
+ # Where the second ends is read from its version table, so nothing past
20
+ # the window's end is.
21
+ #
22
+ # A range of versions reads the same way from its first version's
23
+ # transaction (see TransactionCut), and from where a transaction open
24
+ # there began (`crossing`).
25
+ #: (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) -> void
12
26
  def initialize( # rubocop:disable Metrics/ParameterLists
13
27
  record,
14
28
  root_ids:,
@@ -18,44 +32,34 @@ module PaperTrailDiff
18
32
  start_at:,
19
33
  end_at:,
20
34
  live_records:,
21
- window: false,
22
- opening: []
35
+ window:
23
36
  )
24
- @window = window
25
37
  @record = record
26
38
  @root_ids = root_ids
27
- @root_versions = root_versions
28
39
  @tree = tree
29
40
  @traversal = traversal
30
- @end_at = end_at
31
- @live_records = live_records
32
- start_at = VersionBoundary.transaction_start(root_versions, start_at)
41
+ unless window
42
+ start_at = TransactionCut.range_start(root_versions, start_at)
43
+ start_at = TransactionCut.crossing_start(record.class, traversal, start_at)
44
+ end
33
45
  @records = PreparedRecordIndex.new(start_at, end_at: end_at, live_records: live_records)
34
- @activity = PreparedActivityIndex.new(@records, root_versions, start_at: start_at)
35
- @history = PreparedHistory.new(@records, activity_index: @activity,
36
- opening_transactions: opening)
37
- @edges = PreparedEdgeLoader.new(@records, root_versions, children: @activity.children)
46
+ @activity = PreparedActivityIndex.new(@records, start_at: start_at)
47
+ @history = PreparedHistory.new(@records, activity_index: @activity)
48
+ @edges = PreparedEdgeLoader.new(@records, children: @activity.children)
38
49
  @prepared = {} #: Hash[untyped, Set[String]]
39
50
  end
40
51
 
41
52
  #: () -> PreparedHistory
42
53
  def call
43
- @records.seed(@record.class, @root_ids, @root_versions)
54
+ @records.load(@record.class, @root_ids)
44
55
  load_node(@record.class, @root_ids, @tree, path: '')
45
- opening = @records.opening if @window
46
- return @history unless opening
47
-
48
- reload(*opening)
56
+ @history
49
57
  end
50
58
 
51
59
  private
52
60
 
53
- # @rbs @window: bool
54
- # @rbs @end_at: untyped
55
- # @rbs @live_records: Array[untyped]
56
61
  # @rbs @record: untyped
57
62
  # @rbs @root_ids: Array[untyped]
58
- # @rbs @root_versions: Array[untyped]
59
63
  # @rbs @tree: AssociationTree
60
64
  # @rbs @traversal: AssociationTraversal
61
65
  # @rbs @records: PreparedRecordIndex
@@ -64,28 +68,6 @@ module PaperTrailDiff
64
68
  # @rbs @edges: PreparedEdgeLoader
65
69
  # @rbs @prepared: Hash[untyped, Set[String]]
66
70
 
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
88
-
89
71
  #: (untyped, Array[untyped], AssociationTree, path: String) -> void
90
72
  def load_node(model_class, ids, tree, path:)
91
73
  @activity.register(model_class, ids)
@@ -108,7 +90,9 @@ module PaperTrailDiff
108
90
 
109
91
  # Either part can itself be a through, which has no key of its own to
110
92
  # read, so each part is prepared as what it is. Like an edge, it returns
111
- # every group it holds, not only those of these owners.
93
+ # every group it holds, not only those of these owners. Under
94
+ # `source_type:`, only targets of that type are members, so the path
95
+ # descends into no other.
112
96
  #: (untyped, Array[untyped], untyped) -> Hash[String, Hash[Symbol, untyped]]
113
97
  def prepare_through(owner_class, owner_ids, reflection)
114
98
  through_groups = prepare_reflection(owner_class, owner_ids, reflection.through_reflection)
@@ -116,21 +100,26 @@ module PaperTrailDiff
116
100
  through_groups.each_value do |group|
117
101
  source = prepare_reflection(group.fetch(:model), group.fetch(:ids),
118
102
  reflection.source_reflection)
119
- targets = merge_groups(targets, source)
103
+ targets = merge_groups(targets, of_source_type(reflection, source))
120
104
  end
121
105
  @history.merge_edge(owner_class, reflection, targets)
122
106
  end
123
107
 
108
+ #: (untyped, Hash[String, Hash[Symbol, untyped]]) -> Hash[String, Hash[Symbol, untyped]]
109
+ def of_source_type(reflection, groups)
110
+ groups.select do |_name, group|
111
+ AssociationTraversal.source_type_member?(reflection, group.fetch(:model).name)
112
+ end
113
+ end
114
+
124
115
  #: (untyped, Array[untyped], untyped) -> Hash[String, Hash[Symbol, untyped]]
125
116
  def prepare_edge(owner_class, owner_ids, reflection)
126
117
  missing = unprepared_ids(owner_ids, reflection)
127
118
  return @history.edge(owner_class, reflection) if missing.empty?
128
119
 
129
- groups, memberships = @edges.call(owner_class, missing, reflection)
120
+ groups = @edges.call(owner_class, missing, reflection)
130
121
  @history.merge_edge(owner_class, reflection, groups)
131
- @history.register_habtm(owner_class, reflection, memberships) if memberships
132
122
  mark_prepared(missing, reflection)
133
- mark_unsupported(owner_class, reflection, groups)
134
123
  load_groups(groups)
135
124
  @history.edge(owner_class, reflection)
136
125
  end
@@ -148,13 +137,6 @@ module PaperTrailDiff
148
137
  (@prepared[reflection] ||= Set.new).merge(ids.map(&:to_s))
149
138
  end
150
139
 
151
- #: (untyped, untyped, Hash[String, Hash[Symbol, untyped]]) -> void
152
- def mark_unsupported(owner_class, reflection, groups)
153
- unsupported = reflection.foreign_key.is_a?(Array) ||
154
- groups.values.any? { |group| !versioned?(group.fetch(:model)) }
155
- @history.mark_unsupported(owner_class, reflection) if unsupported
156
- end
157
-
158
140
  #: (Hash[String, Hash[Symbol, untyped]]) -> void
159
141
  def load_groups(groups)
160
142
  groups.each_value do |group|