class Eco::API::UseCases::OozeSamples module HelpersMigration module Copying def session defined?(super)? super : @session end def logger session.logger end def copy_generic_paired_fields(src, dst, src_excluded: [], dst_excluded: [], exclude_ximport: false) HelpersMigration::TypedFieldsPairing.new(src, dst, src_excluded: src_excluded, dst_excluded: dst_excluded, exclude_ximport: false).tap do |pairing| pairing.each_pair do |src_fld, dst_fld| copy_field_content(src_fld, dst_fld) yield(src_fld, dst_fld, pairing) if block_given? end end end def copy_hooked_fields(src, dst, hook:, type:, fields_tracker: nil, src_excluded: [], dst_excluded: []) case hook when String with_src_dst_field_pair(src, dst, label: to_regex(hook), type: type, src_excluded: src_excluded, dst_excluded: dst_excluded) do |src_fld, dst_fld| copy_field_content(src_fld, dst_fld).tap do |result| fields_tracker.resolve(src_fld, dst_fld) if fields_tracker yield(src_fld, dst_fld, fields_tracker) if block_given? end end when Hash hook.each do |hook_src, hook_dst| src_lab, dst_lab = to_regex(hook_src), to_regex(hook_dst) with_src_dst_field_pair(src, dst, label: src_lab, label_dst: dst_lab, type: type, src_excluded: src_excluded, dst_excluded: dst_excluded) do |src_fld, dst_fld| copy_field_content(src_fld, dst_fld).tap do |result| fields_tracker.resolve(src_fld, dst_fld) if fields_tracker yield(src_fld, dst_fld, fields_tracker) if block_given? end end end end end def to_regex(str) /^#{str}$/i end # @note # - When used with `type: nil` it may pair fields of different type. There isn't refinement for this yet. def with_src_dst_field_pair(src, dst, label: nil, label_dst: label, type: nil, src_excluded: [], dst_excluded: []) unless respond_to?(:with_fields, true) raise "These helpers are to be included in class cases inheriting from Eco::API::UseCases::OozeSamples::RegisterUpdateCase" end src_flds = with_fields(src, label: label, type: type) dst_flds = with_fields(dst, label: label_dst, type: type) src_fld, dst_fld = src_flds.first, dst_flds.first # Not found if dst_flds.empty? || src_flds.empty? msg = "There isn't a '#{type}'" dst_msg = dst_flds.empty?? "destination field named '#{label_dst}'" : nil src_msg = src_flds.empty?? "source field named '#{label}'" : nil msg << " #{[dst_msg, src_msg].join(" or ")} (source: '#{src.id}')" logger.warn(msg) return nil, nil end # Fields exclusion dst_flds -= dst_excluded unless dst_excluded.empty? src_flds -= src_excluded unless src_excluded.empty? src_fld, dst_fld = src_flds.first, dst_flds.first return src_fld, dst_fld if dst_flds.empty? || src_flds.empty? if dst_flds.count > 1 logger.warn("There are #{dst_flds.count} destination '#{type}' fields named '#{label_dst}' (source: '#{dst.id}'). Using first...") end if src_flds.count > 1 msg = "There are #{src_flds.count} source '#{type}' fields named '#{label}' (source: '#{src.id}'). " src_content = src_flds.reject(&:empty?) if src_content.empty? msg << "None with content. Using first..." else src_fld = src_content.first if src_content.count == 1 msg << "Using the only one with content (idx: #{src_flds.index(src_fld)})..." else msg << "#{src_content.count} have content. Using first..." end end logger.warn(msg) end [src_fld, dst_fld].tap do |(src_fld, dst_fld)| yield(src_fld, dst_fld) if block_given? end end def copy_field_content(src, dst) type = src.type case type when "select" src.values.each {|val| dst.select(val)} dst.other_desc = src.other_desc if src.other && src.other_desc && dst.other when "plain_text", "date", "number", "gauge" dst.value = src.value when "rich_text" dst.content = src.content when "cross_reference" src.reference_ids.each {|id| dst.add(id)} when "people" dst.people_ids << src.people_ids.to_a when "geo" src_coo, dst_coo = src.coordinates, dst.coordinates dst_coo.lat, dst_coo.lon = src_coo.lat, src_coo.lon when "file" src.items.each do |src_item| dst.add_file(src_item.file_container_id) do |dst_item| dst_item.label = src_item.label end end when "image_gallery" src.images.each do |src_image| dst.add_image(src_image.upload_id) do |dst_image| dst_image.caption = src_image.caption end end when "checklist" src.items.each do |src_item| dst.add_item(label: src_item.label) do |dst_item| dst_item.checked = src_item.checked end end when "law" "won't copy" when "page_action" "won't copy" when "actions_list" "won't copy" when "signature" "can't copy" when "tag_field" "can't copy" when "chart" "won't copy" when "frequency_rate_chart" "won't copy" end end end end end