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) ⇒ Object



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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/eco/api/usecases/ooze_samples/helpers_migration/copying.rb', line 98

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, src_excluded: [], dst_excluded: [], 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, 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

#copy_hooked_fields(src, dst, hook:, type:, fields_tracker: nil, src_excluded: [], dst_excluded: []) ⇒ 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, 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

#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_dst_field_pair(src, dst, label: nil, label_dst: label, type: nil, src_excluded: [], dst_excluded: []) ⇒ Object

Note:
  • When used with type: nil it may pair fields of different type. There isn't refinement for this yet.


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/eco/api/usecases/ooze_samples/helpers_migration/copying.rb', line 49

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