From 121002ef9e26f10dee2b8a1ba32f614821c11d3e Mon Sep 17 00:00:00 2001 From: "ajit.shetty" Date: Fri, 26 Aug 2022 13:08:49 +0530 Subject: [PATCH] [Update] added the radio button fix --- .../data/exporters/record_data_exporter.rb | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/migration/v1_to_v2/data/exporters/record_data_exporter.rb b/migration/v1_to_v2/data/exporters/record_data_exporter.rb index 793c3d6..c818344 100644 --- a/migration/v1_to_v2/data/exporters/record_data_exporter.rb +++ b/migration/v1_to_v2/data/exporters/record_data_exporter.rb @@ -6,6 +6,11 @@ class RecordDataExporter < DataExporter private + def initialize(options = {}) + super(options) + @radio_fields = get_radio_fields + end + def parse_object(object) super(object).merge(ownership_fields(object)) end @@ -17,6 +22,46 @@ def migrate_notes(notes) end end + def get_radio_fields + primero_module = PrimeroModule.find_by_name("CP") #hardcoded for now. need to change it to accomodate either one. Cannot use all since there comflicting fields in both modules + formsections = primero_module.associated_forms(true) + radio_fields = [] + formsections.each do |form| + radio_fields << form.fields.map{ |s| s.name if s.visible == true && s.type == "radio_button" && s.option_strings_source == "lookup lookup-yes-no" }.compact + end + return radio_fields.flatten.compact + end + + def value_to_ruby_string(value, include_blank = false) + return 'nil' if include_blank && value.nil? + + puts value + + if value.is_a?(Hash) + ruby_string = "{\n" + _i + ruby_string += i + # TODO: was using .compact instead of .reject but it was throwing away false values. We want to keep those + ruby_string += (include_blank ? value : value.reject { |_, v| v.nil? || v == [] }).map do |k,v| + if @radio_fields.include?(k) + "#{key_to_ruby(k)}: \"#{v}\"" + else + "#{key_to_ruby(k)}: #{value_to_ruby_string(v, include_blank)}" + end + end.join(",\n#{i}") + i_ + ruby_string + "\n#{i}}" + elsif value.is_a?(Array) + array_value_to_ruby_string(value, include_blank) + elsif value.is_a?(Range) + value + elsif value.is_a?(String) && (value.include?('.parse(') || value.include?('.find_by(')) + value + else + value.to_json + end + end + def data_hash_case(data_hash) keys = data_hash.keys data_hash['notes_section'] = migrate_notes(data_hash['notes_section']) if data_hash['notes_section'].present?