paper_trail_diff 0.13.0 → 0.14.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 (121) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +318 -0
  3. data/QUICKSTART.md +9 -6
  4. data/README.md +84 -9
  5. data/docs/architecture.md +32 -9
  6. data/docs/performance.md +106 -13
  7. data/docs/reference.md +242 -29
  8. data/docs/testing.md +55 -9
  9. data/lib/paper_trail_diff/activity_belongs_to_event_applier.rb +19 -9
  10. data/lib/paper_trail_diff/activity_collection_event_applier.rb +11 -1
  11. data/lib/paper_trail_diff/activity_collection_record_updater.rb +16 -2
  12. data/lib/paper_trail_diff/activity_event_record_resolver.rb +20 -1
  13. data/lib/paper_trail_diff/activity_event_route_finder.rb +2 -1
  14. data/lib/paper_trail_diff/activity_event_snapshot_refresher.rb +1 -1
  15. data/lib/paper_trail_diff/activity_range.rb +14 -2
  16. data/lib/paper_trail_diff/activity_relationship.rb +12 -0
  17. data/lib/paper_trail_diff/activity_root_snapshot_refresher.rb +5 -2
  18. data/lib/paper_trail_diff/activity_target_candidate_loader.rb +131 -0
  19. data/lib/paper_trail_diff/activity_timeline_builder.rb +30 -24
  20. data/lib/paper_trail_diff/activity_version_collector.rb +31 -20
  21. data/lib/paper_trail_diff/association_activity_edge.rb +236 -0
  22. data/lib/paper_trail_diff/association_activity_selection.rb +197 -0
  23. data/lib/paper_trail_diff/association_discovery.rb +8 -13
  24. data/lib/paper_trail_diff/association_traversal.rb +169 -45
  25. data/lib/paper_trail_diff/batch_safety.rb +23 -15
  26. data/lib/paper_trail_diff/batched_root_versions.rb +21 -39
  27. data/lib/paper_trail_diff/collection_comparator.rb +18 -20
  28. data/lib/paper_trail_diff/collection_identity_index.rb +8 -12
  29. data/lib/paper_trail_diff/comparison_batch.rb +37 -10
  30. data/lib/paper_trail_diff/deleted_analysis_batch.rb +36 -17
  31. data/lib/paper_trail_diff/diagnostics.rb +26 -8
  32. data/lib/paper_trail_diff/direct_record_state_loader.rb +10 -1
  33. data/lib/paper_trail_diff/errors.rb +2 -1
  34. data/lib/paper_trail_diff/habtm_transaction_owners.rb +68 -0
  35. data/lib/paper_trail_diff/historical_association_reifier.rb +32 -2
  36. data/lib/paper_trail_diff/historical_filter.rb +12 -17
  37. data/lib/paper_trail_diff/historical_snapshot_store.rb +6 -9
  38. data/lib/paper_trail_diff/live_endpoint_batch_loader.rb +35 -2
  39. data/lib/paper_trail_diff/live_graph_collector.rb +1 -1
  40. data/lib/paper_trail_diff/paper_trail_adapter.rb +22 -6
  41. data/lib/paper_trail_diff/preloaded_endpoint_batch_loader.rb +1 -1
  42. data/lib/paper_trail_diff/prepared_activity_index.rb +110 -36
  43. data/lib/paper_trail_diff/prepared_edge_loader.rb +29 -45
  44. data/lib/paper_trail_diff/prepared_history.rb +63 -54
  45. data/lib/paper_trail_diff/prepared_history_loader.rb +33 -43
  46. data/lib/paper_trail_diff/prepared_record_index.rb +26 -0
  47. data/lib/paper_trail_diff/prepared_snapshot_builder.rb +1 -1
  48. data/lib/paper_trail_diff/prepared_state_association_reader.rb +119 -29
  49. data/lib/paper_trail_diff/recorded_changes.rb +83 -0
  50. data/lib/paper_trail_diff/recorded_class.rb +53 -0
  51. data/lib/paper_trail_diff/root_version_plan.rb +10 -1
  52. data/lib/paper_trail_diff/scope_condition.rb +331 -0
  53. data/lib/paper_trail_diff/scope_ranking.rb +196 -0
  54. data/lib/paper_trail_diff/scoped_analysis_builder.rb +4 -3
  55. data/lib/paper_trail_diff/scoped_root_selection.rb +93 -43
  56. data/lib/paper_trail_diff/snapshot.rb +5 -1
  57. data/lib/paper_trail_diff/snapshot_normalizer.rb +50 -47
  58. data/lib/paper_trail_diff/support.rb +9 -4
  59. data/lib/paper_trail_diff/time_activity_timeline_builder.rb +0 -1
  60. data/lib/paper_trail_diff/time_range.rb +9 -13
  61. data/lib/paper_trail_diff/time_version_range.rb +6 -6
  62. data/lib/paper_trail_diff/timeline_range.rb +5 -4
  63. data/lib/paper_trail_diff/version.rb +1 -1
  64. data/lib/paper_trail_diff/version_association_candidate_scope.rb +13 -3
  65. data/lib/paper_trail_diff/version_query.rb +9 -1
  66. data/lib/paper_trail_diff/version_range.rb +8 -15
  67. data/lib/paper_trail_diff/version_scope_filter.rb +11 -3
  68. data/lib/paper_trail_diff.rb +31 -2
  69. data/sig/generated/paper_trail_diff/activity_belongs_to_event_applier.rbs +10 -2
  70. data/sig/generated/paper_trail_diff/activity_collection_event_applier.rbs +5 -0
  71. data/sig/generated/paper_trail_diff/activity_collection_record_updater.rbs +5 -0
  72. data/sig/generated/paper_trail_diff/activity_event_record_resolver.rbs +7 -0
  73. data/sig/generated/paper_trail_diff/activity_range.rbs +6 -2
  74. data/sig/generated/paper_trail_diff/activity_relationship.rbs +3 -0
  75. data/sig/generated/paper_trail_diff/activity_root_snapshot_refresher.rbs +2 -0
  76. data/sig/generated/paper_trail_diff/activity_target_candidate_loader.rbs +79 -0
  77. data/sig/generated/paper_trail_diff/activity_timeline_builder.rbs +11 -5
  78. data/sig/generated/paper_trail_diff/activity_version_collector.rbs +10 -4
  79. data/sig/generated/paper_trail_diff/association_activity_edge.rbs +148 -0
  80. data/sig/generated/paper_trail_diff/association_activity_selection.rbs +112 -0
  81. data/sig/generated/paper_trail_diff/association_discovery.rbs +3 -5
  82. data/sig/generated/paper_trail_diff/association_traversal.rbs +84 -9
  83. data/sig/generated/paper_trail_diff/batch_safety.rbs +10 -4
  84. data/sig/generated/paper_trail_diff/batched_root_versions.rbs +2 -8
  85. data/sig/generated/paper_trail_diff/collection_comparator.rbs +6 -3
  86. data/sig/generated/paper_trail_diff/collection_identity_index.rbs +3 -3
  87. data/sig/generated/paper_trail_diff/comparison_batch.rbs +22 -4
  88. data/sig/generated/paper_trail_diff/deleted_analysis_batch.rbs +16 -4
  89. data/sig/generated/paper_trail_diff/diagnostics.rbs +7 -2
  90. data/sig/generated/paper_trail_diff/direct_record_state_loader.rbs +6 -0
  91. data/sig/generated/paper_trail_diff/errors.rbs +2 -1
  92. data/sig/generated/paper_trail_diff/habtm_transaction_owners.rbs +40 -0
  93. data/sig/generated/paper_trail_diff/historical_association_reifier.rbs +13 -0
  94. data/sig/generated/paper_trail_diff/historical_filter.rbs +6 -6
  95. data/sig/generated/paper_trail_diff/historical_snapshot_store.rbs +3 -2
  96. data/sig/generated/paper_trail_diff/live_endpoint_batch_loader.rbs +17 -0
  97. data/sig/generated/paper_trail_diff/paper_trail_adapter.rbs +5 -0
  98. data/sig/generated/paper_trail_diff/prepared_activity_index.rbs +52 -10
  99. data/sig/generated/paper_trail_diff/prepared_edge_loader.rbs +4 -12
  100. data/sig/generated/paper_trail_diff/prepared_history.rbs +33 -22
  101. data/sig/generated/paper_trail_diff/prepared_history_loader.rbs +15 -13
  102. data/sig/generated/paper_trail_diff/prepared_record_index.rbs +15 -0
  103. data/sig/generated/paper_trail_diff/prepared_state_association_reader.rbs +56 -11
  104. data/sig/generated/paper_trail_diff/recorded_changes.rbs +47 -0
  105. data/sig/generated/paper_trail_diff/recorded_class.rbs +22 -0
  106. data/sig/generated/paper_trail_diff/root_version_plan.rbs +7 -0
  107. data/sig/generated/paper_trail_diff/scope_condition.rbs +185 -0
  108. data/sig/generated/paper_trail_diff/scope_ranking.rbs +117 -0
  109. data/sig/generated/paper_trail_diff/scoped_analysis_builder.rbs +2 -2
  110. data/sig/generated/paper_trail_diff/scoped_root_selection.rbs +41 -7
  111. data/sig/generated/paper_trail_diff/snapshot_normalizer.rbs +25 -9
  112. data/sig/generated/paper_trail_diff/support.rbs +5 -2
  113. data/sig/generated/paper_trail_diff/time_range.rbs +4 -0
  114. data/sig/generated/paper_trail_diff/time_version_range.rbs +4 -4
  115. data/sig/generated/paper_trail_diff/timeline_range.rbs +2 -1
  116. data/sig/generated/paper_trail_diff/version_association_candidate_scope.rbs +7 -0
  117. data/sig/generated/paper_trail_diff/version_query.rbs +7 -1
  118. data/sig/generated/paper_trail_diff/version_range.rbs +4 -2
  119. data/sig/generated/paper_trail_diff/version_scope_filter.rbs +4 -0
  120. data/sig/generated/paper_trail_diff.rbs +21 -2
  121. metadata +20 -4
@@ -38,8 +38,9 @@ module PaperTrailDiff
38
38
  if @association_tree.empty?
39
39
  Engine.compare(snapshot_for_endpoint(from_endpoint), snapshot_for_endpoint(to_endpoint))
40
40
  else
41
- # Prepared history orders versions that share a timestamp; reading
42
- # associations straight from PT-AT would resolve them by time alone.
41
+ # Prepared history orders versions that share a timestamp and finds
42
+ # children that later moved away; reading associations straight from
43
+ # PT-AT would resolve them by time alone and miss those children.
43
44
  comparison_batch([{ from: from_endpoint, to: to_endpoint }],
44
45
  preparer: @traversal_preparer.method(:call)).call.values.fetch(0)
45
46
  end
@@ -133,7 +134,7 @@ module PaperTrailDiff
133
134
  #: (untyped, limit: Integer?, within: untyped, ?activity: bool, ?version_scope: untyped, ?close_on: Symbol?, ?snapshots: bool, ?group: Symbol?, ?historical_filter: untyped, ?max_filter_versions: Integer, ?include_deleted: bool) -> ScopedAnalysis
134
135
  def analyze_scope(scope, limit:, within:, activity: false, version_scope: nil, close_on: nil, snapshots: false, group: nil, historical_filter: nil, max_filter_versions: HistoricalFilter::DEFAULT_VERSION_LIMIT, include_deleted: false) # rubocop:disable Metrics/ParameterLists, Layout/LineLength
135
136
  ActivityGrouping.validate!(group, activity: activity, snapshots: snapshots)
136
- @batch_safety.call(scope.all.model) if scope.respond_to?(:all) && scope.respond_to?(:where)
137
+ check_scope(scope, activity)
137
138
  # @type var options: report_options
138
139
  options = { within: within, activity: activity, version_scope: version_scope,
139
140
  close_on: close_on, snapshots: snapshots, group: group }
@@ -143,18 +144,32 @@ module PaperTrailDiff
143
144
  analyzer: ->(records) { analyze_many(records, **options, within: within) },
144
145
  deleted_analyzer: lambda { |identities|
145
146
  analyze_deleted(scope.all.model, identities, options)
146
- }
147
+ },
148
+ # Checkpoints compare a root's own versions, so only activity can
149
+ # report a window in which just its associations changed.
150
+ activity_tree: (@association_tree if activity)
147
151
  ).call
148
152
  end
149
153
 
150
154
  private
151
155
 
156
+ # Selecting by activity reads association history, so it must be available
157
+ # before the first selection query rather than once analysis starts.
158
+ #: (untyped, bool) -> void
159
+ def check_scope(scope, activity)
160
+ return unless scope.respond_to?(:all) && scope.respond_to?(:where)
161
+
162
+ @batch_safety.call(scope.all.model)
163
+ @traversal_preparer.call(scope.all.model, historical: true) if activity
164
+ end
165
+
152
166
  #: (Array[comparison_input], preparer: untyped) -> ComparisonBatch
153
167
  def comparison_batch(comparisons, preparer:)
154
168
  ComparisonBatch.new(
155
169
  comparisons,
156
170
  live_loader: @live_endpoints, preparer: preparer,
157
171
  history_preparer: @historical_store.method(:prepare_batch),
172
+ version_history_preparer: @historical_store.method(:prepare_deleted_batch),
158
173
  historical_snapshotter: method(:historical_snapshot),
159
174
  live_normalizer: method(:normalize_live_snapshot)
160
175
  )
@@ -168,7 +183,7 @@ module PaperTrailDiff
168
183
 
169
184
  #: (untyped, historical: bool) -> void
170
185
  def prepare_batch_model(model, historical:)
171
- @batch_safety.call(model, historical: historical)
186
+ @batch_safety.call(model)
172
187
  @traversal_preparer.call(model, historical: historical)
173
188
  end
174
189
 
@@ -185,7 +200,8 @@ module PaperTrailDiff
185
200
  model, identities: identities, time_range: within.nil? ? nil : TimeRange.new(within),
186
201
  version_scope: options.fetch(:version_scope),
187
202
  history_preparer: @historical_store.method(:prepare_deleted_batch),
188
- analyzer: analyzer
203
+ analyzer: analyzer,
204
+ context_required: options.fetch(:activity) && !@association_tree.empty?
189
205
  ).call
190
206
  end
191
207
 
@@ -30,7 +30,7 @@ module PaperTrailDiff
30
30
 
31
31
  #: (untyped, AssociationTree, String, untyped) -> void
32
32
  def validate_association(record, tree, path, reflection)
33
- association_path = Support.association_path(path, reflection.name.to_s)
33
+ association_path = Support.association_path(path, reflection.name)
34
34
  association = record.association(reflection.name)
35
35
  unless association.loaded?
36
36
  message = "current endpoint association is not preloaded: #{association_path}"
@@ -10,39 +10,93 @@ module PaperTrailDiff
10
10
  @range = ActivityRange.new(start_at, end_at)
11
11
  @roots = root_versions.group_by { |version| [version.item_type, version.item_id.to_s] }
12
12
  @nodes = {} #: Hash[String, Array[untyped]]
13
- @children = {} #: Hash[Array[String], untyped]
14
- @rows = {} #: Hash[Array[String], Hash[String, Array[untyped]]]
13
+ # Rows are cached by reflection, which a subclass shares with its base.
14
+ @children = {} #: Hash[untyped, untyped]
15
+ @targets = {} #: Hash[untyped, untyped]
16
+ @rows = {} #: Hash[untyped, Hash[String, Array[untyped]]]
15
17
  end
16
18
 
17
19
  #: (untyped, Array[untyped]) -> void
18
20
  def register(model, ids)
19
- name = model.base_class.name.to_s
21
+ name = model.base_class.name
20
22
  @nodes[name] = (@nodes.fetch(name, []) | ids)
21
23
  end
22
24
 
23
25
  #: (untyped, Array[untyped]) -> Array[untyped]
24
26
  def versions(model, ids)
25
27
  ids.flat_map do |id|
26
- key = [model.base_class.name.to_s, id.to_s]
27
- @roots[key] || @records.versions_for_id(model, id)
28
+ @roots[[model.base_class.name, id.to_s]] || @records.versions_for_id(model, id)
29
+ end
30
+ end
31
+
32
+ # Children are found by key across the whole table, so an association to
33
+ # an STI subclass finds the rows of its siblings too. Only a row that was
34
+ # of the class at some point belongs to it, and of that row only the
35
+ # versions it had the class before or after.
36
+ #: (untyped, Array[untyped]) -> Array[untyped]
37
+ def member_ids(model, ids)
38
+ return ids if model.descends_from_active_record?
39
+
40
+ ids.select { |id| member_class?(model, @records.model_classes(model, id)) }
41
+ end
42
+
43
+ #: (untyped, Array[untyped], ActivityRange) -> Array[untyped]
44
+ def member_versions(model, ids, range)
45
+ all = versions(model, ids).select { |version| range.include?(version) }
46
+ return all if model.descends_from_active_record?
47
+
48
+ all.select do |version|
49
+ member_class?(model, @records.model_classes(model, version.item_id, version))
50
+ end
51
+ end
52
+
53
+ # The versions of a through's join rows that are on its path. Under
54
+ # `source_type:`, a row is on it only while its type column names that
55
+ # class, so only the versions it had that type before or after count. A
56
+ # version saved before the type column existed has no type to read.
57
+ #: (Hash[Symbol, untyped], untyped) -> Array[untyped]
58
+ def join_versions(group, reflection)
59
+ versions = group.fetch(:versions)
60
+ source_type = reflection.options[:source_type]
61
+ return versions unless source_type
62
+
63
+ column = reflection.foreign_type
64
+ versions.select do |version|
65
+ @records.states_around(group.fetch(:model), version.item_id, version).any? do |state|
66
+ state.attributes[column] == source_type.to_s
67
+ end
28
68
  end
29
69
  end
30
70
 
31
71
  #: (untyped, Array[untyped], untyped, ActivityRange) -> Array[untyped]
32
72
  def child_ids(model, ids, reflection, range)
33
- key = [model.base_class.name.to_s, reflection.name.to_s]
34
- historical, live = @children[key] ||= child_rows(model, reflection)
73
+ historical, live = @children[reflection] ||= child_rows(model, reflection)
35
74
  empty = [] #: Array[untyped]
36
75
  selected = historical_ids(reflection.klass, ids, historical, range)
37
76
  selected |= ids.flat_map { |id| live.fetch(id.to_s, empty) } if range.current_end?
38
77
  selected.uniq
39
78
  end
40
79
 
41
- #: (untyped, Array[untyped], untyped, ?transactions: bool) -> Array[untyped]
42
- def association_rows(model, selected_versions, reflection, transactions: false)
43
- key = [model.base_class.name.to_s, reflection.name.to_s]
44
- rows = @rows[key] ||= load_association_rows(model, reflection, transactions)
45
- version_ids(selected_versions, transactions).flat_map { |id| rows.fetch(id.to_s, []) }
80
+ # The `belongs_to` targets each owner pointed at in the range: at its start
81
+ # and after each change inside it, plus the live row at a current end.
82
+ #: (untyped, Array[untyped], untyped, ActivityRange) -> Array[ActivityTargetCandidateLoader::Row]
83
+ def target_rows(model, ids, reflection, range)
84
+ historical, live = @targets[reflection] ||= target_candidates(model, reflection)
85
+ rows = eligible_rows(model, rows_for(historical, ids), range)
86
+ range.current_end? ? rows + rows_for(live, ids) : rows
87
+ end
88
+
89
+ #: (untyped, untyped) -> bool
90
+ def loaded?(model, id)
91
+ @records.loaded?(model, id)
92
+ end
93
+
94
+ # PT-AT records HABTM membership once per transaction, under the
95
+ # association's name, so the rows are keyed by transaction id.
96
+ #: (untyped, Array[untyped], untyped) -> Array[untyped]
97
+ def habtm_rows(model, selected_versions, reflection)
98
+ rows = @rows[reflection] ||= load_habtm_rows(model, reflection)
99
+ transaction_ids(selected_versions).flat_map { |id| rows.fetch(id.to_s, []) }
46
100
  end
47
101
 
48
102
  private
@@ -51,22 +105,29 @@ module PaperTrailDiff
51
105
  # @rbs @range: ActivityRange
52
106
  # @rbs @roots: Hash[Array[String], Array[untyped]]
53
107
  # @rbs @nodes: Hash[String, Array[untyped]]
54
- # @rbs @children: Hash[Array[String], untyped]
55
- # @rbs @rows: Hash[Array[String], Hash[String, Array[untyped]]]
108
+ # @rbs @children: Hash[untyped, untyped]
109
+ # @rbs @targets: Hash[untyped, untyped]
110
+ # @rbs @rows: Hash[untyped, Hash[String, Array[untyped]]]
111
+
112
+ #: (untyped, Array[untyped]) -> bool
113
+ def member_class?(model, classes)
114
+ classes.any? { |model_class| model_class <= model }
115
+ end
56
116
 
57
117
  #: (untyped, Array[untyped], untyped, ActivityRange) -> Array[untyped]
58
118
  def historical_ids(model, ids, historical, range)
59
119
  empty = [] #: Array[untyped]
60
120
  ids.flat_map do |id|
61
121
  historical.fetch(id.to_s, empty).filter_map do |row|
62
- row[1] if eligible_child?(model, row, range)
122
+ _owner, child_id, timestamp, event = row
123
+ child_id if eligible_version?(model, child_id, timestamp, event, range)
63
124
  end
64
125
  end
65
126
  end
66
127
 
67
128
  #: (untyped) -> Array[untyped]
68
129
  def node_ids(model)
69
- name = model.base_class.name.to_s
130
+ name = model.base_class.name
70
131
  @nodes.fetch(name) do
71
132
  raise ConfigurationError,
72
133
  "prepared activity graph is missing #{name}; " \
@@ -84,12 +145,31 @@ module PaperTrailDiff
84
145
  .transform_values { |rows| rows.map(&:last) }]
85
146
  end
86
147
 
148
+ #: (Hash[String, Array[ActivityTargetCandidateLoader::Row]], Array[untyped]) -> Array[ActivityTargetCandidateLoader::Row]
149
+ def rows_for(grouped, ids)
150
+ ids.flat_map { |id| grouped[id.to_s] || [] }
151
+ end
152
+
153
+ #: (untyped, Array[ActivityTargetCandidateLoader::Row], ActivityRange) -> Array[ActivityTargetCandidateLoader::Row]
154
+ def eligible_rows(model, rows, range)
155
+ rows.select do |row|
156
+ eligible_version?(model, row.owner_id, row.created_at, row.event, range) &&
157
+ range.recorded_before_end?(row.created_at, row.version_id)
158
+ end
159
+ end
160
+
161
+ #: (untyped, untyped) -> untyped
162
+ def target_candidates(model, reflection)
163
+ loader = ActivityTargetCandidateLoader.new(model, node_ids(model), reflection, @range)
164
+ [loader.historical_rows.group_by { |row| row.owner_id.to_s },
165
+ loader.current_rows.group_by { |row| row.owner_id.to_s }]
166
+ end
167
+
87
168
  # Mirrors VersionAssociationCandidateScope's before-window rule. The loaded
88
169
  # series includes changes to other owners too, so an old membership cannot
89
170
  # survive a later pre-window reassignment just because that owner is absent.
90
- #: (untyped, Array[untyped], ActivityRange) -> bool
91
- def eligible_child?(model, row, range)
92
- _owner, id, timestamp, event = row
171
+ #: (untyped, untyped, untyped, String?, ActivityRange) -> bool
172
+ def eligible_version?(model, id, timestamp, event, range)
93
173
  return timestamp <= range.end_time if timestamp >= range.start_time
94
174
  return false if event == 'destroy'
95
175
 
@@ -98,24 +178,18 @@ module PaperTrailDiff
98
178
  !later || later.created_at >= range.start_time
99
179
  end
100
180
 
101
- #: (untyped, untyped, bool) -> Hash[String, Array[untyped]]
102
- def load_association_rows(model, reflection, transactions)
103
- all_versions = versions(model, node_ids(model))
104
- foreign_key = transactions ? reflection.name.to_s : reflection.foreign_key.to_s
181
+ #: (untyped, untyped) -> Hash[String, Array[untyped]]
182
+ def load_habtm_rows(model, reflection)
105
183
  model.paper_trail.version_association_class.where(
106
- version_id: version_ids(all_versions, transactions), foreign_key_name: foreign_key
107
- ).to_a.group_by { |row| row.version_id.to_s }
108
- end
109
-
110
- #: (Array[untyped], bool) -> Array[untyped]
111
- def version_ids(versions, transactions)
112
- if transactions
113
- versions.filter_map do |version|
114
- version.transaction_id if version.respond_to?(:transaction_id)
115
- end.uniq
116
- else
117
- versions.map(&:id)
118
- end
184
+ version_id: transaction_ids(versions(model, node_ids(model))),
185
+ foreign_key_name: reflection.name
186
+ ).group_by { |row| row.version_id.to_s }
187
+ end
188
+
189
+ # A version without a transaction id names no rows, so it can stay in.
190
+ #: (Array[untyped]) -> Array[untyped]
191
+ def transaction_ids(versions)
192
+ versions.map { |version| version.transaction_id if version.respond_to?(:transaction_id) }.uniq
119
193
  end
120
194
  end
121
195
  end
@@ -4,25 +4,21 @@
4
4
  module PaperTrailDiff
5
5
  # Loads every historical identity that can participate in one association edge.
6
6
  class PreparedEdgeLoader
7
- EDGE_LOADERS = {
8
- belongs_to: :belongs_to_edge,
9
- has_one: :child_edge,
10
- has_many: :child_edge,
11
- has_and_belongs_to_many: :habtm_edge
12
- }.freeze
13
- private_constant :EDGE_LOADERS
14
-
15
7
  #: (PreparedRecordIndex, Array[untyped], ?start_at: untyped) -> void
16
8
  def initialize(records, root_versions, start_at: root_versions.first&.created_at)
17
9
  @records = records
18
- @transaction_ids = root_versions.filter_map(&:transaction_id).uniq.freeze
10
+ @transaction_ids = root_versions.filter_map(&:transaction_id)
19
11
  @start_at = start_at
20
12
  end
21
13
 
14
+ # HABTM alone also returns each root transaction's members.
22
15
  #: (untyped, Array[untyped], untyped) -> [Hash[String, Hash[Symbol, untyped]], Hash[String, Array[untyped]]?]
23
16
  def call(owner_class, owner_ids, reflection)
24
- loader = EDGE_LOADERS.fetch(reflection.macro)
25
- send(loader, owner_class, owner_ids, reflection)
17
+ case reflection.macro
18
+ when :belongs_to then [belongs_to_groups(owner_class, owner_ids, reflection), nil]
19
+ when :has_and_belongs_to_many then habtm_groups(owner_class, reflection)
20
+ else [child_groups(owner_class, owner_ids, reflection), nil]
21
+ end
26
22
  end
27
23
 
28
24
  private
@@ -31,21 +27,6 @@ module PaperTrailDiff
31
27
  # @rbs @transaction_ids: Array[untyped]
32
28
  # @rbs @start_at: untyped
33
29
 
34
- #: (untyped, Array[untyped], untyped) -> [Hash[String, Hash[Symbol, untyped]], nil]
35
- def belongs_to_edge(owner_class, owner_ids, reflection)
36
- [belongs_to_groups(owner_class, owner_ids, reflection), nil]
37
- end
38
-
39
- #: (untyped, Array[untyped], untyped) -> [Hash[String, Hash[Symbol, untyped]], nil]
40
- def child_edge(owner_class, owner_ids, reflection)
41
- [child_groups(owner_class, owner_ids, reflection), nil]
42
- end
43
-
44
- #: (untyped, Array[untyped], untyped) -> [Hash[String, Hash[Symbol, untyped]], Hash[String, Array[untyped]]]
45
- def habtm_edge(owner_class, _owner_ids, reflection)
46
- habtm_groups(owner_class, reflection)
47
- end
48
-
49
30
  #: (untyped, Array[untyped], untyped) -> Hash[String, Hash[Symbol, untyped]]
50
31
  def belongs_to_groups(owner_class, owner_ids, reflection)
51
32
  key = scalar_foreign_key(reflection)
@@ -56,9 +37,12 @@ module PaperTrailDiff
56
37
  end
57
38
 
58
39
  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.
59
42
  @records.records_for(owner_class, owner_ids).each do |owner|
60
43
  id = owner.public_send(key)
61
- model_class = AssociationTraversal.belongs_to_target(reflection, owner)
44
+ type = owner.public_send(reflection.foreign_type).to_s
45
+ model_class = AssociationTraversal.named_class(type)
62
46
  add_identity(grouped, model_class, id) if model_class && id
63
47
  end
64
48
  grouped
@@ -75,28 +59,31 @@ module PaperTrailDiff
75
59
  group_for(reflection.klass, owners.values.flatten, owners: owners)
76
60
  end
77
61
 
62
+ # A transaction more than one owner recorded members in is left out, so
63
+ # its read falls back to HistoricalAssociationReifier, which raises.
78
64
  #: (untyped, untyped) -> [Hash[String, Hash[Symbol, untyped]], Hash[String, Array[untyped]]]
79
65
  def habtm_groups(owner_class, reflection)
80
66
  rows = habtm_rows(owner_class, reflection)
67
+ shared = HabtmTransactionOwners.new(owner_class, reflection).shared(rows.map(&:version_id))
81
68
  memberships = @transaction_ids.to_h do |transaction_id|
82
69
  [transaction_id.to_s, membership_ids(rows, transaction_id)]
83
70
  end
84
- [group_for(reflection.klass, rows.filter_map(&:foreign_key_id)), memberships]
71
+ [group_for(reflection.klass, rows.map(&:foreign_key_id)), memberships.except(*shared)]
85
72
  end
86
73
 
87
74
  #: (untyped, untyped) -> Array[untyped]
88
75
  def habtm_rows(owner_class, reflection)
89
76
  owner_class.paper_trail.version_association_class.where(
90
77
  version_id: @transaction_ids,
91
- foreign_key_name: reflection.name.to_s
92
- ).to_a
78
+ foreign_key_name: reflection.name
79
+ )
93
80
  end
94
81
 
95
82
  #: (Array[untyped], untyped) -> Array[untyped]
96
83
  def membership_ids(rows, transaction_id)
97
84
  rows.filter_map do |row|
98
85
  row.foreign_key_id if row.version_id.to_s == transaction_id.to_s
99
- end.uniq
86
+ end
100
87
  end
101
88
 
102
89
  #: (untyped, Array[untyped], untyped, String) -> Hash[String, Array[untyped]]
@@ -109,7 +96,7 @@ module PaperTrailDiff
109
96
  version_class = owner_class.paper_trail.version_class
110
97
  association_class = owner_class.paper_trail.version_association_class
111
98
  relation = historical_child_relation(owner_class, owner_ids, reflection, foreign_key)
112
- pairs = child_candidate_scope(version_class).call(relation).distinct.pluck(
99
+ pairs = child_candidate_scope(version_class).call(relation).pluck(
113
100
  association_class.arel_table[:foreign_key_id],
114
101
  version_class.arel_table[:item_id]
115
102
  )
@@ -141,10 +128,10 @@ module PaperTrailDiff
141
128
 
142
129
  #: (untyped, Array[untyped], untyped, String) -> Hash[String, Array[untyped]]
143
130
  def live_child_ids(owner_class, owner_ids, reflection, foreign_key)
131
+ # Every row of the table, as history reads them: an STI subclass's
132
+ # siblings are left out where children are resolved.
144
133
  relation = reflection.klass.base_class.unscoped.where(foreign_key => owner_ids)
145
- if reflection.options[:as]
146
- relation = relation.where(reflection.type => parent_types(owner_class))
147
- end
134
+ relation = relation.where(reflection.type => parent_types(owner_class)) if reflection.type
148
135
  group_ids_by_owner(relation.pluck(foreign_key, reflection.klass.primary_key))
149
136
  end
150
137
 
@@ -161,18 +148,15 @@ module PaperTrailDiff
161
148
 
162
149
  group = { model: model_class, ids: unique } #: Hash[Symbol, untyped]
163
150
  group[:owners] = owners if owners
164
- { model_class.name.to_s => group }
151
+ { model_class.name => group }
165
152
  end
166
153
 
167
- # Keyed rather than scanned so that a wide edge stays linear in its width.
154
+ # A set rather than a scan, so that a wide edge stays linear in its width.
168
155
  #: (Array[Array[untyped]]) -> Hash[String, Array[untyped]]
169
156
  def group_ids_by_owner(pairs)
170
- grouped = {} #: Hash[String, Hash[untyped, true]]
171
- pairs.each do |owner_id, child_id|
172
- ids = grouped[owner_id.to_s] ||= {} #: Hash[untyped, true]
173
- ids[child_id] = true
174
- end
175
- grouped.transform_values(&:keys)
157
+ grouped = {} #: Hash[String, Set[untyped]]
158
+ pairs.each { |owner_id, child_id| (grouped[owner_id.to_s] ||= Set.new) << child_id }
159
+ grouped.transform_values(&:to_a)
176
160
  end
177
161
 
178
162
  #: (Hash[String, Array[untyped]], Hash[String, Array[untyped]]) -> Hash[String, Array[untyped]]
@@ -183,12 +167,12 @@ module PaperTrailDiff
183
167
  #: (untyped) -> String?
184
168
  def scalar_foreign_key(reflection)
185
169
  key = reflection.foreign_key
186
- key.to_s unless key.is_a?(Array)
170
+ key unless key.is_a?(Array)
187
171
  end
188
172
 
189
173
  #: (untyped) -> Array[String?]
190
174
  def parent_types(owner_class)
191
- [nil, '', owner_class.name.to_s, owner_class.base_class.name.to_s].uniq
175
+ [nil, '', owner_class.name, owner_class.base_class.name]
192
176
  end
193
177
  end
194
178
  end
@@ -17,14 +17,16 @@ module PaperTrailDiff
17
17
  #: (PreparedRecordIndex, ?activity_index: PreparedActivityIndex?) -> void
18
18
  def initialize(records, activity_index: nil)
19
19
  @records = records
20
- @state_reader = PreparedStateAssociationReader.new(records, method(:edge))
20
+ @conditions = ScopeCondition::Cache.new
21
+ @state_reader = PreparedStateAssociationReader.new(records, method(:edge), @conditions)
21
22
  @activity_index = activity_index
22
23
  @edges = {} #: Hash[Array[String], Hash[String, Hash[Symbol, untyped]]]
23
24
  @unsupported = {} #: Hash[Array[String], bool]
24
25
  @habtm = {} #: Hash[Array[String], Hash[String, Array[untyped]]]
25
26
  end
26
27
 
27
- #: (untyped, untyped, Hash[String, Hash[Symbol, untyped]]) -> void
28
+ # Returns every group the edge now holds.
29
+ #: (untyped, untyped, Hash[String, Hash[Symbol, untyped]]) -> Hash[String, Hash[Symbol, untyped]]
28
30
  def merge_edge(owner_class, reflection, groups)
29
31
  key = edge_key(owner_class, reflection)
30
32
  empty = {} #: Hash[String, Hash[Symbol, untyped]]
@@ -62,14 +64,16 @@ module PaperTrailDiff
62
64
  @state_reader.call(owner, reflection, boundary)
63
65
  end
64
66
 
65
- #: (untyped, untyped, untyped, habtm_boundary: untyped) -> [bool, Array[untyped]]
66
- def resolve(record, reflection, boundary, habtm_boundary:)
67
- return [false, []] unless supported?(record.class, reflection)
67
+ # Unranked, as a part of a has_many through is read, it returns every row
68
+ # the part's conditions admit, a record reached twice appearing twice.
69
+ #: (untyped, untyped, untyped, habtm_boundary: untyped, ?ranked: bool) -> [bool, Array[untyped]]
70
+ def resolve(record, reflection, boundary, habtm_boundary:, ranked: true)
71
+ return [false, []] unless supported?(record.class, reflection, ranked)
68
72
 
69
73
  records = if reflection.options[:through]
70
- through_records(record, reflection, boundary, habtm_boundary)
74
+ through_records(record, reflection, boundary, habtm_boundary, ranked)
71
75
  else
72
- direct_records(record, reflection, boundary, habtm_boundary)
76
+ direct_records(record, reflection, boundary, habtm_boundary, ranked)
73
77
  end
74
78
  records ? [true, records] : [false, []]
75
79
  end
@@ -81,31 +85,34 @@ module PaperTrailDiff
81
85
  # @rbs @edges: Hash[Array[String], Hash[String, Hash[Symbol, untyped]]]
82
86
  # @rbs @unsupported: Hash[Array[String], bool]
83
87
  # @rbs @habtm: Hash[Array[String], Hash[String, Array[untyped]]]
88
+ # @rbs @conditions: ScopeCondition::Cache
84
89
 
85
- #: (untyped, untyped) -> bool
86
- def supported?(owner_class, reflection)
90
+ # A polymorphic target's class is known only once a record names it, so
91
+ # belongs_to_records reads its scope for each record instead.
92
+ #: (untyped, untyped, bool) -> bool
93
+ def supported?(owner_class, reflection, ranked)
87
94
  !@unsupported[edge_key(owner_class, reflection)] &&
88
- reflection.scope.nil? && !reflection.klass.default_scopes?
95
+ (reflection.polymorphic? || !@conditions.call(reflection, ranked: ranked).nil?)
89
96
  end
90
97
 
91
- #: (untyped, untyped, untyped, untyped) -> Array[untyped]?
92
- def direct_records(record, reflection, boundary, habtm_boundary)
98
+ #: (untyped, untyped, untyped, untyped, bool) -> Array[untyped]?
99
+ def direct_records(record, reflection, boundary, habtm_boundary, ranked)
93
100
  resolver = DIRECT_RESOLVERS.fetch(reflection.macro)
94
- send(resolver, record, reflection, boundary, habtm_boundary)
101
+ send(resolver, record, reflection, boundary, habtm_boundary, ranked)
95
102
  end
96
103
 
97
- #: (untyped, untyped, untyped, untyped) -> Array[untyped]?
98
- def resolve_belongs_to(record, reflection, boundary, _habtm_boundary)
99
- belongs_to_records(record, reflection, boundary)
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)
100
107
  end
101
108
 
102
- #: (untyped, untyped, untyped, untyped) -> Array[untyped]
103
- def resolve_children(record, reflection, boundary, _habtm_boundary)
104
- child_records(record, reflection, boundary)
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)
105
112
  end
106
113
 
107
- #: (untyped, untyped, untyped) -> Array[untyped]?
108
- def belongs_to_records(record, reflection, boundary)
114
+ #: (untyped, untyped, untyped, bool) -> Array[untyped]?
115
+ def belongs_to_records(record, reflection, boundary, ranked)
109
116
  foreign_key = scalar_foreign_key(reflection)
110
117
  return unless foreign_key
111
118
 
@@ -113,20 +120,23 @@ module PaperTrailDiff
113
120
  return [] if id.nil?
114
121
 
115
122
  model_class = AssociationTraversal.belongs_to_target(reflection, record)
116
- return unless model_class && @records.loaded?(model_class, id)
123
+ return unless model_class && @conditions.call(reflection, model_class, ranked: ranked) &&
124
+ @records.loaded?(model_class, id)
117
125
 
118
- Array(@records.record_before(model_class, id, boundary)).compact
126
+ target = Array(@records.record_before(model_class, id, boundary)).compact
127
+ @conditions.select(reflection, target, model_class, ranked: ranked)
119
128
  end
120
129
 
121
- #: (untyped, untyped, untyped) -> Array[untyped]
122
- def child_records(record, reflection, boundary)
123
- edge(record.class, reflection).values.flat_map do |group|
130
+ #: (untyped, untyped, untyped, bool) -> Array[untyped]
131
+ def child_records(record, reflection, boundary, ranked)
132
+ children = edge(record.class, reflection).values.flat_map do |group|
124
133
  model_class = group.fetch(:model)
125
134
  child_ids_for(group, record).filter_map do |id|
126
135
  child = @records.record_before(model_class, id, boundary)
127
136
  child if child && member_of?(child, reflection, record)
128
137
  end
129
138
  end
139
+ @conditions.select(reflection, children, ranked: ranked)
130
140
  end
131
141
 
132
142
  #: (Hash[Symbol, untyped], untyped) -> Array[untyped]
@@ -136,8 +146,8 @@ module PaperTrailDiff
136
146
  owners ? owners.fetch(owner.id.to_s, empty) : group.fetch(:ids)
137
147
  end
138
148
 
139
- #: (untyped, untyped, untyped, untyped) -> Array[untyped]?
140
- def habtm_records(record, reflection, boundary, habtm_boundary)
149
+ #: (untyped, untyped, untyped, untyped, bool) -> Array[untyped]?
150
+ def habtm_records(record, reflection, boundary, habtm_boundary, ranked)
141
151
  transaction_id = transaction_id_for(habtm_boundary)
142
152
  return unless transaction_id
143
153
 
@@ -145,37 +155,35 @@ module PaperTrailDiff
145
155
  ids = @habtm.fetch(edge_key(record.class, reflection), empty)[transaction_id.to_s]
146
156
  return unless ids
147
157
 
148
- ids.filter_map do |id|
149
- @records.record_before(reflection.klass, id, boundary)
150
- end
158
+ members = ids.filter_map { |id| @records.record_before(reflection.klass, id, boundary) }
159
+ @conditions.select(reflection, members, ranked: ranked)
151
160
  end
152
161
 
153
- #: (untyped, untyped, untyped, untyped) -> Array[untyped]?
154
- def through_records(record, reflection, boundary, habtm_boundary)
155
- handled, intermediates = resolve(
156
- record,
157
- reflection.through_reflection,
158
- boundary,
159
- habtm_boundary: habtm_boundary
160
- )
161
- return unless handled
162
-
163
- resolved = resolve_sources(intermediates, reflection, boundary, habtm_boundary)
164
- return unless resolved.all?(&:first)
162
+ # A has_many through reads its parts unranked, as its query does. Its own
163
+ # scope applies to each row, and a ranked read then keeps a record reached
164
+ # twice as one member.
165
+ #: (untyped, untyped, untyped, untyped, bool) -> Array[untyped]?
166
+ def through_records(record, reflection, boundary, habtm_boundary, ranked)
167
+ read = lambda do |owner, part|
168
+ resolve(owner, part, boundary, habtm_boundary: habtm_boundary,
169
+ ranked: ranked && !reflection.collection?)
170
+ end
171
+ rows = through_rows(record, reflection, read)
172
+ return unless rows
165
173
 
166
- unique_records(resolved.flat_map(&:last))
174
+ rows = @conditions.select(reflection, rows, ranked: ranked)
175
+ ranked ? unique_records(rows) : rows
167
176
  end
168
177
 
169
- #: (Array[untyped], untyped, untyped, untyped) -> Array[[bool, Array[untyped]]]
170
- def resolve_sources(intermediates, reflection, boundary, habtm_boundary)
171
- intermediates.map do |intermediate|
172
- resolve(
173
- intermediate,
174
- reflection.source_reflection,
175
- boundary,
176
- habtm_boundary: habtm_boundary
177
- )
178
- end
178
+ # The source records of each join record, once for each join.
179
+ #: (untyped, untyped, ^(untyped, untyped) -> [bool, Array[untyped]]) -> Array[untyped]?
180
+ def through_rows(record, reflection, read)
181
+ handled, joins = read.call(record, reflection.through_reflection)
182
+ return unless handled
183
+
184
+ joins = joins.select { |join| AssociationTraversal.source_type_join?(reflection, join) }
185
+ resolved = joins.map { |join| read.call(join, reflection.source_reflection) }
186
+ resolved.flat_map(&:last) if resolved.all?(&:first)
179
187
  end
180
188
 
181
189
  #: (untyped, untyped, untyped) -> bool
@@ -183,6 +191,7 @@ module PaperTrailDiff
183
191
  foreign_key = scalar_foreign_key(reflection)
184
192
  return false unless foreign_key
185
193
  return false unless child.public_send(foreign_key).to_s == owner.id.to_s
194
+ return false unless AssociationTraversal.sti_member?(reflection.klass, child.attributes)
186
195
  return true unless reflection.options[:as]
187
196
 
188
197
  owner_types(reflection, owner).include?(child.public_send(reflection.type).to_s)