Sha256: d3f69eabeb71f6e0292249d501fc3a69bcd9b0c2320cbfe5c0710e801676b008
Contents?: true
Size: 1.59 KB
Versions: 26
Compression:
Stored size: 1.59 KB
Contents
module Ecoportal module API class V2 class Page class Component class FilesField < Page::Component embeds_many :items, klass: "Ecoportal::API::V2::Page::Component::File", order_key: :position def empty? items.empty? end def add_file(container_id, pos: NOT_USED, before: NOT_USED, after: NOT_USED) file_doc = items.items_class.new_doc items.upsert!(file_doc, pos: pos, before: before, after: after) do |file| file.file_container_id = container_id if prev = previous_file(file) file.position = prev.position end yield(file) if block_given? fix_file_positions! end end def ordered_files items.sort_by.with_index do |file, index| [file.position, index] end end def to_s(delimiter: "\n") ordered_files.map(&:to_s).join(delimiter) end private def fix_file_positions! ordered_files.each_with_index do |file, index| file.position = index end end def previous_file(value) fls = ordered_files pos = fls.index(value) - 1 return if pos < 0 fls[pos] end end end end end end end require 'ecoportal/api/v2/page/component/file'
Version data entries
26 entries across 26 versions & 1 rubygems