module Ecoportal module API class V2 class Page class Component class ReferenceField < Page::Component passthrough :register_id passarray :references passboolean :hide_create, :hide_attach passboolean :hide_metadata, :hide_dashboards passboolean :display_fields, :display_fields_in_lookup def empty? reference_ids.empty? end def reference_ids references.map do |ref| ref["id"] end end def add(*ref_ids) doc["references"].tap do |refs| ref_ids.each do |ref_id| next if reference_ids.include?(ref_id) refs.push({ "id" => ref_id, "weight" => 0, "patch_ver" => 0 }) end end end def clear delete(*reference_ids) end def delete(*ref_ids) ref_ids.each do |ref_id| doc_ref = doc["references"].find do |df| df["id"] == ref_id end next unless doc_ref doc["references"].delete(doc_ref) end end def to_s(delimiter: "\n") reference_ids.to_a.join(delimiter) end # Quick config helper # @param conf [Symbol, Array] # - `:show_fields` specify if the public register fields should be shown (requires `register_id`) # - `:create` specify if the `NEW` button should appear # - `:attach` specify if the `ATTACH` button should appear # - `:metadata` specify if `metadata` should be shown (i.e. status) def configure(*conf) conf.each_with_object([]) do |cnf, unused| case cnf when :show_fields self.display_fields = true self.display_fields_in_lookup = true when Hash supported = %i[create attach metadata] rest = hash_except(cnf.dup, *supported) unused.push(rest) unless rest.empty? self.hide_create = !cnf[:create] if cnf.key?(:create) self.hide_attach = !cnf[:attach] if cnf.key?(:attach) self.hide_metadata = !cnf[:metadata] if cnf.key?(:metadata) else unused.push(cnf) end end.then do |unused| super(*unused) end end end end end end end end