paper_trail_diff 0.16.0 → 0.17.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 (206) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +566 -1
  3. data/README.md +42 -7
  4. data/docs/architecture.md +7 -5
  5. data/docs/performance.md +86 -38
  6. data/docs/reference.md +309 -92
  7. data/docs/testing.md +240 -32
  8. data/lib/paper_trail_diff/activity_belongs_to_event_applier.rb +2 -16
  9. data/lib/paper_trail_diff/activity_boundary.rb +55 -34
  10. data/lib/paper_trail_diff/activity_child_candidate_loader.rb +46 -57
  11. data/lib/paper_trail_diff/activity_collection_event_applier.rb +3 -13
  12. data/lib/paper_trail_diff/activity_collection_route_updater.rb +1 -1
  13. data/lib/paper_trail_diff/activity_event.rb +33 -21
  14. data/lib/paper_trail_diff/activity_event_record_resolver.rb +16 -116
  15. data/lib/paper_trail_diff/activity_event_route_finder.rb +29 -29
  16. data/lib/paper_trail_diff/activity_event_snapshot_refresher.rb +2 -7
  17. data/lib/paper_trail_diff/activity_history.rb +90 -49
  18. data/lib/paper_trail_diff/activity_range.rb +9 -35
  19. data/lib/paper_trail_diff/activity_root_snapshot_refresher.rb +74 -40
  20. data/lib/paper_trail_diff/activity_root_steps.rb +10 -13
  21. data/lib/paper_trail_diff/activity_scope_filter.rb +77 -0
  22. data/lib/paper_trail_diff/activity_snapshot_delta.rb +12 -15
  23. data/lib/paper_trail_diff/activity_snapshot_sequence.rb +37 -15
  24. data/lib/paper_trail_diff/activity_target_candidate_loader.rb +35 -43
  25. data/lib/paper_trail_diff/activity_timeline_builder.rb +194 -249
  26. data/lib/paper_trail_diff/activity_transaction_grouper.rb +3 -1
  27. data/lib/paper_trail_diff/activity_tree_filter.rb +54 -13
  28. data/lib/paper_trail_diff/activity_version_collector.rb +72 -138
  29. data/lib/paper_trail_diff/activity_view.rb +22 -11
  30. data/lib/paper_trail_diff/analysis.rb +19 -5
  31. data/lib/paper_trail_diff/analysis_batch.rb +16 -58
  32. data/lib/paper_trail_diff/approximate_paths.rb +4 -8
  33. data/lib/paper_trail_diff/association_activity_edge.rb +5 -24
  34. data/lib/paper_trail_diff/association_activity_selection.rb +10 -18
  35. data/lib/paper_trail_diff/association_discovery.rb +1 -6
  36. data/lib/paper_trail_diff/association_key.rb +9 -6
  37. data/lib/paper_trail_diff/association_traversal.rb +49 -6
  38. data/lib/paper_trail_diff/batch_boundary_resolver.rb +33 -42
  39. data/lib/paper_trail_diff/batch_preload.rb +2 -12
  40. data/lib/paper_trail_diff/batched_root_analyses.rb +72 -0
  41. data/lib/paper_trail_diff/batched_root_analyzer.rb +16 -14
  42. data/lib/paper_trail_diff/batched_root_versions.rb +25 -20
  43. data/lib/paper_trail_diff/branch_snapshot_refresher.rb +27 -31
  44. data/lib/paper_trail_diff/change_attribution.rb +314 -0
  45. data/lib/paper_trail_diff/change_row.rb +30 -10
  46. data/lib/paper_trail_diff/comparison_batch.rb +3 -4
  47. data/lib/paper_trail_diff/configuration.rb +1 -1
  48. data/lib/paper_trail_diff/deleted_analysis_batch.rb +22 -37
  49. data/lib/paper_trail_diff/deleted_root_filter.rb +3 -6
  50. data/lib/paper_trail_diff/deprecation.rb +4 -43
  51. data/lib/paper_trail_diff/diagnostics.rb +1 -2
  52. data/lib/paper_trail_diff/direct_record_state_loader.rb +8 -1
  53. data/lib/paper_trail_diff/endpoint.rb +78 -44
  54. data/lib/paper_trail_diff/errors.rb +2 -9
  55. data/lib/paper_trail_diff/fallback_paths.rb +2 -5
  56. data/lib/paper_trail_diff/habtm_transaction_owners.rb +5 -3
  57. data/lib/paper_trail_diff/historical_association_reifier.rb +3 -1
  58. data/lib/paper_trail_diff/historical_filter.rb +40 -15
  59. data/lib/paper_trail_diff/historical_snapshot_store.rb +80 -38
  60. data/lib/paper_trail_diff/instrumentation.rb +0 -8
  61. data/lib/paper_trail_diff/live_endpoint_batch_loader.rb +11 -7
  62. data/lib/paper_trail_diff/live_endpoint_provider.rb +7 -23
  63. data/lib/paper_trail_diff/live_graph_collector.rb +3 -3
  64. data/lib/paper_trail_diff/nested_comparator.rb +46 -19
  65. data/lib/paper_trail_diff/net_change.rb +276 -0
  66. data/lib/paper_trail_diff/own_window.rb +35 -0
  67. data/lib/paper_trail_diff/paper_trail_adapter.rb +40 -45
  68. data/lib/paper_trail_diff/prepared_activity_index.rb +170 -42
  69. data/lib/paper_trail_diff/prepared_child_candidates.rb +58 -0
  70. data/lib/paper_trail_diff/prepared_edge_loader.rb +43 -107
  71. data/lib/paper_trail_diff/prepared_history.rb +20 -31
  72. data/lib/paper_trail_diff/prepared_history_loader.rb +46 -9
  73. data/lib/paper_trail_diff/prepared_record_index.rb +137 -47
  74. data/lib/paper_trail_diff/prepared_snapshot_builder.rb +16 -0
  75. data/lib/paper_trail_diff/prepared_state_association_reader.rb +17 -19
  76. data/lib/paper_trail_diff/read_snapshot.rb +41 -19
  77. data/lib/paper_trail_diff/recorded_changes.rb +10 -11
  78. data/lib/paper_trail_diff/recorded_changes_reader.rb +60 -0
  79. data/lib/paper_trail_diff/recorded_class.rb +14 -19
  80. data/lib/paper_trail_diff/recorded_columns.rb +33 -0
  81. data/lib/paper_trail_diff/root_version_plan.rb +20 -22
  82. data/lib/paper_trail_diff/root_version_selection.rb +39 -29
  83. data/lib/paper_trail_diff/scope_condition.rb +7 -17
  84. data/lib/paper_trail_diff/scope_ranking.rb +16 -22
  85. data/lib/paper_trail_diff/scoped_analysis.rb +27 -4
  86. data/lib/paper_trail_diff/scoped_analysis_builder.rb +4 -3
  87. data/lib/paper_trail_diff/scoped_root_selection.rb +60 -41
  88. data/lib/paper_trail_diff/snapshot_normalizer.rb +18 -3
  89. data/lib/paper_trail_diff/stored_json_values.rb +52 -0
  90. data/lib/paper_trail_diff/stored_time_precision.rb +30 -18
  91. data/lib/paper_trail_diff/stored_values.rb +20 -0
  92. data/lib/paper_trail_diff/support.rb +185 -35
  93. data/lib/paper_trail_diff/time_of_day.rb +39 -0
  94. data/lib/paper_trail_diff/time_range.rb +37 -20
  95. data/lib/paper_trail_diff/time_version_range.rb +5 -10
  96. data/lib/paper_trail_diff/timeline_builder.rb +3 -3
  97. data/lib/paper_trail_diff/timeline_range.rb +18 -12
  98. data/lib/paper_trail_diff/timeline_snapshot_provider.rb +1 -1
  99. data/lib/paper_trail_diff/total_order.rb +55 -0
  100. data/lib/paper_trail_diff/transaction_cut.rb +192 -91
  101. data/lib/paper_trail_diff/traversal_preparer.rb +5 -8
  102. data/lib/paper_trail_diff/value_objects.rb +8 -4
  103. data/lib/paper_trail_diff/version.rb +1 -1
  104. data/lib/paper_trail_diff/version_association_candidate_scope.rb +1 -12
  105. data/lib/paper_trail_diff/version_boundary.rb +37 -21
  106. data/lib/paper_trail_diff/version_query.rb +25 -25
  107. data/lib/paper_trail_diff/version_range.rb +12 -16
  108. data/lib/paper_trail_diff/version_sequence_diagnostics.rb +32 -0
  109. data/lib/paper_trail_diff/version_table.rb +12 -8
  110. data/lib/paper_trail_diff.rb +28 -42
  111. data/sig/generated/paper_trail_diff/activity_belongs_to_event_applier.rbs +0 -8
  112. data/sig/generated/paper_trail_diff/activity_boundary.rbs +39 -17
  113. data/sig/generated/paper_trail_diff/activity_child_candidate_loader.rbs +19 -22
  114. data/sig/generated/paper_trail_diff/activity_collection_event_applier.rbs +0 -5
  115. data/sig/generated/paper_trail_diff/activity_event.rbs +25 -9
  116. data/sig/generated/paper_trail_diff/activity_event_record_resolver.rbs +6 -34
  117. data/sig/generated/paper_trail_diff/activity_event_route_finder.rbs +11 -4
  118. data/sig/generated/paper_trail_diff/activity_event_snapshot_refresher.rbs +0 -3
  119. data/sig/generated/paper_trail_diff/activity_history.rbs +54 -19
  120. data/sig/generated/paper_trail_diff/activity_range.rbs +6 -20
  121. data/sig/generated/paper_trail_diff/activity_root_snapshot_refresher.rbs +36 -2
  122. data/sig/generated/paper_trail_diff/activity_root_steps.rbs +6 -6
  123. data/sig/generated/paper_trail_diff/activity_scope_filter.rbs +51 -0
  124. data/sig/generated/paper_trail_diff/activity_snapshot_delta.rbs +5 -3
  125. data/sig/generated/paper_trail_diff/activity_snapshot_sequence.rbs +21 -3
  126. data/sig/generated/paper_trail_diff/activity_target_candidate_loader.rbs +19 -24
  127. data/sig/generated/paper_trail_diff/activity_timeline_builder.rbs +121 -127
  128. data/sig/generated/paper_trail_diff/activity_tree_filter.rbs +41 -8
  129. data/sig/generated/paper_trail_diff/activity_version_collector.rbs +42 -35
  130. data/sig/generated/paper_trail_diff/activity_view.rbs +16 -8
  131. data/sig/generated/paper_trail_diff/analysis.rbs +15 -6
  132. data/sig/generated/paper_trail_diff/analysis_batch.rbs +6 -24
  133. data/sig/generated/paper_trail_diff/association_activity_edge.rbs +0 -13
  134. data/sig/generated/paper_trail_diff/association_activity_selection.rbs +6 -9
  135. data/sig/generated/paper_trail_diff/association_discovery.rbs +0 -3
  136. data/sig/generated/paper_trail_diff/association_key.rbs +5 -0
  137. data/sig/generated/paper_trail_diff/association_traversal.rbs +26 -2
  138. data/sig/generated/paper_trail_diff/batch_boundary_resolver.rbs +17 -17
  139. data/sig/generated/paper_trail_diff/batch_preload.rbs +0 -4
  140. data/sig/generated/paper_trail_diff/batched_root_analyses.rbs +47 -0
  141. data/sig/generated/paper_trail_diff/batched_root_analyzer.rbs +9 -8
  142. data/sig/generated/paper_trail_diff/batched_root_versions.rbs +10 -6
  143. data/sig/generated/paper_trail_diff/branch_snapshot_refresher.rbs +11 -5
  144. data/sig/generated/paper_trail_diff/change_attribution.rbs +185 -0
  145. data/sig/generated/paper_trail_diff/change_row.rbs +22 -7
  146. data/sig/generated/paper_trail_diff/deleted_analysis_batch.rbs +11 -12
  147. data/sig/generated/paper_trail_diff/deleted_root_filter.rbs +0 -2
  148. data/sig/generated/paper_trail_diff/deprecation.rbs +4 -24
  149. data/sig/generated/paper_trail_diff/direct_record_state_loader.rbs +4 -0
  150. data/sig/generated/paper_trail_diff/endpoint.rbs +40 -16
  151. data/sig/generated/paper_trail_diff/errors.rbs +2 -11
  152. data/sig/generated/paper_trail_diff/habtm_transaction_owners.rbs +2 -1
  153. data/sig/generated/paper_trail_diff/historical_filter.rbs +20 -9
  154. data/sig/generated/paper_trail_diff/historical_snapshot_store.rbs +40 -10
  155. data/sig/generated/paper_trail_diff/instrumentation.rbs +0 -3
  156. data/sig/generated/paper_trail_diff/live_endpoint_batch_loader.rbs +3 -0
  157. data/sig/generated/paper_trail_diff/live_endpoint_provider.rbs +5 -8
  158. data/sig/generated/paper_trail_diff/live_graph_collector.rbs +2 -2
  159. data/sig/generated/paper_trail_diff/nested_comparator.rbs +20 -4
  160. data/sig/generated/paper_trail_diff/net_change.rbs +168 -0
  161. data/sig/generated/paper_trail_diff/own_window.rbs +22 -0
  162. data/sig/generated/paper_trail_diff/paper_trail_adapter.rbs +9 -6
  163. data/sig/generated/paper_trail_diff/prepared_activity_index.rbs +103 -14
  164. data/sig/generated/paper_trail_diff/prepared_child_candidates.rbs +39 -0
  165. data/sig/generated/paper_trail_diff/prepared_edge_loader.rbs +18 -35
  166. data/sig/generated/paper_trail_diff/prepared_history.rbs +9 -12
  167. data/sig/generated/paper_trail_diff/prepared_history_loader.rbs +28 -3
  168. data/sig/generated/paper_trail_diff/prepared_record_index.rbs +68 -14
  169. data/sig/generated/paper_trail_diff/prepared_snapshot_builder.rbs +12 -0
  170. data/sig/generated/paper_trail_diff/prepared_state_association_reader.rbs +6 -7
  171. data/sig/generated/paper_trail_diff/read_snapshot.rbs +27 -8
  172. data/sig/generated/paper_trail_diff/recorded_changes.rbs +4 -2
  173. data/sig/generated/paper_trail_diff/recorded_changes_reader.rbs +41 -0
  174. data/sig/generated/paper_trail_diff/recorded_class.rbs +7 -6
  175. data/sig/generated/paper_trail_diff/recorded_columns.rbs +21 -0
  176. data/sig/generated/paper_trail_diff/root_version_plan.rbs +8 -4
  177. data/sig/generated/paper_trail_diff/root_version_selection.rbs +27 -13
  178. data/sig/generated/paper_trail_diff/scope_condition.rbs +0 -2
  179. data/sig/generated/paper_trail_diff/scope_ranking.rbs +10 -10
  180. data/sig/generated/paper_trail_diff/scoped_analysis.rbs +19 -0
  181. data/sig/generated/paper_trail_diff/scoped_analysis_builder.rbs +2 -2
  182. data/sig/generated/paper_trail_diff/scoped_root_selection.rbs +37 -14
  183. data/sig/generated/paper_trail_diff/snapshot_normalizer.rbs +7 -0
  184. data/sig/generated/paper_trail_diff/stored_json_values.rbs +35 -0
  185. data/sig/generated/paper_trail_diff/stored_time_precision.rbs +19 -5
  186. data/sig/generated/paper_trail_diff/stored_values.rbs +13 -0
  187. data/sig/generated/paper_trail_diff/support.rbs +120 -23
  188. data/sig/generated/paper_trail_diff/time_of_day.rbs +21 -0
  189. data/sig/generated/paper_trail_diff/time_range.rbs +15 -8
  190. data/sig/generated/paper_trail_diff/time_version_range.rbs +2 -5
  191. data/sig/generated/paper_trail_diff/timeline_builder.rbs +2 -2
  192. data/sig/generated/paper_trail_diff/timeline_range.rbs +12 -7
  193. data/sig/generated/paper_trail_diff/total_order.rbs +32 -0
  194. data/sig/generated/paper_trail_diff/transaction_cut.rbs +111 -52
  195. data/sig/generated/paper_trail_diff/traversal_preparer.rbs +2 -2
  196. data/sig/generated/paper_trail_diff/value_objects.rbs +5 -1
  197. data/sig/generated/paper_trail_diff/version_association_candidate_scope.rbs +0 -7
  198. data/sig/generated/paper_trail_diff/version_boundary.rbs +15 -8
  199. data/sig/generated/paper_trail_diff/version_query.rbs +17 -10
  200. data/sig/generated/paper_trail_diff/version_range.rbs +6 -9
  201. data/sig/generated/paper_trail_diff/version_sequence_diagnostics.rbs +13 -0
  202. data/sig/generated/paper_trail_diff/version_table.rbs +3 -0
  203. data/sig/generated/paper_trail_diff.rbs +16 -16
  204. metadata +28 -6
  205. data/lib/paper_trail_diff/preloaded_endpoint_batch_loader.rb +0 -60
  206. data/sig/generated/paper_trail_diff/preloaded_endpoint_batch_loader.rbs +0 -32
@@ -11,16 +11,21 @@ module PaperTrailDiff
11
11
  # and the snapshots read it from there. ActivityTreeFilter leaves out the
12
12
  # events of a candidate that was outside the tree throughout.
13
13
  class PreparedActivityIndex
14
+ # The children the history's owners had, which its edge loader reads
15
+ # first for most of them.
16
+ attr_reader :children #: PreparedChildCandidates
17
+
14
18
  #: (PreparedRecordIndex, Array[untyped], start_at: untyped) -> void
15
19
  def initialize(records, root_versions, start_at:)
16
20
  @records = records
17
- @range = ActivityRange.new(start_at, nil)
18
- @roots = root_versions.group_by { |version| [version.item_type, version.item_id.to_s] }
21
+ @start_at = start_at
22
+ @children = PreparedChildCandidates.new(start_at)
23
+ @roots = root_versions.group_by { |version| Endpoint.identity(version) }
19
24
  @nodes = {} #: Hash[String, Array[untyped]]
20
25
  # Rows are cached by reflection, which a subclass shares with its base.
21
- @children = {} #: Hash[untyped, untyped]
22
26
  @targets = {} #: Hash[untyped, untyped]
23
27
  @rows = {} #: Hash[untyped, Hash[String, Array[untyped]]]
28
+ @conditions = ScopeCondition::Cache.new
24
29
  end
25
30
 
26
31
  #: (untyped, Array[untyped]) -> void
@@ -32,7 +37,7 @@ module PaperTrailDiff
32
37
  #: (untyped, Array[untyped]) -> Array[untyped]
33
38
  def versions(model, ids)
34
39
  ids.flat_map do |id|
35
- @roots[[model.base_class.name, id.to_s]] || @records.versions_for_id(model, id)
40
+ @roots[Support.record_identity(model, id)] || @records.versions_for_id(model, id)
36
41
  end
37
42
  end
38
43
 
@@ -57,38 +62,48 @@ module PaperTrailDiff
57
62
  end
58
63
  end
59
64
 
60
- # The versions of a through's join rows that are on its path. Under
61
- # `source_type:`, a row is on it only while its type column names that
62
- # class, so only the versions it had that type before or after count. A
63
- # version saved before the type column existed has no type to read.
64
- #: (Hash[Symbol, untyped], untyped) -> Array[untyped]
65
- def join_versions(group, reflection)
65
+ # The versions of a through's join rows that are on its path (see
66
+ # ThroughPath). A row's versions in one transaction are judged together, by
67
+ # its states before the transaction and after it, as ActivityTreeFilter
68
+ # judges a record; a version saved outside a transaction is judged alone.
69
+ # A row created under another owner and moved here later, one that left,
70
+ # or one created and destroyed inside one transaction, saved versions that
71
+ # change nothing these `owners` show. As events they would open steps of
72
+ # their own, and inside a transaction, where a change surfaces one event
73
+ # late, take the credit for another record's change.
74
+ #: (Hash[Symbol, untyped], Hash[Symbol, untyped], untyped) -> Array[untyped]
75
+ def join_versions(owners, group, reflection)
76
+ path = ThroughPath.new(owners, reflection, @conditions)
66
77
  versions = group.fetch(:versions)
67
- source_type = reflection.options[:source_type]
68
- return versions unless source_type
78
+ return versions if path.open?
69
79
 
70
- column = reflection.foreign_type
71
- versions.select do |version|
72
- @records.states_around(group.fetch(:model), version.item_id, version).any? do |state|
73
- state.attributes[column] == source_type.to_s
74
- end
75
- end
80
+ runs = versions.group_by { |version| join_run(version) }.values
81
+ runs.select { |run| on_path?(group.fetch(:model), run, path) }.flat_map(&:itself)
76
82
  end
77
83
 
84
+ # Only the children the history loaded. Snapshots read children only under
85
+ # an owner the history prepared, and it loaded every child of those. An
86
+ # owner reached another way, such as a belongs_to target loaded for a
87
+ # different path, can have children the history never loaded, which no
88
+ # snapshot holds.
78
89
  #: (untyped, Array[untyped], untyped, ActivityRange) -> Array[untyped]
79
90
  def child_ids(model, ids, reflection, range)
80
- historical, live = @children[reflection] ||= child_rows(model, reflection)
81
- empty = [] #: Array[untyped]
82
- selected = historical_ids(reflection.klass, ids, historical, range)
83
- selected | ids.flat_map { |id| live.fetch(id.to_s, empty) }
91
+ # An owner the edges did not read is read with every other one this
92
+ # history holds, so that the next needs nothing more.
93
+ historical, live = @children.call(model, ids, reflection) { node_ids(model) }
94
+ child = reflection.klass
95
+ # An owner with no live children reads as nil, which names no record.
96
+ current = ids.flat_map { |id| live[id.to_s] }.select { |id| loaded?(child, id) }
97
+ historical_ids(child, ids, historical, range) | current
84
98
  end
85
99
 
86
100
  # The `belongs_to` targets each owner pointed at from the range's start
87
- # onward, plus the live row.
101
+ # onward, plus the live row and those its recorded states name.
88
102
  #: (untyped, Array[untyped], untyped, ActivityRange) -> Array[ActivityTargetCandidateLoader::Row]
89
103
  def target_rows(model, ids, reflection, range)
90
104
  historical, live = @targets[reflection] ||= target_candidates(model, reflection)
91
- eligible_rows(model, rows_for(historical, ids), range) + rows_for(live, ids)
105
+ eligible_rows(model, rows_for(historical, ids), range) + rows_for(live, ids) +
106
+ recorded_target_rows(model, ids, reflection)
92
107
  end
93
108
 
94
109
  #: (untyped, untyped) -> bool
@@ -107,12 +122,48 @@ module PaperTrailDiff
107
122
  private
108
123
 
109
124
  # @rbs @records: PreparedRecordIndex
110
- # @rbs @range: ActivityRange
125
+ # @rbs @start_at: untyped
111
126
  # @rbs @roots: Hash[Array[String], Array[untyped]]
112
127
  # @rbs @nodes: Hash[String, Array[untyped]]
113
- # @rbs @children: Hash[untyped, untyped]
114
128
  # @rbs @targets: Hash[untyped, untyped]
115
129
  # @rbs @rows: Hash[untyped, Hash[String, Array[untyped]]]
130
+ # @rbs @conditions: ScopeCondition::Cache
131
+
132
+ # The versions one row saved in one transaction, or alone.
133
+ #: (untyped) -> Array[untyped]
134
+ def join_run(version)
135
+ [version.item_id, ActivityTransactionGroups.transaction_key(version) || version]
136
+ end
137
+
138
+ # A run's versions are in the order they were saved, as the row's series
139
+ # holds them.
140
+ #: (untyped, Array[untyped], ThroughPath) -> bool
141
+ def on_path?(model, run, path)
142
+ first = run.fetch(0)
143
+ @records.typed_states_across(model, first.item_id, first, run.fetch(-1))
144
+ .any? { |attributes| path.include?(attributes) }
145
+ end
146
+
147
+ # The targets the owners' recorded states point at. PT-AT names a target
148
+ # only in the rows its owner's versions wrote, so a key changed without a
149
+ # version, such as one `has_paper_trail only:` leaves out, names its target
150
+ # only in the state the owner's next version recorded. Prepared history
151
+ # reads that state, so its snapshots show the target, and the target's
152
+ # versions are events too. A key of two columns is read per record instead.
153
+ #: (untyped, Array[untyped], untyped) -> Array[ActivityTargetCandidateLoader::Row]
154
+ def recorded_target_rows(model, ids, reflection)
155
+ key = reflection.foreign_key
156
+ return [] if key.is_a?(Array)
157
+
158
+ ids.flat_map do |id|
159
+ targets = @records.attribute_values_for(model, [id], key)
160
+ types = (@records.attribute_values_for(model, [id], reflection.foreign_type) if
161
+ reflection.polymorphic?) || []
162
+ targets.zip(types).filter_map do |target, type|
163
+ ActivityTargetCandidateLoader::Row.new(id, target, type) unless target.nil?
164
+ end
165
+ end
166
+ end
116
167
 
117
168
  #: (untyped, Array[untyped]) -> bool
118
169
  def member_class?(model, classes)
@@ -125,6 +176,8 @@ module PaperTrailDiff
125
176
  ids.flat_map do |id|
126
177
  historical.fetch(id.to_s, empty).filter_map do |row|
127
178
  _owner, child_id, timestamp, event = row
179
+ next unless loaded?(model, child_id)
180
+
128
181
  child_id if eligible_version?(model, child_id, timestamp, event, range)
129
182
  end
130
183
  end
@@ -140,16 +193,6 @@ module PaperTrailDiff
140
193
  end
141
194
  end
142
195
 
143
- #: (untyped, untyped) -> untyped
144
- def child_rows(model, reflection)
145
- loader = ActivityChildCandidateLoader.new(
146
- model, node_ids(model), reflection, @range
147
- )
148
- [loader.historical_rows.group_by { |row| row.first.to_s },
149
- loader.current_pairs.group_by { |row| row.first.to_s }
150
- .transform_values { |rows| rows.map(&:last) }]
151
- end
152
-
153
196
  #: (Hash[String, Array[ActivityTargetCandidateLoader::Row]], Array[untyped]) -> Array[ActivityTargetCandidateLoader::Row]
154
197
  def rows_for(grouped, ids)
155
198
  ids.flat_map { |id| grouped[id.to_s] || [] }
@@ -164,7 +207,7 @@ module PaperTrailDiff
164
207
 
165
208
  #: (untyped, untyped) -> untyped
166
209
  def target_candidates(model, reflection)
167
- loader = ActivityTargetCandidateLoader.new(model, node_ids(model), reflection, @range)
210
+ loader = ActivityTargetCandidateLoader.new(model, node_ids(model), reflection, @start_at)
168
211
  [loader.historical_rows.group_by { |row| row.owner_id.to_s },
169
212
  loader.current_rows.group_by { |row| row.owner_id.to_s }]
170
213
  end
@@ -172,6 +215,9 @@ module PaperTrailDiff
172
215
  # Mirrors VersionAssociationCandidateScope's before-window rule. The loaded
173
216
  # series includes changes to other owners too, so an old membership cannot
174
217
  # survive a later pre-window reassignment just because that owner is absent.
218
+ # A range starting at a version starts before that version's transaction,
219
+ # as the scope's start does (see VersionBoundary.transaction_start), so a
220
+ # membership that ended earlier in that transaction still held there.
175
221
  #: (untyped, untyped, untyped, String?, ActivityRange) -> bool
176
222
  def eligible_version?(model, id, timestamp, event, range)
177
223
  return true if timestamp >= range.start_time
@@ -179,15 +225,17 @@ module PaperTrailDiff
179
225
 
180
226
  later = @records.versions_for_id(model, id)
181
227
  .bsearch { |version| version.created_at > timestamp }
182
- !later || later.created_at >= range.start_time
228
+ !later || later.created_at >= range.start_time || range.opening?(later)
183
229
  end
184
230
 
231
+ # Each statement reads the rows of 500 transactions.
185
232
  #: (untyped, untyped) -> Hash[String, Array[untyped]]
186
233
  def load_habtm_rows(model, reflection)
187
- model.paper_trail.version_association_class.where(
188
- version_id: transaction_ids(versions(model, node_ids(model))),
189
- foreign_key_name: reflection.name
190
- ).group_by { |row| row.version_id.to_s }
234
+ rows = model.paper_trail.version_association_class.where(foreign_key_name: reflection.name)
235
+ transaction_ids(versions(model, node_ids(model)))
236
+ .each_slice(Support::IDS_PER_STATEMENT)
237
+ .flat_map { |slice| rows.where(version_id: slice) }
238
+ .group_by { |row| row.version_id.to_s }
191
239
  end
192
240
 
193
241
  # A version without a transaction id names no rows, so it can stay in.
@@ -195,5 +243,85 @@ module PaperTrailDiff
195
243
  def transaction_ids(versions)
196
244
  versions.map { |version| VersionBoundary.transaction_id(version) }.uniq
197
245
  end
246
+
247
+ # Whether one state of a through's join row puts it on the through's path.
248
+ # The row must join one of the owners as the join association reads it: by
249
+ # its key, by the owner's type under `as:`, by its class when the
250
+ # association is to a subclass, and by the association's scope when the
251
+ # gem can read it, unranked, as a through reads its parts. A through that
252
+ # goes through a belongs_to, or through another through, is joined by the
253
+ # owner's key or by rows further down, which a row's own state cannot
254
+ # show. Under `source_type:`, the row's type column must name that class;
255
+ # a version saved before the column existed has no type to read.
256
+ class ThroughPath
257
+ #: (Hash[Symbol, untyped], untyped, ScopeCondition::Cache) -> void
258
+ def initialize(owners, reflection, conditions)
259
+ @reflection = reflection
260
+ @through = reflection.through_reflection
261
+ @keyed = keyed?
262
+ @ids = owners.fetch(:ids).to_set(&:to_s)
263
+ @owner_types = owner_types(owners.fetch(:model))
264
+ @conditions = conditions
265
+ end
266
+
267
+ # Whether every state of a row is on the path, so that none need be read.
268
+ #: () -> bool
269
+ def open?
270
+ !@keyed && @reflection.options[:source_type].nil?
271
+ end
272
+
273
+ #: (Hash[untyped, untyped]) -> bool
274
+ def include?(attributes)
275
+ (!@keyed || joined?(attributes)) && typed?(attributes)
276
+ end
277
+
278
+ private
279
+
280
+ # @rbs @reflection: untyped
281
+ # @rbs @through: untyped
282
+ # @rbs @keyed: bool
283
+ # @rbs @ids: Set[String]
284
+ # @rbs @owner_types: Array[String]?
285
+ # @rbs @conditions: ScopeCondition::Cache
286
+
287
+ #: () -> bool
288
+ def keyed?
289
+ !@through.through_reflection? && !@through.belongs_to? &&
290
+ !@through.foreign_key.is_a?(Array)
291
+ end
292
+
293
+ #: (Hash[untyped, untyped]) -> bool
294
+ def joined?(attributes)
295
+ @ids.include?(attributes.fetch(@through.foreign_key).to_s) && owner_type?(attributes) &&
296
+ AssociationTraversal.sti_member?(@through.klass, attributes) && in_scope?(attributes)
297
+ end
298
+
299
+ # The names an `as:` row's type column may hold for an owner.
300
+ #: (untyped) -> Array[String]?
301
+ def owner_types(model)
302
+ return unless @through.type
303
+
304
+ [model.name, model.base_class.name, @through.active_record.name]
305
+ end
306
+
307
+ #: (Hash[untyped, untyped]) -> bool
308
+ def owner_type?(attributes)
309
+ types = @owner_types
310
+ !types || types.include?(attributes.fetch(@through.type))
311
+ end
312
+
313
+ #: (Hash[untyped, untyped]) -> bool
314
+ def in_scope?(attributes)
315
+ condition = @conditions.call(@through, ranked: false)
316
+ !condition || condition.matches?(attributes)
317
+ end
318
+
319
+ # As the through's snapshots read a join row (see
320
+ # PreparedStateAssociationReader#typed_join?).
321
+ #: (Hash[untyped, untyped]) -> bool
322
+ def typed?(attributes)
323
+ AssociationTraversal.source_type_member?(@reflection, attributes[@reflection.foreign_type])
324
+ end
325
+ end
198
326
  end
199
327
  end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ module PaperTrailDiff
5
+ # The children each owner of a has_many or has_one had from a history's
6
+ # start onward, and has now, read once for each owner across the history
7
+ # (see ActivityChildCandidateLoader). Preparation reads them for the owners
8
+ # it loads, and activity then finds them here.
9
+ class PreparedChildCandidates
10
+ # What one class's association has read: the rows recorded under each
11
+ # owner and its live children's ids, by the owner's id.
12
+ class Read
13
+ attr_reader :historical #: Hash[String, Array[Array[untyped]]]
14
+ attr_reader :live #: Hash[String, Array[untyped]]
15
+
16
+ #: () -> void
17
+ def initialize
18
+ @historical = {}
19
+ @live = {}
20
+ @owners = Set.new #: Set[String]
21
+ end
22
+
23
+ #: (Array[untyped]) -> Array[untyped]
24
+ def unread(ids)
25
+ ids.reject { |id| @owners.include?(id.to_s) }
26
+ end
27
+
28
+ #: (Array[untyped], ActivityChildCandidateLoader) -> void
29
+ def add(owners, loader)
30
+ loader.historical_rows.each { |row| (@historical[row.first.to_s] ||= []) << row }
31
+ loader.current_pairs.each { |owner, child| (@live[owner.to_s] ||= []) << child }
32
+ owners.each { |id| @owners << id.to_s }
33
+ end
34
+ end
35
+
36
+ #: (untyped) -> void
37
+ def initialize(start_at)
38
+ @start_at = start_at
39
+ @read = {} #: Hash[Array[untyped], Read]
40
+ end
41
+
42
+ # The owners not read yet are read together, with the others the block
43
+ # names, so that owners asked about later need none. Each owner class
44
+ # reads its own, as a polymorphic row can store the class's name.
45
+ #: (untyped, Array[untyped], untyped) ?{ () -> Array[untyped] } -> [Hash[String, Array[Array[untyped]]], Hash[String, Array[untyped]]]
46
+ def call(model, ids, reflection)
47
+ read = @read[[model, reflection]] ||= Read.new
48
+ unless read.unread(ids).empty?
49
+ owners = read.unread(block_given? ? yield | ids : ids)
50
+ read.add(owners, ActivityChildCandidateLoader.new(model, owners, reflection, @start_at))
51
+ end
52
+ [read.historical, read.live]
53
+ end
54
+
55
+ # @rbs @start_at: untyped
56
+ # @rbs @read: Hash[Array[untyped], Read]
57
+ end
58
+ end
@@ -4,11 +4,14 @@
4
4
  module PaperTrailDiff
5
5
  # Loads every historical identity that can participate in one association edge.
6
6
  class PreparedEdgeLoader
7
- #: (PreparedRecordIndex, Array[untyped], ?start_at: untyped) -> void
8
- def initialize(records, root_versions, start_at: root_versions.first&.created_at)
7
+ # A history shares `children` with its activity index, which reads the
8
+ # children this finds again.
9
+ #: (PreparedRecordIndex, Array[untyped], ?start_at: untyped, ?children: PreparedChildCandidates) -> void
10
+ def initialize(records, root_versions, start_at: root_versions.first&.created_at,
11
+ children: PreparedChildCandidates.new(start_at))
9
12
  @records = records
10
13
  @transaction_ids = root_versions.filter_map(&:transaction_id)
11
- @start_at = start_at
14
+ @children = children
12
15
  end
13
16
 
14
17
  # HABTM alone also returns each root transaction's members.
@@ -25,37 +28,40 @@ module PaperTrailDiff
25
28
 
26
29
  # @rbs @records: PreparedRecordIndex
27
30
  # @rbs @transaction_ids: Array[untyped]
28
- # @rbs @start_at: untyped
31
+ # @rbs @children: PreparedChildCandidates
29
32
 
30
33
  #: (untyped, Array[untyped], untyped) -> Hash[String, Hash[Symbol, untyped]]
31
34
  def belongs_to_groups(owner_class, owner_ids, reflection)
32
- key = scalar_foreign_key(reflection)
35
+ key = AssociationKey.scalar_foreign_key(reflection)
33
36
  return {} unless key
34
- unless reflection.polymorphic?
35
- return group_for(reflection.klass,
36
- @records.attribute_values_for(owner_class, owner_ids, key))
37
- end
37
+ return polymorphic_groups(owner_class, owner_ids, reflection, key) if reflection.polymorphic?
38
+
39
+ group_for(reflection.klass, @records.attribute_values_for(owner_class, owner_ids, key))
40
+ end
38
41
 
42
+ # Grouping only decides what to preload, so a type that names no class is
43
+ # left to raise where that state is read.
44
+ #: (untyped, Array[untyped], untyped, String) -> Hash[String, Hash[Symbol, untyped]]
45
+ def polymorphic_groups(owner_class, owner_ids, reflection, key)
39
46
  grouped = {} #: Hash[String, Hash[Symbol, untyped]]
40
- # Grouping only decides what to preload, so a type that names no class is
41
- # left to raise where that state is read.
42
47
  @records.records_for(owner_class, owner_ids).each do |owner|
43
48
  id = owner.public_send(key)
44
49
  type = owner.public_send(reflection.foreign_type).to_s
45
50
  model_class = AssociationTraversal.named_class(type)
46
51
  add_identity(grouped, model_class, id) if model_class && id
47
52
  end
48
- grouped
53
+ grouped.transform_values { |group| group.merge(ids: group.fetch(:ids).uniq) }
49
54
  end
50
55
 
56
+ # Each owner's children, recorded or live, each once.
51
57
  #: (untyped, Array[untyped], untyped) -> Hash[String, Hash[Symbol, untyped]]
52
58
  def child_groups(owner_class, owner_ids, reflection)
53
- key = scalar_foreign_key(reflection)
54
- return {} unless key
55
-
56
- historical = historical_child_ids(owner_class, owner_ids, reflection, key)
57
- live = live_child_ids(owner_class, owner_ids, reflection, key)
58
- owners = merge_owners(historical, live)
59
+ historical, live = @children.call(owner_class, owner_ids, reflection)
60
+ empty = [] #: Array[untyped]
61
+ owners = owner_ids.to_h do |id|
62
+ key = id.to_s
63
+ [key, historical.fetch(key, empty).map { |row| row.fetch(1) } | live.fetch(key, empty)]
64
+ end
59
65
  group_for(reflection.klass, owners.values.flatten, owners: owners)
60
66
  end
61
67
 
@@ -64,82 +70,36 @@ module PaperTrailDiff
64
70
  #: (untyped, untyped) -> [Hash[String, Hash[Symbol, untyped]], Hash[String, Array[untyped]]]
65
71
  def habtm_groups(owner_class, reflection)
66
72
  rows = habtm_rows(owner_class, reflection)
67
- shared = HabtmTransactionOwners.new(owner_class, reflection).shared(rows.map(&:version_id))
68
- memberships = @transaction_ids.to_h do |transaction_id|
69
- [transaction_id.to_s, membership_ids(rows, transaction_id)]
73
+ recorded = rows.group_by { |row| row.version_id.to_s }
74
+ shared = HabtmTransactionOwners.new(owner_class, reflection).shared(recorded.keys)
75
+ [group_for(reflection.klass, rows.map(&:foreign_key_id)),
76
+ memberships(recorded).except(*shared)]
77
+ end
78
+
79
+ # Each root transaction's members, from its rows grouped by transaction.
80
+ #: (Hash[String, Array[untyped]]) -> Hash[String, Array[untyped]]
81
+ def memberships(recorded)
82
+ empty = [] #: Array[untyped]
83
+ @transaction_ids.to_h do |transaction_id|
84
+ key = transaction_id.to_s
85
+ [key, recorded.fetch(key, empty).map(&:foreign_key_id)]
70
86
  end
71
- [group_for(reflection.klass, rows.map(&:foreign_key_id)), memberships.except(*shared)]
72
87
  end
73
88
 
89
+ # Each statement reads up to 500 of the root transactions: SQL Server
90
+ # compiles a long list of ids slowly.
74
91
  #: (untyped, untyped) -> Array[untyped]
75
92
  def habtm_rows(owner_class, reflection)
76
- owner_class.paper_trail.version_association_class.where(
77
- version_id: @transaction_ids,
78
- foreign_key_name: reflection.name
79
- )
80
- end
81
-
82
- #: (Array[untyped], untyped) -> Array[untyped]
83
- def membership_ids(rows, transaction_id)
84
- rows.filter_map do |row|
85
- row.foreign_key_id if row.version_id.to_s == transaction_id.to_s
86
- end
87
- end
88
-
89
- #: (untyped, Array[untyped], untyped, String) -> Hash[String, Array[untyped]]
90
- def historical_child_ids(
91
- owner_class,
92
- owner_ids,
93
- reflection,
94
- foreign_key
95
- )
96
- version_class = owner_class.paper_trail.version_class
97
- association_class = owner_class.paper_trail.version_association_class
98
- relation = historical_child_relation(owner_class, owner_ids, reflection, foreign_key)
99
- pairs = child_candidate_scope(version_class).call(relation).pluck(
100
- association_class.arel_table[:foreign_key_id],
101
- version_class.arel_table[:item_id]
102
- )
103
- group_ids_by_owner(typed_child_ids(reflection.klass, pairs))
104
- end
105
-
106
- #: (untyped, Array[Array[untyped]]) -> Array[Array[untyped]]
107
- def typed_child_ids(model_class, pairs)
108
- pairs.map { |owner_id, child_id| [owner_id, Support.record_id(model_class, child_id)] }
109
- end
110
-
111
- #: (untyped, Array[untyped], untyped, String) -> untyped
112
- def historical_child_relation(owner_class, owner_ids, reflection, foreign_key)
113
- version_class = owner_class.paper_trail.version_class
114
93
  associations = owner_class.paper_trail.version_association_class
115
- VersionTable.joined(associations, version_class).where(
116
- foreign_key_name: foreign_key,
117
- foreign_key_id: owner_ids,
118
- foreign_type: parent_types(owner_class)
119
- ).where(version_class.table_name => { item_type: reflection.klass.base_class.name })
120
- end
121
-
122
- #: (untyped) -> VersionAssociationCandidateScope
123
- def child_candidate_scope(version_class)
124
- VersionAssociationCandidateScope.new(
125
- version_class,
126
- @start_at
127
- )
128
- end
129
-
130
- #: (untyped, Array[untyped], untyped, String) -> Hash[String, Array[untyped]]
131
- def live_child_ids(owner_class, owner_ids, reflection, foreign_key)
132
- # Every row of the table, as history reads them: an STI subclass's
133
- # siblings are left out where children are resolved.
134
- relation = reflection.klass.base_class.unscoped.where(foreign_key => owner_ids)
135
- relation = relation.where(reflection.type => parent_types(owner_class)) if reflection.type
136
- group_ids_by_owner(relation.pluck(foreign_key, reflection.klass.primary_key))
94
+ @transaction_ids.uniq.each_slice(Support::IDS_PER_STATEMENT).flat_map do |slice|
95
+ associations.where(version_id: slice, foreign_key_name: reflection.name)
96
+ end
137
97
  end
138
98
 
139
99
  #: (Hash[String, Hash[Symbol, untyped]], untyped, untyped) -> void
140
100
  def add_identity(groups, model_class, id)
141
101
  group = groups[model_class.name.to_s] ||= { model: model_class, ids: [] }
142
- group.fetch(:ids) << id unless group.fetch(:ids).include?(id)
102
+ group.fetch(:ids) << id
143
103
  end
144
104
 
145
105
  #: (untyped, Array[untyped], ?owners: Hash[String, Array[untyped]]?) -> Hash[String, Hash[Symbol, untyped]]
@@ -151,29 +111,5 @@ module PaperTrailDiff
151
111
  group[:owners] = owners if owners
152
112
  { model_class.name => group }
153
113
  end
154
-
155
- # A set rather than a scan, so that a wide edge stays linear in its width.
156
- #: (Array[Array[untyped]]) -> Hash[String, Array[untyped]]
157
- def group_ids_by_owner(pairs)
158
- grouped = {} #: Hash[String, Set[untyped]]
159
- pairs.each { |owner_id, child_id| (grouped[owner_id.to_s] ||= Set.new) << child_id }
160
- grouped.transform_values(&:to_a)
161
- end
162
-
163
- #: (Hash[String, Array[untyped]], Hash[String, Array[untyped]]) -> Hash[String, Array[untyped]]
164
- def merge_owners(left, right)
165
- left.merge(right) { |_owner, first, second| first | second }
166
- end
167
-
168
- #: (untyped) -> String?
169
- def scalar_foreign_key(reflection)
170
- key = reflection.foreign_key
171
- key unless key.is_a?(Array)
172
- end
173
-
174
- #: (untyped) -> Array[String?]
175
- def parent_types(owner_class)
176
- [nil, '', owner_class.name, owner_class.base_class.name]
177
- end
178
114
  end
179
115
  end
@@ -6,17 +6,15 @@ module PaperTrailDiff
6
6
  class PreparedHistory
7
7
  attr_reader :activity_index #: PreparedActivityIndex?
8
8
 
9
- DIRECT_RESOLVERS = {
10
- belongs_to: :resolve_belongs_to,
11
- has_one: :resolve_children,
12
- has_many: :resolve_children,
13
- has_and_belongs_to_many: :habtm_records
14
- }.freeze
15
- private_constant :DIRECT_RESOLVERS
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
- resolver = DIRECT_RESOLVERS.fetch(reflection.macro)
101
- send(resolver, record, reflection, boundary, habtm_boundary, ranked)
102
- end
103
-
104
- #: (untyped, untyped, untyped, untyped, bool) -> Array[untyped]?
105
- def resolve_belongs_to(record, reflection, boundary, _habtm_boundary, ranked)
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| [record.class.base_class.name, record.id.to_s] }
222
+ records.uniq { |record| Support.record_identity(record.class, record.id) }
234
223
  end
235
224
 
236
225
  #: (untyped, untyped) -> Array[String]