module Ecoportal module API class V2 class Page class Component class ActionField < Page::Component passboolean :create_actions passthrough :required_number_of_completed_actions passboolean :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.sort_by.with_index do |task, index| [task.weight, index] end end def to_s(delimiter: "\n") ordered_tasks.map(&:to_s).join(delimiter) end # Quick config helper # @param conf [Symbol, Array] # - `:requires` required number of completed actions # - `:all` # - `#Number` def configure(*conf) self.create_actions = true conf.each_with_object([]) do |cnf, unused| case cnf when Hash supported = [:requires] unless (rest = hash_except(cnf.dup, *supported)).empty? unused.push(rest) end if cnf.key?(:requires) then configure_required(cnf[:requires]) end else unused.push(cnf) end end.yield_self do |unused| super(*unused) end end private def configure_required(req) self.required = true case req when :all, NilClass self.required_number_of_completed_actions = "all" when Numeric self.required_number_of_completed_actions = req else # Unsupported end end 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'