ecoportal-api-v2 0.9.1 → 0.9.2

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: fd6779bebb389337e0a18e355c36cbb5c2ba0ed84f31b7cdbca3bec051a30bd2
4
- data.tar.gz: 32e9cc407394ef5ae7ef56f87650180789d229930daf5d7e65d44c33768041ca
3
+ metadata.gz: 172ffff0cc645bf064114c90364ee2a0593ebc6e1d6e1ac4fd5095774c538cc6
4
+ data.tar.gz: bde0a6b28097ddc8bb971a6f0f1ad622da4d87298651122952bc90cf4686cd10
5
5
  SHA512:
6
- metadata.gz: f2e84e20a4952e07ce7575f98a32b7cd216dbdc163fed0dcf9b9e383062e509d0702cff679b51e02b235afa0b68add386638d41eb5dc7cfcaaabfd965288530b
7
- data.tar.gz: d0b3a2d2043a31cca656c98a812dc2e632ae565c121cc85991c211b51acafebb58206be305dbc9da1fcfaf23acc83218ddb1db952d43b76fb5fccc1bc933bde9
6
+ metadata.gz: 9e5f80eb2dfb8623dde76445f5c85e86baa9a14327c3599840e6ef1a3de822cc40b61ba9dad7b6749c40c9cde11af43643571a61003609b9b51571d81c366a2a
7
+ data.tar.gz: 909755671b618ebe4187ac016ca0c1ea9b75837f70c29639bb0fbdde8ecba39f0855e0c1527de558fce5c4f20754a36bfa03a565bed99704d034f39d630ff074
data/CHANGELOG.md CHANGED
@@ -1,12 +1,31 @@
1
1
  # Change Log
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
- ## [0.9.2] - 2022-09-26
4
+ ## [0.9.3] - 2022-09-xx
5
5
 
6
6
  ### Added
7
7
  ### Changed
8
8
  ### Fixed
9
+ - Remove debugging message
9
10
 
11
+ ## [0.9.2] - 2022-09-29
12
+
13
+ ### Added
14
+ - `Ecoportal::API::V2::Page::Component::SelectionOption#delete!`
15
+ - `Ecoportal::API::V2::Page::Component#delete!`
16
+ - `Ecoportal::API::V2::Page::Component#move` to move to another section or side of the same
17
+ - `Ecoportal::API::V2::Page::Component#unattach!` to detach the component from any section
18
+ - `Ecoportal::API::V2::Page::Component::SelectionOption#delete!`
19
+ - `Ecoportal::API::V2::Page::Component::Binding#delete!`
20
+ - `Ecoportal::API::V2::Page::Section#remove_component!` to simplify the removal of a field from the section
21
+
22
+ ### Changed
23
+ - `Ecoportal::API::V2::Page::Component::SelectionField#options`
24
+ - **added** and moved logics to `Ecoportal::API::V2::Page::Component::SelectionOptions`
25
+ - `Ecoportal::API::V2::Page::Component::SelectionOption#name` to return `#value` when it's empty
26
+
27
+ ### Fixed
28
+ - Remove debugging message
10
29
 
11
30
  ## [0.9.1] - 2022-09-26
12
31
 
@@ -16,9 +35,6 @@ All notable changes to this project will be documented in this file.
16
35
  ### Changed
17
36
  - `Ecoportal::API::V2::Page::Force`, logic around `helpers` (remote forces)
18
37
  - **breaking change**: `script` is not writable anymore. You should use `custom_script` instead.
19
-
20
- ### Fixed
21
-
22
38
 
23
39
  ## [0.8.33] - 2022-09-23
24
40
 
@@ -278,7 +278,6 @@ module Ecoportal
278
278
  _items.delete(value)
279
279
  end
280
280
  end
281
-
282
281
  end
283
282
  end
284
283
  end
@@ -240,7 +240,6 @@ module Ecoportal
240
240
  def model_forced_keys
241
241
  @forced_model_keys ||= {}
242
242
  end
243
-
244
243
  end
245
244
 
246
245
  inheritable_class_vars :forced_model_keys, :key
@@ -7,8 +7,7 @@ module Ecoportal
7
7
  passboolean :multiple, :flat, :other
8
8
  passthrough :other_desc
9
9
  passthrough :data_type
10
-
11
- embeds_many :options, klass: "Ecoportal::API::V2::Page::Component::SelectionOption", order_key: :weight
10
+ embeds_many :options, enum_class: "Ecoportal::API::V2::Page::Component::SelectionOptions"
12
11
 
13
12
  def numeric?
14
13
  self.data_type == "num"
@@ -19,12 +18,12 @@ module Ecoportal
19
18
  end
20
19
 
21
20
  def numeric!(&block)
22
- ordered_options.each {|opt| opt.numeric!(&block)}
21
+ options.ordered.each {|opt| opt.numeric!(&block)}
23
22
  self.data_type = "num"
24
23
  end
25
24
 
26
25
  def text!(&block)
27
- ordered_options.each {|opt| opt.text!(&block)}
26
+ options.ordered.each {|opt| opt.text!(&block)}
28
27
  self.data_type = "str"
29
28
  end
30
29
 
@@ -33,7 +32,7 @@ module Ecoportal
33
32
  end
34
33
 
35
34
  def select(value_name, by_name: false)
36
- opt = find_option(value_name, by_name: by_name)
35
+ opt = options.find_option(value_name, by_name: by_name)
37
36
  sel = selected
38
37
  return true if !multiple && opt == sel
39
38
  sel.selected = false if !multiple && sel
@@ -41,7 +40,7 @@ module Ecoportal
41
40
  end
42
41
 
43
42
  def deselect(value_name, by_name: false)
44
- if opt = find_option(value_name, by_name: by_name)
43
+ if opt = options.find_option(value_name, by_name: by_name)
45
44
  opt.selected = false
46
45
  end
47
46
  end
@@ -50,17 +49,17 @@ module Ecoportal
50
49
  case
51
50
  when by_value
52
51
  elems = [selected].flatten.compact
53
- options_hash(elems) do |option|
52
+ options.hash(elems) do |option|
54
53
  name ? option.name : option
55
54
  end
56
55
  when by_name
57
56
  elems = [selected].flatten.compact
58
- options_hash(elems, by_name: true) do |option|
57
+ options.hash(elems, by_name: true) do |option|
59
58
  value ? option.value : option
60
59
  end
61
60
  else
62
61
  if multiple
63
- ordered_options.select {|opt| opt.selected}
62
+ options.ordered.select {|opt| opt.selected}
64
63
  else
65
64
  options.find {|opt| opt.selected}
66
65
  end
@@ -91,43 +90,23 @@ module Ecoportal
91
90
  [name].flatten.compact
92
91
  end
93
92
 
94
- def add_option(value:, name: nil, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
95
- opt_doc = options.items_class.new_doc
96
- options.upsert!(opt_doc, pos: pos, before: before, after: after) do |option|
97
- option.name = name
98
- option.value = value
99
- if prev = previous_option(option)
100
- option.weight = prev.weight
101
- end
102
- yield(option) if block_given?
103
- fix_option_weights!
104
- end
93
+ # @see Ecoportal::API::V2::Component::SelectionOptions#add
94
+ def add_option(**kargs, &block)
95
+ options.add(**kargs, &block)
105
96
  end
106
97
 
107
98
  def ordered_options
108
- options.sort_by.with_index do |option, index|
109
- [option.weight, index]
110
- end
99
+ options.ordered
111
100
  end
112
101
 
113
- # @param name [Boolean] whether or not the values of the `Hash` should be the `names`
114
- # @return [Hash] of **key** _value_ and **value**:
115
- # 1. _option_ if `name` is `false`
116
- # 2. _name_ if `name` is `true`
102
+ # @see Ecoportal::API::V2::Component::SelectionOptions#by_value
117
103
  def options_by_value(name: false)
118
- options_hash do |option|
119
- name ? option.name : option
120
- end
104
+ options.by_value(name: name)
121
105
  end
122
106
 
123
- # @param value [Boolean] whether or not the values of the `Hash` should be the `values`
124
- # @return [Hash] of **key** _name_ and **value**:
125
- # 1. _option_ if `value` is `false`
126
- # 2. _value_ if `value` is `true`
107
+ # @see Ecoportal::API::V2::Component::SelectionOptions#by_name
127
108
  def options_by_name(value: false)
128
- options_hash(by_name: true) do |option|
129
- value ? option.value : option
130
- end
109
+ options.by_name(value: value)
131
110
  end
132
111
 
133
112
  def to_s(value: true, delimiter: "\n")
@@ -198,34 +177,6 @@ module Ecoportal
198
177
  end
199
178
  end
200
179
  end
201
-
202
- def fix_option_weights!
203
- ordered_options.each_with_index do |option, index|
204
- option.weight = index
205
- end
206
- end
207
-
208
- def previous_option(value)
209
- opts = ordered_options
210
- pos = opts.index(value) - 1
211
- return if pos < 0
212
- opts[pos]
213
- end
214
-
215
- def find_option(value_name, by_name: false)
216
- if by_name
217
- opt = options.find {|opt| same_string?(opt.name, value_name)}
218
- else
219
- opt = options.find {|opt| opt.value == value_name}
220
- end
221
- end
222
-
223
- def options_hash(elems = ordered_options, by_name: false)
224
- elems.each_with_object({}) do |option, hash|
225
- key = by_name ? option.name : option.value
226
- hash[key] = block_given?? yield(option) : option
227
- end
228
- end
229
180
  end
230
181
  end
231
182
  end
@@ -233,4 +184,5 @@ module Ecoportal
233
184
  end
234
185
  end
235
186
 
187
+ require 'ecoportal/api/v2/page/component/selection_options'
236
188
  require 'ecoportal/api/v2/page/component/selection_option'
@@ -4,7 +4,6 @@ module Ecoportal
4
4
  class Page
5
5
  class Component
6
6
  class SelectionOption < Common::Content::DoubleModel
7
-
8
7
  class << self
9
8
  def new_doc
10
9
  {
@@ -20,6 +19,14 @@ module Ecoportal
20
19
  passthrough :weight
21
20
  passboolean :selected
22
21
 
22
+ def name
23
+ doc["name"] || "#{value}"
24
+ end
25
+
26
+ def delete!
27
+ _parent.delete!(self)
28
+ end
29
+
23
30
  def numeric!
24
31
  self.value = block_given?? yield(value) : to_i(value)
25
32
  end
@@ -0,0 +1,93 @@
1
+ module Ecoportal
2
+ module API
3
+ class V2
4
+ class Page
5
+ class Component
6
+ class SelectionOptions < Common::Content::CollectionModel
7
+ class_resolver :option_class, "Ecoportal::API::V2::Page::Component::SelectionOption"
8
+
9
+ self.klass = :option_class
10
+ self.order_key = :weight
11
+
12
+ def component
13
+ self._parent
14
+ end
15
+
16
+ def ooze
17
+ component.ooze
18
+ end
19
+
20
+ def add(value:, name: nil, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
21
+ opt_doc = items_class.new_doc
22
+ upsert!(opt_doc, pos: pos, before: before, after: after) do |option|
23
+ option.name = name
24
+ option.value = value
25
+ if prev = previous_option(option)
26
+ option.weight = prev.weight
27
+ end
28
+ yield(option) if block_given?
29
+ fix_weights!
30
+ end
31
+ end
32
+
33
+ def find_option(value_name, by_name: false)
34
+ if by_name
35
+ opt = self.find {|opt| same_string?(opt.name, value_name)}
36
+ else
37
+ opt = self.find {|opt| opt.value == value_name}
38
+ end
39
+ end
40
+
41
+ def ordered
42
+ self.sort_by.with_index do |option, index|
43
+ [option.weight, index]
44
+ end
45
+ end
46
+
47
+ # @param name [Boolean] whether or not the values of the `Hash` should be the `names`
48
+ # @return [Hash] of **key** _value_ and **value**:
49
+ # 1. _option_ if `name` is `false`
50
+ # 2. _name_ if `name` is `true`
51
+ def by_value(name: false)
52
+ hash do |option|
53
+ name ? option.name : option
54
+ end
55
+ end
56
+
57
+ # @param value [Boolean] whether or not the values of the `Hash` should be the `values`
58
+ # @return [Hash] of **key** _name_ and **value**:
59
+ # 1. _option_ if `value` is `false`
60
+ # 2. _value_ if `value` is `true`
61
+ def by_name(value: false)
62
+ hash(by_name: true) do |option|
63
+ value ? option.value : option
64
+ end
65
+ end
66
+
67
+ def hash(elems = ordered, by_name: false)
68
+ elems.each_with_object({}) do |option, hash|
69
+ key = by_name ? option.name : option.value
70
+ hash[key] = block_given?? yield(option) : option
71
+ end
72
+ end
73
+
74
+ private
75
+
76
+ def fix_weights!
77
+ ordered.each_with_index do |option, index|
78
+ option.weight = index
79
+ end
80
+ end
81
+
82
+ def previous_option(value)
83
+ opts = ordered
84
+ pos = opts.index(value) - 1
85
+ return if pos < 0
86
+ opts[pos]
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -118,6 +118,16 @@ module Ecoportal
118
118
  !!section
119
119
  end
120
120
 
121
+ # Unattaches the field from any section
122
+ # @note please make sure to reattach or delete! the field before save
123
+ def unattach!
124
+ ooze.sections.select do |sec|
125
+ sec.component?(id)
126
+ end.each do |sec|
127
+ sec.remove_component!(self)
128
+ end
129
+ end
130
+
121
131
  # @return [Boolean] whether or not the component has been attached to more than one section.
122
132
  def multi_section?
123
133
  secs = ooze.sections.select {|sec| sec.component?(id)}
@@ -144,6 +154,17 @@ module Ecoportal
144
154
  forces.count > 0
145
155
  end
146
156
 
157
+ def delete!
158
+ bindings.each {|b| b.delete!}
159
+ self.unattach!
160
+ self._parent.delete!(self)
161
+ end
162
+
163
+ def move(section:, before: nil, after: nil, side: nil)
164
+ self.unattach!
165
+ section.add_component(self, before: before, after: after, side: side)
166
+ end
167
+
147
168
  def indexable_label(any_length: false)
148
169
  self.class.indexable_label(label, any_length: any_length)
149
170
  end
@@ -52,6 +52,10 @@ module Ecoportal
52
52
  def reference
53
53
  self.component? ? component : section
54
54
  end
55
+
56
+ def delete!
57
+ self._parent.delete!(self)
58
+ end
55
59
  end
56
60
  end
57
61
  end
@@ -174,7 +174,6 @@ module Ecoportal
174
174
  get_by_name(value).first
175
175
  end
176
176
  end
177
-
178
177
  end
179
178
  end
180
179
  end
@@ -7,6 +7,7 @@ module Ecoportal
7
7
 
8
8
  self.klass = :force_class
9
9
  order_matters = true
10
+ self.order_key = :weight
10
11
 
11
12
  def ooze
12
13
  self._parent.ooze
@@ -83,6 +83,27 @@ module Ecoportal
83
83
  left_component_ids.to_a | right_component_ids.to_a
84
84
  end
85
85
 
86
+ # @note when removing a component, please make sure to either
87
+ # add it to another section or to delete! it before save
88
+ def remove_component!(*comps_or_ids)
89
+ comps_or_ids.each do |com_or_id|
90
+ case com_or_id
91
+ when Ecoportal::API::V2::Page::Component
92
+ remove_component!(com_or_id.id)
93
+ when String
94
+ if split?
95
+ left_component_ids.delete!(com_or_id)
96
+ right_component_ids.delete!(com_or_id)
97
+ else
98
+ component_ids.delete!(com_or_id)
99
+ end
100
+ else
101
+ msg = "Missuse: com_or_id should be a Component or a String. Given: #{com_or_id.class}"
102
+ raise ArgumentError.new(msg)
103
+ end
104
+ end
105
+ end
106
+
86
107
  # @raise [ArgumentError] if `com_or_id` is not of any of the allowed types
87
108
  # @param com_or_id [Ecoportal::API::V2::Page::Component, String] Component or `id` thereof
88
109
  # @return [Boolean] whether or not a component/field belongs to this section.
@@ -98,14 +119,32 @@ module Ecoportal
98
119
  end
99
120
  end
100
121
 
122
+ # @return[Symbol, Nil] might be `:right`, maybe `:left`, or `nil`
123
+ def component_side(com_or_id)
124
+ return nil unless split?
125
+ case com_or_id
126
+ when Ecoportal::API::V2::Page::Component
127
+ component_side(com_or_id.id)
128
+ when String
129
+ if left_component_ids.include?(com_or_id)
130
+ :left
131
+ elsif right_component_ids.include?(com_or_id)
132
+ :right
133
+ end
134
+ else
135
+ msg = "Missuse: com_or_id should be a Component or a String. Given: #{com_or_id.class}"
136
+ raise ArgumentError.new(msg)
137
+ end
138
+ end
139
+
101
140
  # It looks up the components that belong to this section.
102
141
  # @return [Array<Ecoportal::API::V2::Page::Component>]
103
142
  def components(side: nil)
104
143
  case side
105
144
  when :right
106
- components_right
145
+ right_components
107
146
  when :left
108
- components_left
147
+ left_components
109
148
  when NilClass
110
149
  components_by_id(*all_component_ids)
111
150
  else
@@ -116,7 +155,7 @@ module Ecoportal
116
155
  # It looks up the components that belong to the `left` side of this section.
117
156
  # @raise [Exception] if this is not a `split` section
118
157
  # @return [Array<Ecoportal::API::V2::Page::Component>]
119
- def components_left
158
+ def left_components
120
159
  raise "You are trying to retrieve side components in a Split Section" unless split?
121
160
  components_by_id(*left_component_ids)
122
161
  end
@@ -124,7 +163,7 @@ module Ecoportal
124
163
  # It looks up the components that belong to the `right` side of this section.
125
164
  # @raise [Exception] if this is not a `split` section
126
165
  # @return [Array<Ecoportal::API::V2::Page::Component>]
127
- def components_right
166
+ def right_components
128
167
  raise "You are trying to retrieve side components in a Split Section" unless split?
129
168
  components_by_id(*right_component_ids)
130
169
  end
@@ -159,9 +198,13 @@ module Ecoportal
159
198
  end
160
199
 
161
200
  if before
162
- before_fld = to_component(before, side: side)
201
+ if before_fld = to_component(before, side: side)
202
+ side ||= component_side(before_fld)
203
+ end
163
204
  elsif after
164
- after_fld = to_component(after, side: side)
205
+ if after_fld = to_component(after, side: side)
206
+ side ||= component_side(after_fld)
207
+ end
165
208
  end
166
209
 
167
210
  if split?
@@ -176,20 +219,14 @@ module Ecoportal
176
219
  private
177
220
 
178
221
  def components_by_id(*ids)
179
- root.components.values_at(*ids).select.with_index do |fld, i|
222
+ ooze.components.values_at(*ids).select.with_index do |fld, i|
180
223
  puts "Warning: field id #{ids[i]} points to missing field" if !fld
181
224
  fld && (!block_given? || yield(fld))
182
225
  end
183
226
  end
184
227
 
185
228
  def to_component(value, side: nil)
186
- if split?
187
- fields = components(side: side || :left)
188
- else
189
- fields = components
190
- end
191
-
192
- fields.find do |fld|
229
+ components(side: side).find do |fld|
193
230
  found = nil
194
231
  found ||= fld.id == value.id if value.is_a?(Ecoportal::API::V2::Page::Component)
195
232
  found ||= fld.id == value
@@ -6,7 +6,8 @@ module Ecoportal
6
6
  class_resolver :section_class, "Ecoportal::API::V2::Page::Section"
7
7
 
8
8
  self.klass = :section_class
9
-
9
+ self.order_key = :weight
10
+
10
11
  def ooze
11
12
  self._parent.ooze
12
13
  end
@@ -6,7 +6,8 @@ module Ecoportal
6
6
  class_resolver :stage_class, "Ecoportal::API::V2::Page::Stage"
7
7
 
8
8
  self.klass = :stage_class
9
-
9
+ self.order_key = :ordering
10
+
10
11
  def ooze
11
12
  self._parent.ooze
12
13
  end
@@ -1,5 +1,5 @@
1
1
  module Ecoportal
2
2
  module API
3
- GEM2_VERSION = "0.9.1"
3
+ GEM2_VERSION = "0.9.2"
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.9.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oscar Segura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-09-25 00:00:00.000000000 Z
11
+ date: 2022-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -216,6 +216,7 @@ files:
216
216
  - lib/ecoportal/api/v2/page/component/rich_text_field.rb
217
217
  - lib/ecoportal/api/v2/page/component/selection_field.rb
218
218
  - lib/ecoportal/api/v2/page/component/selection_option.rb
219
+ - lib/ecoportal/api/v2/page/component/selection_options.rb
219
220
  - lib/ecoportal/api/v2/page/component/signature_field.rb
220
221
  - lib/ecoportal/api/v2/page/component/tag_field.rb
221
222
  - lib/ecoportal/api/v2/page/components.rb