module Ecoportal module API class V2 class Pages class PageStage < V2::Page passthrough :mould_counter, :archive passthrough :task_priority, :state, :status passthrough :votes_enabled, :upvotes, :downvotes embeds_many :permits, klass: "Ecoportal::API::V2::Page::Permit" passarray :force_errors, :subtags, order_matters: false # @return [String] `id` of the stage we got the data of. def current_stage_id doc.dig("active_stage", "id") || doc["current_stage_id"] end # @return [Ecoportal::API::V2::Page::Stage] def current_stage if stage_id = current_stage_id stages[stage_id] end end # @raise [Exception] if for this page instance, there are any of: # 1. orphaned components (fields not belonging to any section) # 2. components multi-section (fields belonging to more than one section) # 3. orphaned sections (sections not belonging to any stage) def as_update(*args, **kargs) validate.tap do |validation| raise validation if validation.is_a?(String) end super(*args, **kargs) end private def validate msg = "" if (orphans = components.unattached).length > 0 msg += "There are fields not attached to any sections:" msg += orphans.map do |fld| fld.label end.join("\n • ") + "\n" end if (multi = components.multi_section).length > 0 msg += "There are fields attached to more than one section:" msg += orphans.map do |fld| fld.label end.join("\n • ") + "\n" end if (orphans = sections.unattached).length > 0 msg += "There are sections not attached to any stage:" msg += orphans.map do |sec| "'#{sec.header}' (#{sec.id})" end.join("\n • ") + "\n" end msg.empty?? true : msg end end end end end end