ecoportal-api-v2 0.8.12 → 0.8.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 692fe87a2a9dd089ee091bae7133915ac5c9ae4b0643be6086b57206569a1044
4
- data.tar.gz: 4d9866b6e5727b176caaff881cb44a3cd13ca08418884e69283f8c64c0e80272
3
+ metadata.gz: 146b0998371f28683143b8a95ae1b68e11f8f079698502bede31c8e29369fc97
4
+ data.tar.gz: 4ce0f3dd75e4e1d822d4540fa0ff1e3e18a7c7dca1c54d7030bdb475adc21a93
5
5
  SHA512:
6
- metadata.gz: b812fc350ccdfd173d8ca7b02b05bd0676eda8dac97e2a981cd6c87e1e62c7170744a352ef962144ffb24b3bf9eb43011156c78c09f22a00db154431fd2be5ca
7
- data.tar.gz: 00c551099e991a339125e5a985f98aa918b8f9844e03b7b6ba79c6cc2fc5d9ffa4303eaee51c2acc6205932472cdf5324692a73d875e647ba0bcd685f676c60c
6
+ metadata.gz: 21605169ab88cb159ee32a2f6f36e3100b183a03a7f9c895b33631934811bb9e3f16c6bc6101ddfac562854f8762f545a743b68c55d662c97be54ff35fc5b9e8
7
+ data.tar.gz: d2a66296e4b9194667983ba5c37c646fd6a12e037138870aa2771c743fc75dbf9b2ee91d07af7b1e4b501ad1afd846b427a8b92f5dc99ee4551200cc742dcd20
data/CHANGELOG.md CHANGED
@@ -1,12 +1,27 @@
1
1
  # Change Log
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
- ## [0.8.12] - 2021-08-xx
4
+ ## [0.8.13] - 2021-08-xx
5
5
 
6
6
  ### Added
7
- - `Ecoportal::API::V2::Pages::PageStage#current_stage`
8
-
9
7
  ### Changed
8
+ ### Fixed
9
+ - `Ecoportal::API::V2::Stage::Sections`
10
+ - `weight` fixing should only happen on entire page, **not** on stage sections that could change the order of section shared with other stages
11
+ - `#scope_weight` was not excluding the section when using `weight` of the last one
12
+ - Several classes with `embeds_many` were performing the `ordering` of those elements in the wrong way
13
+ - `Ecoportall::API::Common::Content::DoubleModel`
14
+ - `#replace_original_doc` had a couple of typos
15
+ - `#replace_doc` should only remove the variable referring to the current object and only if `new_doc` is `nil`
16
+ - `Ecoportal::API::Common::Content::CollectionModel` on insertion and deletion it was removing instance variable objects.
17
+ - There was no need for this approach, as the only necessary thing is to just keep those variable instances up to date in the correct unique access point to do so.
18
+ - Aside note: the reason why this was initially designed this way is because there were some difficulties to identify the existing elements in the subjacent model. Once `#passkey` as added, this difficulty could be overcome.
19
+ - `Ecoportal::API::Common::Content::ArrayModel` on initialize, default `doc` should be an `Array`
20
+
21
+ ## [0.8.12] - 2021-08-30
22
+
23
+ ### Added
24
+ - `Ecoportal::API::V2::Pages::PageStage#current_stage`
10
25
 
11
26
  ### Fixed
12
27
  - `Ecoportal::API::Common::Content::DocHelpers#get_body` typo and wrong parameters on call.
@@ -33,7 +33,7 @@ module Ecoportal
33
33
 
34
34
  end
35
35
 
36
- def initialize(doc = {}, parent: self, key: nil)
36
+ def initialize(doc = [], parent: self, key: nil)
37
37
  super(doc, parent: parent, key: key)
38
38
  end
39
39
 
@@ -110,6 +110,9 @@ module Ecoportal
110
110
  end
111
111
 
112
112
  # Transforms `value` into the actual `key` to access the object in the doc `Array`
113
+ # @note
114
+ # - The name of the method is after the paren't class method
115
+ # - This method would have been better called `_doc_pos` :)
113
116
  def _doc_key(value)
114
117
  #print "*(#{value.class})"
115
118
  return super(value) unless value.is_a?(Hash) || value.is_a?(Content::DoubleModel)
@@ -167,7 +170,10 @@ module Ecoportal
167
170
  if item = self[value]
168
171
  item.replace_doc(item_doc)
169
172
  else
170
- pos_idx = _doc_upsert(item_doc, pos: pos, before: before, after: after)
173
+ _doc_upsert(item_doc, pos: pos, before: before, after: after).tap do |pos_idx|
174
+ _items.insert(pos_idx, new_item(item_doc))
175
+ @indexed = false
176
+ end
171
177
  end
172
178
  (item || self[item_doc]).tap do |item|
173
179
  yield(item) if block_given?
@@ -185,6 +191,8 @@ module Ecoportal
185
191
  end
186
192
  if item = self[value]
187
193
  _doc_delete(item.doc)
194
+ @indexed = false
195
+ _items.delete(item)
188
196
  end
189
197
  end
190
198
 
@@ -195,7 +203,8 @@ module Ecoportal
195
203
  def items_key; self.class.items_key; end
196
204
 
197
205
  def on_change
198
- variables_remove!
206
+ @indexed = false
207
+ #variables_remove!
199
208
  end
200
209
 
201
210
  # Gets the `key` of the object `value`
@@ -253,9 +262,7 @@ module Ecoportal
253
262
  # @return [Object] the element deleted from `doc`
254
263
  def _doc_delete(value)
255
264
  if current_pos = _doc_key(value)
256
- _doc_items.delete_at(current_pos).tap do |deleted|
257
- on_change
258
- end
265
+ _doc_items.delete_at(current_pos)
259
266
  end
260
267
  end
261
268
 
@@ -275,7 +282,6 @@ module Ecoportal
275
282
  pos = (pos && pos < _doc_items.length)? pos : _doc_items.length
276
283
  pos.tap do |i|
277
284
  _doc_items.insert(pos, value)
278
- on_change
279
285
  end
280
286
 
281
287
  end
@@ -240,7 +240,7 @@ module Ecoportal
240
240
  end
241
241
 
242
242
  inheritable_class_vars :forced_model_keys
243
-
243
+
244
244
  attr_reader :_parent, :_key
245
245
 
246
246
  def initialize(doc = {}, parent: self, key: nil)
@@ -367,8 +367,8 @@ module Ecoportal
367
367
  @doc = new_doc
368
368
  else
369
369
  dig_set(_parent.doc, [_doc_key(_key)].flatten, new_doc)
370
- _parent.variable_remove!(_key)
371
- variables_remove!
370
+ _parent.variable_remove!(_key) unless new_doc
371
+ #variables_remove!
372
372
  end
373
373
  end
374
374
 
@@ -385,9 +385,9 @@ module Ecoportal
385
385
  def replace_original_doc(new_doc)
386
386
  raise UnlinkedModel.new(from: "#{self.class}#replace_original_doc", key: _key) unless linked?
387
387
  if is_root?
388
- @orginal_doc = new_doc
388
+ @original_doc = new_doc
389
389
  else
390
- dig_set(_parent.orginal_doc, [_doc_key(_key)].flatten, new_doc)
390
+ dig_set(_parent.original_doc, [_doc_key(_key)].flatten, new_doc)
391
391
  end
392
392
  end
393
393
 
@@ -409,11 +409,21 @@ module Ecoportal
409
409
 
410
410
  # Removes all the persistent variables
411
411
  def variables_remove!
412
+ #puts "going to remove vars: #{@_dim_vars} on #{self.class} (parent: #{identify_parent(self._parent)})"
412
413
  @_dim_vars.dup.map {|k| variable_remove!(k)}
413
414
  end
414
415
 
415
416
  private
416
417
 
418
+ def identify_parent(object)
419
+ case object
420
+ when Ecoportal::API::V2::Page::Stage
421
+ "stage #{object.name}"
422
+ when Ecoportal::API::V2::Page::Section
423
+ "section #{object.heading}"
424
+ end
425
+ end
426
+
417
427
  def instance_variable_name(key)
418
428
  self.class.instance_variable_name(key)
419
429
  end
@@ -8,7 +8,7 @@ module Ecoportal
8
8
  def new_doc
9
9
  {
10
10
  "id" => new_uuid,
11
- "weight" => 9999
11
+ "weight" => 99
12
12
  }
13
13
  end
14
14
  end
@@ -25,9 +25,9 @@ module Ecoportal
25
25
  end
26
26
 
27
27
  def ordered_tasks
28
- actions.each_with_index.sort_by do |task, index|
29
- (task.weight >= 9999) ? [index, index] : [task.weight, index]
30
- end.map(&:first)
28
+ actions.sort_by.with_index do |task, index|
29
+ [task.weight, index]
30
+ end
31
31
  end
32
32
 
33
33
  # Quick config helper
@@ -19,9 +19,9 @@ module Ecoportal
19
19
  end
20
20
 
21
21
  def ordered_items
22
- items.each_with_index.sort_by do |item, index|
23
- (item.weight >= 9999) ? [index, index] : [item.weight, index]
24
- end.map(&:first)
22
+ items.sort_by.with_index do |item, index|
23
+ [item.weight, index]
24
+ end
25
25
  end
26
26
 
27
27
  private
@@ -8,7 +8,7 @@ module Ecoportal
8
8
  def new_doc
9
9
  {
10
10
  "id" => new_uuid,
11
- "position" => 9999
11
+ "position" => 99
12
12
  }
13
13
  end
14
14
  end
@@ -19,9 +19,9 @@ module Ecoportal
19
19
  end
20
20
 
21
21
  def ordered_files
22
- items.each_with_index.sort_by do |file, index|
23
- (file.position >= 9999) ? [index, index] : [file.position, index]
24
- end.map(&:first)
22
+ items.sort_by.with_index do |file, index|
23
+ [file.position, index]
24
+ end
25
25
  end
26
26
 
27
27
  private
@@ -21,9 +21,9 @@ module Ecoportal
21
21
  end
22
22
 
23
23
  def ordered_stops
24
- stops.each_with_index.sort_by do |stop, index|
25
- (stop.threshold >= 9999) ? [index, index] : [stop.threshold, index]
26
- end.map(&:first)
24
+ stops.sort_by.with_index do |stop, index|
25
+ [stop.threshold, index]
26
+ end
27
27
  end
28
28
 
29
29
  end
@@ -58,9 +58,9 @@ module Ecoportal
58
58
  end
59
59
 
60
60
  def ordered_options
61
- options.each_with_index.sort_by do |option, index|
62
- (option.weight >= 9999) ? [index, index] : [option.weight, index]
63
- end.map(&:first)
61
+ options.sort_by.with_index do |option, index|
62
+ [option.weight, index]
63
+ end
64
64
  end
65
65
 
66
66
  # Quick config helper
@@ -3,12 +3,14 @@ module Ecoportal
3
3
  class V2
4
4
  class Page
5
5
  class Section < Common::Content::DoubleModel
6
+ INITIAL_WEIGHT = 9999
7
+
6
8
  class << self
7
9
  def new_doc(split: false)
8
10
  {
9
11
  "id" => new_uuid,
10
12
  "type" => split ? "split" : "content",
11
- "weight" => 800
13
+ "weight" => INITIAL_WEIGHT
12
14
  }.tap do |out|
13
15
  component_ids = if split
14
16
  {
@@ -78,8 +80,9 @@ module Ecoportal
78
80
  # @param field [Ecoportal::API::V2::Page::Component] the field to be added.
79
81
  def add_component(field, after: nil, side: nil)
80
82
  raise "field should be a Ecoportal::API::V2::Page::Component. Given: #{field.class}" unless field.is_a?(Ecoportal::API::V2::Page::Component)
83
+ # IMPORTANT NOTE: The code below creates objects, because field.section does a search on section.component_ids
81
84
  if field.section == self
82
- raise "Field with id '#{field.id}' already belongs to this section"
85
+ puts "Field with id '#{field.id}' already belongs to this section"
83
86
  elsif sec = field.section
84
87
  # Field belongs to another section
85
88
  raise "Field with id '#{field.id}' belongs to section '#{sec.heading || "Unnamed"}' (id: '#{sec.id}')"
@@ -92,6 +95,7 @@ module Ecoportal
92
95
  ids_ary = component_ids
93
96
  fields = components
94
97
  end
98
+
95
99
  if after
96
100
  after_fld = fields.find do |fld|
97
101
  found = nil
@@ -100,6 +104,7 @@ module Ecoportal
100
104
  found ||= same_string?(fld.label, after)
101
105
  end
102
106
  end
107
+
103
108
  ids_ary.insert_one(field.id, after: after_fld&.id)
104
109
  self
105
110
  end
@@ -8,11 +8,15 @@ module Ecoportal
8
8
  self.klass = :section_class
9
9
 
10
10
  # Creates a new `section`
11
+ # @note
12
+ # - It won't fix weights unless all the sections of the ooze are present
13
+ # - This means that it doesn't fix section weights on stages,
14
+ # as shared sections could change order in other stages
11
15
  def add(name: nil, split: false, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
12
16
  sec_doc = section_class.new_doc(split: split)
13
17
  upsert!(sec_doc) do |section| #, pos: pos, before: before, after: after) do |section|
14
18
  section.heading = name
15
- if weight = scope_weight(pos: pos, before: before, after: after)
19
+ if weight = scope_weight(section, pos: pos, before: before, after: after)
16
20
  section.weight = weight
17
21
  end
18
22
  fix_weights!
@@ -51,13 +55,13 @@ module Ecoportal
51
55
  # Gets the sections ordered by `weight` (as they appear in the page)
52
56
  def ordered
53
57
  self.sort_by.with_index do |section, index|
54
- (section.weight >= 9999) ? [index, index] : [section.weight, index]
58
+ [section.weight, index]
55
59
  end
56
60
  end
57
61
 
58
62
  private
59
63
 
60
- def scope_weight(pos: NOT_USED, before: NOT_USED, after: NOT_USED)
64
+ def scope_weight(section, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
61
65
  case
62
66
  when used_param?(pos)
63
67
  if pos = to_section(pos)
@@ -71,6 +75,11 @@ module Ecoportal
71
75
  if after = to_section(after)
72
76
  after.weight
73
77
  end
78
+ end.yield_self do |weight|
79
+ weight = ordered.reject do |sec|
80
+ sec.id == section.id
81
+ end.last&.weight
82
+ weight ||= section_class.const_get(:INITIAL_WEIGHT)
74
83
  end
75
84
  end
76
85
 
@@ -86,8 +95,10 @@ module Ecoportal
86
95
  end
87
96
 
88
97
  def fix_weights!
89
- ordered.each_with_index do |section, index|
90
- section.weight = index
98
+ unless self._parent.is_a?(Ecoportal::API::V2::Pages::PageStage)
99
+ ordered.each_with_index do |section, index|
100
+ section.weight = index
101
+ end
91
102
  end
92
103
  end
93
104
 
@@ -22,7 +22,7 @@ module Ecoportal
22
22
  root.sections.values_at(*sec_ids).select.with_index do |sec, i|
23
23
  puts "Warning: section #{id} points to missing section #{sec_ids[i]}" if !sec
24
24
  fld && (!block_given? || yield(sec))
25
- end.sort_by {|sec| sec.weight}
25
+ end.sort_by.with_index {|sec, index| [sec.weight, index]}
26
26
  end
27
27
 
28
28
  def add_section(*secs)
@@ -14,9 +14,9 @@ module Ecoportal
14
14
  end
15
15
 
16
16
  #def ordered
17
- # self.each_with_index.sort_by do |stage, index|
18
- # (stage.ordering >= 9999) ? [index, index] : [stage.ordering, index]
19
- # end.map(&:first)
17
+ # self.sort_by.with_index do |stage, index|
18
+ # [stage.ordering, index]
19
+ # end
20
20
  #end
21
21
 
22
22
  end
@@ -29,6 +29,10 @@ module Ecoportal
29
29
  embeds_many :sections, enum_class: :sections_class
30
30
  embeds_many :stages, enum_class: :stages_class
31
31
 
32
+ def initialize(doc = [], parent: self, key: nil)
33
+ super(_doc_bug_fix(doc), parent: parent, key: key)
34
+ end
35
+
32
36
  def as_update
33
37
  super.tap do |hash|
34
38
  unless !hash
@@ -44,6 +48,35 @@ module Ecoportal
44
48
  self.stages.count > 0
45
49
  end
46
50
 
51
+ private
52
+
53
+ def _doc_bug_fix(hash)
54
+ hash.tap do |hash|
55
+ _fix_doc(hash["stages"], "flow_node_ids", "section_ids") if hash.key?("stages")
56
+ if hash.key?("sections")
57
+ _fix_doc(hash["sections"], "membrane_ids", "component_ids")
58
+ _fix_doc(hash["sections"], "left_membrane_ids", "left_component_ids")
59
+ _fix_doc(hash["sections"], "right_membrane_ids", "right_component_ids")
60
+ end
61
+ end
62
+ end
63
+
64
+ def _fix_doc(value, source, dest)
65
+ value.tap do |value|
66
+ case value
67
+ when Array
68
+ value.each {|v| _fix_doc(v, source, dest)}
69
+ when Hash
70
+ if value.key?(source) && !value.key?(dest)
71
+ value[dest] = value[source]
72
+ value.delete(source)
73
+ end
74
+ else
75
+ # Do nothing!
76
+ end
77
+ end
78
+ end
79
+
47
80
  end
48
81
  end
49
82
  end
@@ -1,5 +1,5 @@
1
1
  module Ecoportal
2
2
  module API
3
- GEM2_VERSION = "0.8.12"
3
+ GEM2_VERSION = "0.8.13"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ecoportal-api-v2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.12
4
+ version: 0.8.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oscar Segura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-30 00:00:00.000000000 Z
11
+ date: 2021-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler