From 07909fb334cb271a0f60d506862a41887e780e75 Mon Sep 17 00:00:00 2001 From: d-m-u Date: Thu, 12 Jul 2018 12:59:49 -0400 Subject: [PATCH] Start of adding rake task for cb exports --- lib/task_helpers/exports/custom_buttons.rb | 57 ++++++++++++++++++++++ lib/tasks/evm_export_import.rake | 16 ++++++ 2 files changed, 73 insertions(+) create mode 100644 lib/task_helpers/exports/custom_buttons.rb diff --git a/lib/task_helpers/exports/custom_buttons.rb b/lib/task_helpers/exports/custom_buttons.rb new file mode 100644 index 000000000000..256e3659255d --- /dev/null +++ b/lib/task_helpers/exports/custom_buttons.rb @@ -0,0 +1,57 @@ +module TaskHelpers + class Exports + class CustomButtons + class ExportArInstances + EXCLUDE_ATTRS = %w(id created_on updated_on created_at updated_at dialog_id resource_id).freeze + def self.export_object(obj, hash) + class_name = obj.class.name.underscore + + $log.send(:info, "Exporting #{obj.class.name} [#{obj.try('name')}] with ID: #{obj.id}") if obj.try(:id).present? + (hash[class_name] ||= []) << item = { 'attributes' => build_attr_list(obj.try(:attributes)) } + create_association_list(obj, item) + descendant_list(obj, item) + end + + def self.build_attr_list(attrs) + attrs&.except(*EXCLUDE_ATTRS) + end + + def self.create_association_list(obj, item) + associations = obj.class.try(:reflections) + if associations.present? + associations = associations.collect { |r| { r[0] => r[1].class.to_s.demodulize } }.select { |d| d.values.first != "BelongsToReflection" && d.keys.first != "all_relationships" } + associations.each do |assoc| + #next if assoc.blank? + assoc.each do |a| + next if obj.try(a.first.to_sym).blank? + export_object(obj.try(a.first.to_sym), (item['associations'] ||= {})) + end + end + end + end + + def self.descendant_list(obj, item) + obj.try(:children).each { |c| export_object(c, (item['children'] ||= {})) } if obj.try(:children).present? + end + end + + def export(options = {}) + parent_id_list = [] + objects = CustomButton.where.not(:applies_to_class => %w(ServiceTemplate GenericObject)) + + export = objects.each_with_object({}) do |obj, export_hash| + if obj.try(:parent).present? + next if parent_id_list.include?(obj.parent.id) + ExportArInstances.export_object(obj.parent, export_hash) + parent_id_list << obj.parent.id + else + ExportArInstances.export_object(obj, export_hash) + end + end + + export_dir = options[:directory] + File.write("#{export_dir}/CustomButtons.yaml", YAML.dump(export)) + end + end + end +end diff --git a/lib/tasks/evm_export_import.rake b/lib/tasks/evm_export_import.rake index 3c55988b13db..12c7d93b5842 100644 --- a/lib/tasks/evm_export_import.rake +++ b/lib/tasks/evm_export_import.rake @@ -68,6 +68,14 @@ namespace :evm do exit # exit so that parameters to the first rake task are not run as rake tasks end + + desc 'Exports all custom buttons to a single YAML file' + task :custom_buttons => :environment do + options = TaskHelpers::Exports.parse_options + TaskHelpers::Exports::CustomButtons.new.export(options) + + exit # exit so that parameters to the first rake task are not run as rake tasks + end end namespace :import do @@ -132,5 +140,13 @@ namespace :evm do exit # exit so that parameters to the first rake task are not run as rake tasks end + + desc 'Imports all custom buttons from (individual) YAML file(s)' + task :custom_buttons => :environment do + options = TaskHelpers::Imports.parse_options + TaskHelpers::Imports::CustomButtons.new.import(options) + + exit # exit so that parameters to the first rake task are not run as rake tasks + end end end