module Ecoportal module API class V2 class Page class Component class ActionField < Page::Component passthrough :create_actions passthrough :required_number_of_completed_actions passthrough :permits_and_rules_integration, :add_subscribed, :add_subscribed_to_tasks embeds_many :actions, klass: "Ecoportal::API::V2::Page::Component::Action", order_key: :weight # Adds a task with `name` short description # @return [Ecoportal::API::V2::Page::Component::Action] def add_task (name) task_doc = actions.items_class.new_doc actions.upsert!(task_doc) do |task| task.name = name if prev = previous_task(task) task.weight = prev.weight end yield(task) if block_given? fix_task_weights! end end def ordered_tasks actions.each_with_index.sort_by do |task, index| (task.weight >= 9999) ? [index, index] : [task.weight, index] end.map(&:first) end private def fix_task_weights! ordered_tasks.each_with_index do |task, index| task.weight = index end end def previous_task(value) tasks = ordered_tasks pos = tasks.index(value) - 1 return if pos < 0 tasks[pos] end end end end end end end require 'ecoportal/api/v2/page/component/action'