Sha256: 25e0e94446e991c9a4745259679871b27558e43ba044e921cd44bb62ed886c5c
Contents?: true
Size: 1.51 KB
Versions: 16
Compression:
Stored size: 1.51 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 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
16 entries across 16 versions & 1 rubygems