module Ecoportal module API class V2 class Page < Common::Content::DoubleModel ALLOWED_KEYS = [ "id", "patch_ver", "name", "template_id", "base_tags", "tags", "time_zone", "created_at", "updated_at", "components", "sections", "stages", "permits", "mould_counter", "mould", "state", "task_priority", "votes_enabled", "upvotes", "downvotes", "forces", "force_errors", "subtags" ] passkey :id passforced :patch_ver, default: 1 passthrough :name, :template_id passarray :base_tags, :tags, order_matters: false passthrough :time_zone passdate :created_at, :updated_at, read_only: true passthrough :can class_resolver :components_class, "Ecoportal::API::V2::Page::Components" class_resolver :sections_class, "Ecoportal::API::V2::Page::Sections" class_resolver :stages_class, "Ecoportal::API::V2::Page::Stages" class_resolver :forces_class, "Ecoportal::API::V2::Page::Forces" embeds_many :components, enum_class: :components_class embeds_many :sections, enum_class: :sections_class embeds_many :stages, enum_class: :stages_class embeds_many :forces, enum_class: :forces_class def initialize(doc = [], parent: self, key: nil) super(_doc_bug_fix(doc), parent: parent, key: key) end def ooze self end def as_update super.tap do |hash| unless !hash hash["data"].select! do |key, value| ALLOWED_KEYS.include?(key) end return nil if (hash["data"].keys - ["patch_ver"]).empty? end end end def stages? self.doc["stages"] && (self.stages.count > 0) end # @return [String] with feedback, if for this page instance, there are any of: # 1. components multi-section (fields belonging to more than one section) def validate msg = "" if (multi = components.multi_section).length > 0 msg += "There are fields attached to more than one section:\n • " msg += multi.map do |fld| fld.label end.join("\n • ") + "\n" end msg.empty?? true : msg end private def _doc_bug_fix(hash) hash.tap do |hash| _fix_doc(hash["stages"], "flow_node_ids", "section_ids") if hash.key?("stages") if hash.key?("sections") _fix_doc(hash["sections"], "membrane_ids", "component_ids") _fix_doc(hash["sections"], "left_membrane_ids", "left_component_ids") _fix_doc(hash["sections"], "right_membrane_ids", "right_component_ids") end end end def _fix_doc(value, source, dest) value.tap do |value| case value when Array value.each {|v| _fix_doc(v, source, dest)} when Hash if value.key?(source) && !value.key?(dest) value[dest] = value[source] value.delete(source) end else # Do nothing! end end end end end end end require 'ecoportal/api/v2/page/mould_counter' require 'ecoportal/api/v2/page/permission_flags' require 'ecoportal/api/v2/page/permit' require 'ecoportal/api/v2/page/component' require 'ecoportal/api/v2/page/components' require 'ecoportal/api/v2/page/section' require 'ecoportal/api/v2/page/sections' require 'ecoportal/api/v2/page/stage' require 'ecoportal/api/v2/page/stages' require 'ecoportal/api/v2/page/force' require 'ecoportal/api/v2/page/forces'