kennel 2.26.0 → 2.27.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1feb4824c8c6f37283c35cce058482e40621095e730712892df31b5f60ad51aa
4
- data.tar.gz: ed84b001f02a4cf3c0b6de757c3370b62e071589ea108ee93ca0cc1f3526ccfe
3
+ metadata.gz: e8a2ad2ca075e3372bf8240fcfd2311690549cac696f6f40c37739a624b0787e
4
+ data.tar.gz: 5a46c7a1daf6c9d5d26ed580c5f5b8ca9e26922d5d5dfcc945a278f5029bc361
5
5
  SHA512:
6
- metadata.gz: 146d46c37c4db293575ebb5e10eac5e06cfa47d8a5c7cf97cb817dbcd7f13c6215c06d7d6b6c415c05fb702aa5e2417a6e85d1af482ef5add7df3cbdf63e97ae
7
- data.tar.gz: 5399d92bd418b40ba21016b3b6eea57c870e33fe56f3df1d7ffc2741ae5ac6b7052189a31fb2a2369b7264873b1d666393dac3862478bf0e91b2c9c01534ae67
6
+ metadata.gz: 2ec316eaa442ddb2ffaf3f1ec233e1cfcb40c23d245d1c062f3d812d7fa7a658aa24475b811627c2abaf6ac6642b4509eef647fce645afa7e8d5e95ef94bb77e
7
+ data.tar.gz: 9875b2bc7f779c8983bf98eb43071d693cb100b23e8d68df7c86819d35d4d6b46209a6911f5c7b9438d12cf965000b6a64ad4604636f2d4d437a0ee279d7d15f
data/lib/kennel/api.rb CHANGED
@@ -90,37 +90,49 @@ module Kennel
90
90
 
91
91
  # keep widget + tab ids stable so stored dashboard urls that point at a widget or tab do not break on every update
92
92
  # ids are dropped when diffing since storing them would be ugly, so restore the previous ids by matching widget titles
93
+ # groups and widgets use separate id pools since tabs reference groups and should not end up pointing at a widget
93
94
  def restore_widget_ids(id, attributes)
94
95
  return unless (widgets = attributes[:widgets])
95
96
  return unless (current = show("dashboard", id, {}, ignore_404: true))
96
97
 
97
- # backfill widget ids
98
- widget_title_to_id = {}
99
- each_widget_with_title(current[:widgets]) { |title, widget| widget_title_to_id[title] = widget[:id] }
100
- each_widget_with_title(widgets) do |title, widget|
101
- next unless (id = widget_title_to_id[title])
102
- widget[:id] ||= id # do not set nil, that breaks updates
103
- end
98
+ old_groups, old_widgets = partition_groups(current[:widgets])
99
+ new_groups, new_widgets = partition_groups(widgets)
100
+ reuse_ids(old_groups, new_groups)
101
+ reuse_ids(old_widgets, new_widgets)
104
102
 
105
103
  return unless (tabs = attributes[:tabs])
106
104
 
107
- # backfill tag ids ... first by name, then whatever we got left, then new fake id
108
- # not setting nils so we get the proper `{"errors":["Required parameter 'id' is missing"]}` error
109
- tab_name_to_id = {}
110
- current[:tabs]&.each do |tab|
111
- tab_name_to_id[tab[:name]] = tab[:id] # collect old tab names
112
- end
113
- tabs.each do |tab|
114
- next unless (id = tab_name_to_id.delete(tab[:name])) # reused moved tabs so links keep working
115
- tab[:id] = id
116
- end
117
- tabs.each do |tab|
118
- next if tab[:id]
119
- next unless (id = tab_name_to_id.delete(tab_name_to_id.keys.first)) # reuse renamed tabs so links keep working
120
- tab[:id] ||= id
105
+ old_tabs = (current[:tabs] || []).map { |t| [t[:name], t] }
106
+ new_tabs = tabs.map { |t| [t[:name], t] }
107
+ reuse_ids(old_tabs, new_tabs)
108
+
109
+ # new tabs get a fake id since the api requires one
110
+ tabs.each { |tab| tab[:id] ||= SecureRandom.uuid }
111
+ end
112
+
113
+ # split all widgets including nested ones into [key, widget] pairs of groups and non-groups
114
+ def partition_groups(widgets)
115
+ pairs = []
116
+ each_widget_with_title(widgets) { |key, widget| pairs << [key, widget] }
117
+ pairs.partition { |_, widget| widget.dig(:definition, :type) == "group" }
118
+ end
119
+
120
+ # assign old ids to new items: exact key matches first, then leftover ids in order to reuse as many ids as possible
121
+ # not setting nils so we get the proper `{"errors":["Required parameter 'id' is missing"]}` error
122
+ def reuse_ids(old_pairs, new_pairs)
123
+ # [[key, item{id}]] -> {key -> [id]}
124
+ pool = old_pairs.group_by(&:first).transform_values { |pairs| pairs.map { |_, item| item[:id] } }
125
+
126
+ unmatched = new_pairs.filter_map do |key, item|
127
+ next item unless (id = pool[key]&.shift)
128
+ item[:id] = id
129
+ nil
121
130
  end
122
- tabs.each do |tab|
123
- tab[:id] ||= SecureRandom.uuid
131
+
132
+ leftover = pool.values.flatten
133
+ unmatched.each do |item|
134
+ break unless (id = leftover.shift)
135
+ item[:id] = id
124
136
  end
125
137
  end
126
138
 
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Kennel
3
- VERSION = "2.26.0"
3
+ VERSION = "2.27.0"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kennel
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.26.0
4
+ version: 2.27.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Grosser