lib/ecoportal/api/common/content/collection_model.rb in ecoportal-api-v2-0.8.12 vs lib/ecoportal/api/common/content/collection_model.rb in ecoportal-api-v2-0.8.13
- old
+ new
@@ -108,10 +108,13 @@
def items_class
self.class.klass
end
# Transforms `value` into the actual `key` to access the object in the doc `Array`
+ # @note
+ # - The name of the method is after the paren't class method
+ # - This method would have been better called `_doc_pos` :)
def _doc_key(value)
#print "*(#{value.class})"
return super(value) unless value.is_a?(Hash) || value.is_a?(Content::DoubleModel)
if id = get_key(value)
#print "^"
@@ -165,11 +168,14 @@
item_doc = value.is_a?(Content::DoubleModel)? value.doc : value
item_doc = JSON.parse(item_doc.to_json)
if item = self[value]
item.replace_doc(item_doc)
else
- pos_idx = _doc_upsert(item_doc, pos: pos, before: before, after: after)
+ _doc_upsert(item_doc, pos: pos, before: before, after: after).tap do |pos_idx|
+ _items.insert(pos_idx, new_item(item_doc))
+ @indexed = false
+ end
end
(item || self[item_doc]).tap do |item|
yield(item) if block_given?
end
end
@@ -183,21 +189,24 @@
unless value.is_a?(Hash) || value.is_a?(Content::DoubleModel) || value.is_a?(String)
raise "'Content::DoubleModel' or 'Hash' doc required"
end
if item = self[value]
_doc_delete(item.doc)
+ @indexed = false
+ _items.delete(item)
end
end
protected
def order_matters?; self.class.order_matters; end
def uniq?; self.class.uniq; end
def items_key; self.class.items_key; end
def on_change
- variables_remove!
+ @indexed = false
+ #variables_remove!
end
# Gets the `key` of the object `value`
def get_key(value)
case value
@@ -251,13 +260,11 @@
# Deletes `value` from `doc` (here referred as `_doc_items`)
# @return [Object] the element deleted from `doc`
def _doc_delete(value)
if current_pos = _doc_key(value)
- _doc_items.delete_at(current_pos).tap do |deleted|
- on_change
- end
+ _doc_items.delete_at(current_pos)
end
end
def _doc_upsert(value, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
current_pos = if elem = self[value]
@@ -273,10 +280,9 @@
end
pos = (pos && pos < _doc_items.length)? pos : _doc_items.length
pos.tap do |i|
_doc_items.insert(pos, value)
- on_change
end
end
def scope_position(pos: NOT_USED, before: NOT_USED, after: NOT_USED)