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, exclude_ximport: false) HelpersMigration::TypedFieldsPairing.new(src, dst, 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) case hook when String with_src_dest_field_pair(src, dst, label: to_regex(hook), type: type) do |src_fld, dst_fld| copy_field_content(src_fld, dst_fld, type).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_dest_field_pair(src, dst, label: src_lab, label_dst: dst_lab, type: type) do |src_fld, dst_fld| copy_field_content(src_fld, dst_fld, type).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 def with_src_dest_field_pair(src, dst, label: nil, label_dst: label, type: nil) 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) if dst_flds.count > 1 logger.warn("There are #{dst_flds.count} destination '#{type}' fields named '#{label_dst}' (source: '#{src.id}'). Using first...") elsif dst_flds.empty? logger.warn("There isn't a destination '#{type}' field named '#{label_dst}' (source: '#{src.id}')") return nil, nil end if src_flds.count > 1 logger.warn("There are #{src_flds.count} source '#{type}' fields named '#{label}' (source: '#{src.id}'). Using first...") elsif src_flds.empty? logger.warn("There isn't a source '#{type}' field named '#{label}' (source: '#{src.id}')") return nil, dst end [src_flds.first, dst_flds.first].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