metadata_presenter 2.15.8 → 2.15.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 653f75d32f4d53e3693e97db20ad1e985b0cc87ea4de2d50bdda17565c960a43
4
- data.tar.gz: 0e358c7b47bbecbe3d19d321759aca4becffa0341150fab41aa23316dd61cb27
3
+ metadata.gz: fd26567ffefc85523ea94a99b37c9ff4ab7c9c923a591af95e2f8c254522b346
4
+ data.tar.gz: c949f8259d9a581317af29eff875330694e1dd9fc8decf507fafdd1230b126f5
5
5
  SHA512:
6
- metadata.gz: ba4e1ab5e37c2aa03b9a079ab808597f481067d68140ee2dcc739e987e12cece01b2cbbcc80570442d23a564a2d533e4f5912e9d7a211d2242dfd501e00103b4
7
- data.tar.gz: 22cc527a15fb85ab087cd5944222d20557f401af4b014c692bef7f06fe0333c647927672c9e1cad19e4262ae2e2ede3c7102ddda44003189ffd73d18fdb53bc3
6
+ metadata.gz: deef6fca91c8cd3203bd4eb6c4326b9e340711753681c41e9f873be0b204b161dd4a2d8e721cfd7abfc368e9a6dd9bead9c1f2039708db6ba602299c7b18e75a
7
+ data.tar.gz: c3bdc48b2cf30259ab65d13d43652bf1f60d66073f1c1106dffded0b09fba581cf2a113e645c8046425c5cd6da4ed4d3cb6bb0f04dd8c12341233dedd8944337
@@ -25,18 +25,35 @@ module MetadataPresenter
25
25
 
26
26
  def column_number
27
27
  @column_number ||= begin
28
- return latest_column if cya_or_confirmation_page?
28
+ return last_column if confirmation_page?
29
+ return latest_column if checkanswers_page?
29
30
 
30
31
  existing_column || new_column
31
32
  end
32
33
  end
33
34
 
35
+ def confirmation_page?
36
+ service.confirmation_page&.uuid == uuid
37
+ end
38
+
39
+ def checkanswers_page?
40
+ service.checkanswers_page&.uuid == uuid
41
+ end
42
+
34
43
  def latest_column
35
44
  [existing_column, new_column].compact.max
36
45
  end
37
46
 
38
- def cya_or_confirmation_page?
39
- [service.checkanswers_page&.uuid, service.confirmation_page&.uuid].include?(uuid)
47
+ def last_column
48
+ checkanswers_column.present? ? checkanswers_column + 1 : latest_column
49
+ end
50
+
51
+ def checkanswers_column
52
+ @checkanswers_column ||= begin
53
+ return if service.checkanswers_page.blank?
54
+
55
+ coordinates.uuid_column(service.checkanswers_page&.uuid)
56
+ end
40
57
  end
41
58
 
42
59
  def existing_column
@@ -41,7 +41,7 @@ module MetadataPresenter
41
41
  set_row_numbers
42
42
  add_by_coordinates
43
43
  insert_expression_spacers
44
- trim_pointers unless main_flow.empty?
44
+ trim_pointers unless main_flow.empty? # only used by detached grids
45
45
  trim_spacers
46
46
  insert_warning if main_flow.empty?
47
47
 
@@ -222,9 +222,42 @@ module MetadataPresenter
222
222
  service.flow_object(uuid)
223
223
  end
224
224
 
225
+ def trim_pointers
226
+ update_branch_destination_pointers
227
+ trim_to_first_pointer
228
+ update_coordinates
229
+ replace_pointers
230
+ end
231
+
232
+ # Since a detached grid is built in the same way as the main flow grid, objects
233
+ # that appear later in the flow are left in their latest position. However
234
+ # for detached grids we want to show a Pointer to that object. This only
235
+ # happens for branch destinations so in those instances we need to replace
236
+ # the Spacers with Pointers.
237
+ def update_branch_destination_pointers
238
+ @ordered.each_with_index do |column, column_number|
239
+ next_column = column_number + 1
240
+
241
+ column.each do |flow_object|
242
+ next unless flow_object.branch?
243
+
244
+ coordinates.branch_spacers[flow_object.uuid].each do |destination_uuid, position|
245
+ if replace_with_pointer?(next_column, position[:row], destination_uuid)
246
+ @ordered[next_column][position[:row]] = MetadataPresenter::Pointer.new(uuid: destination_uuid)
247
+ end
248
+ end
249
+ end
250
+ end
251
+ end
252
+
253
+ def replace_with_pointer?(column_number, row_number, destination_uuid)
254
+ @ordered[column_number][row_number].is_a?(MetadataPresenter::Spacer) &&
255
+ main_flow.include?(destination_uuid)
256
+ end
257
+
225
258
  # A row should end at the first Pointer object it finds.
226
259
  # Therefore replace any Pointers after the first one with Spacers.
227
- def trim_pointers
260
+ def trim_to_first_pointer
228
261
  max_potential_rows.times do |row|
229
262
  first_index_of = first_pointer(row)
230
263
  next unless first_index_of
@@ -237,8 +270,70 @@ module MetadataPresenter
237
270
  end
238
271
 
239
272
  def first_pointer(row)
240
- row_objects = @ordered.map { |column| column[row] }
241
- row_objects.find_index { |obj| obj.is_a?(MetadataPresenter::Pointer) }
273
+ row_objects(row).find_index { |obj| obj.is_a?(MetadataPresenter::Pointer) }
274
+ end
275
+
276
+ def row_objects(row)
277
+ @ordered.map { |column| column[row] }
278
+ end
279
+
280
+ # It's a shame to have to do this.
281
+ # Once the Pointers have been trimmed back the original positions for each
282
+ # object will no longer be correct so we need to update them.
283
+ # We only really need to do this because we replace Pointers with Spacers
284
+ # later on.
285
+ def update_coordinates
286
+ @ordered.each_with_index do |column, column_number|
287
+ column.each_with_index do |flow_object, row_number|
288
+ next if flow_object.is_a?(MetadataPresenter::Spacer)
289
+
290
+ @coordinates.set_column(flow_object.uuid, column_number)
291
+ @coordinates.set_row(flow_object.uuid, row_number)
292
+ end
293
+ end
294
+ end
295
+
296
+ def replace_pointers
297
+ @ordered.each_with_index do |column, column_number|
298
+ next if column_number.zero?
299
+
300
+ column.each_with_index do |flow_object, row_number|
301
+ next if flow_object.is_a?(MetadataPresenter::Spacer)
302
+
303
+ previous_column = column_number - 1
304
+ if replace_with_spacer?(previous_column, column_number, row_number, flow_object.uuid)
305
+ @ordered[column_number][row_number] = MetadataPresenter::Spacer.new
306
+ end
307
+ end
308
+ end
309
+ end
310
+
311
+ def replace_with_spacer?(previous_column, current_column, row_number, uuid)
312
+ no_flow_objects?(previous_column, row_number, uuid) ||
313
+ at_different_position?(current_column, row_number, uuid)
314
+ end
315
+
316
+ # Unless the destination is the Pointer for a branch in the directly preceeding
317
+ # column, we want to swap Pointers for Spacers if there are no Flow objects
318
+ # in that row.
319
+ def no_flow_objects?(previous_column, row_number, uuid)
320
+ !branch_destination?(uuid, previous_column) &&
321
+ row_objects(row_number).none?(MetadataPresenter::Flow)
322
+ end
323
+
324
+ # The object already exists elsewhere in the flow so we can replace the Pointer
325
+ # and let the frontend draw an arrow to that position.
326
+ def at_different_position?(column_number, row_number, uuid)
327
+ @coordinates.uuid_column(uuid) != column_number &&
328
+ @coordinates.uuid_row(uuid) != row_number
329
+ end
330
+
331
+ def branch_destination?(uuid, column_number)
332
+ @ordered[column_number].any? do |flow_object|
333
+ next unless flow_object.branch?
334
+
335
+ flow_object.all_destination_uuids.include?(uuid)
336
+ end
242
337
  end
243
338
 
244
339
  # Find the very last MetadataPresenter::Flow object in every column and
@@ -254,7 +349,7 @@ module MetadataPresenter
254
349
  end
255
350
 
256
351
  def only_spacers?(trimmed_column)
257
- trimmed_column.all? { |item| item.is_a?(MetadataPresenter::Spacer) }
352
+ trimmed_column.all?(MetadataPresenter::Spacer)
258
353
  end
259
354
 
260
355
  # Each branch has a certain number of exits that require their own line
@@ -1,3 +1,3 @@
1
1
  module MetadataPresenter
2
- VERSION = '2.15.8'.freeze
2
+ VERSION = '2.15.9'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metadata_presenter
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.15.8
4
+ version: 2.15.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - MoJ Forms
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-02 00:00:00.000000000 Z
11
+ date: 2022-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: govuk_design_system_formbuilder