Skip to content

Commit

Permalink
Start of rake task to import custom buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
d-m-u committed Jul 24, 2018
1 parent 5080b2f commit 37a5598
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
80 changes: 80 additions & 0 deletions lib/task_helpers/imports/custom_buttons.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
require "English"

module TaskHelpers
class Imports
class CustomButtons
class ImportArInstances
DEBUG_MODE = false

def self.import(obj_hash)
new.import(obj_hash)
end

def import(obj_hash)
ActiveRecord::Base.transaction { obj_hash.each { |obj_def| create_object(*obj_def) } }
end

def create_object(class_name, obj_array)
klass = class_name.camelize.constantize

obj_array.collect do |obj|
create_unique_values(obj) if DEBUG_MODE
begin
klass.create!(obj['attributes'].except('guid')).tap do |new_obj|
if obj['children'].present?
obj['children'].each do |child|
new_obj.add_members(create_object(*child))
end
end

if obj['associations'].present?
obj['associations'].each do |hoo|
new_obj.send("#{hoo.first}=", create_object(*hoo).first)
end
end
try("#{class_name}_post", new_obj)
end
rescue StandardError
$log.send(:info, "Failed to create new instance [#{class_name}] with attributes #{obj['attributes'].inspect}")
$log.send(:info, "#{$ERROR_INFO} at #{$ERROR_POSITION}")
raise
end
end
end

def custom_button_set_post(new_obj)
new_obj.set_data[:button_order] = new_obj.custom_buttons.collect(&:id)
new_obj.save!
end

def create_unique_values(obj)
%w(name description).each do |attr_name|
attr_value = obj.dig('attributes', attr_name)
obj.store_path('attributes', attr_name, "#{attr_value} #{Time.zone.now}") if attr_value.present?
end
end
end

def import(options)
return unless options[:source]

glob = File.file?(options[:source]) ? options[:source] : "#{options[:source]}/CustomButtons.yaml"
Dir.glob(glob) do |filename|
begin
import_custom_buttons(filename)
rescue
p "#{$ERROR_INFO} at #{$ERROR_POSITION}"
warn("Error importing #{options[:source]}")
end
end
end

private

def import_custom_buttons(filename)
custom_buttons = YAML.load_file(filename)
ImportArInstances.import(custom_buttons)
end
end
end
end
8 changes: 8 additions & 0 deletions lib/tasks/evm_export_import.rake
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,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 YAML file'
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

0 comments on commit 37a5598

Please sign in to comment.