Module: Eco::API::UseCases::OozeSamples::HelpersMigration::Copying

Included in:
Eco::API::UseCases::OozeSamples::HelpersMigration
Defined in:
lib/eco/api/usecases/ooze_samples/helpers_migration/copying.rb

Instance Method Summary collapse

Instance Method Details

#copy_field_content(src, dst, type = src.type) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/eco/api/usecases/ooze_samples/helpers_migration/copying.rb', line 73

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

#copy_generic_paired_fields(src, dst, exclude_ximport: false) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/eco/api/usecases/ooze_samples/helpers_migration/copying.rb', line 12

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

#copy_hooked_fields(src, dst, hook:, type:, fields_tracker: nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/eco/api/usecases/ooze_samples/helpers_migration/copying.rb', line 21

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

#loggerObject



8
9
10
# File 'lib/eco/api/usecases/ooze_samples/helpers_migration/copying.rb', line 8

def logger
  session.logger
end

#sessionObject



4
5
6
# File 'lib/eco/api/usecases/ooze_samples/helpers_migration/copying.rb', line 4

def session
  defined?(super)? super : @session
end

#to_regex(str) ⇒ Object



43
44
45
# File 'lib/eco/api/usecases/ooze_samples/helpers_migration/copying.rb', line 43

def to_regex(str)
  /^#{str}$/i
end

#with_src_dest_field_pair(src, dst, label: nil, label_dst: label, type: nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/eco/api/usecases/ooze_samples/helpers_migration/copying.rb', line 47

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