class Ecoportal::API::V2::Page::Component::FilesField

Public Instance Methods

add_file(container_id, pos: NOT_USED, before: NOT_USED, after: NOT_USED) { |file| ... } click to toggle source
# File lib/ecoportal/api/v2/page/component/files_field.rb, line 9
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
ordered_files() click to toggle source
# File lib/ecoportal/api/v2/page/component/files_field.rb, line 21
def ordered_files
  items.sort_by.with_index do |file, index|
    [file.position, index]
  end
end

Private Instance Methods

fix_file_positions!() click to toggle source
# File lib/ecoportal/api/v2/page/component/files_field.rb, line 29
def fix_file_positions!
  ordered_files.each_with_index do |file, index|
    file.position = index
  end
end
previous_file(value) click to toggle source
# File lib/ecoportal/api/v2/page/component/files_field.rb, line 35
def previous_file(value)
  fls = ordered_files
  pos = fls.index(value) - 1
  return if pos < 0
  fls[pos]
end