module Ecoportal module API class V2 class Page class Components < Common::Content::CollectionModel class_resolver :component_class, "Ecoportal::API::V2::Page::Component" self.klass do |doc| component_class.get_class(doc).tap do |klass| klass.key = :id end end order_matters = true def ooze self._parent.ooze end # @return [Ecoportal::API::V2::Page::Component] the field with `id` def get_by_id(id) self.find do |comp| comp.id == id end end # @return [Array] the fields of that `type` def get_by_type(type) self.select do |comp| comp.type.downcase == type.to_s.strip.downcase end end # @param name [String, Regexp] the `name` to search the field based on their `label` # @param type [String] the `type` of the fields to be included # @return [Array] the fields that match `name`. def get_by_name(name, type: nil) pool = type ? get_by_type(type) : self pool.select do |comp| same_string?(comp.label, name) end end # It creates a **new** component # @note # - You can use either `type` and `label` **or** `doc` # @param label [String, nil] # @param type [String] the type of the field # @param doc [Hash] to copy another field model # @yield [field] do some stuff with field # @yieldparam [Ecoportal::API::V2::Page::Component] the created field # @return [Ecoportal::API::V2::Page::Component] the created field. def add(doc: nil, label: doc && doc["label"], type: doc && doc["type"]) fld_doc = doc ? JSON.parse(doc.to_json) : component_class.new_doc(type: type) upsert!(fld_doc) do |fld| fld.label = label if !doc yield(fld) if block_given? end end # @return [Array] **orphaned** fields (with no section). def unattached select {|comp| !comp.attached?} end # @return [Array] fields belonging to more than one section. def multi_section select {|comp| comp.multi_section?} end end end end end end