lib/ecoportal/api/v2/page/components.rb in ecoportal-api-v2-0.8.26 vs lib/ecoportal/api/v2/page/components.rb in ecoportal-api-v2-0.8.27

- old
+ new

@@ -40,18 +40,21 @@ 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(label:, type:) - fld_doc = component_class.new_doc(type: type) + 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 + fld.label = label if !doc yield(fld) if block_given? end end # @return [Array<Ecoportal::API::V2::Page::Component>] **orphaned** fields (with no section).