# frozen_string_literal: true module Concerns # Copies lesson content from one arm's learning tool # (i.e., LEARN tool) to another arm's learning tool module Copier LESSON_MODULE = ContentModules::LessonModule.to_s LEARN_TOOL = Tools::Learn.to_s TOOL_TITLE = "LEARN" Arm.class_eval do def copy_all_to(new_arm) new_tools = new_arm.bit_core_tools bit_core_tools.each do |tool| new_tool = new_tools .create!( position: new_tools.count, title: tool.title, type: tool.type) tool.copy_all_to( new_tool: new_tool) end end def copy_lessons_to(new_arm) new_tools = new_arm.bit_core_tools new_tool = new_tools.find_by(type: LEARN_TOOL) || new_tools.create!( position: new_tools.count, title: TOOL_TITLE, type: LEARN_TOOL) return unless new_tool bit_core_tools .find_by(type: LEARN_TOOL) .copy_content_modules_to( new_tool: new_tool) end end # Extend BitCore classes # rubocop:disable ClassAndModuleChildren module ::BitCore Tool.class_eval do def copy_all_to(new_tool:) ContentModule.transaction do content_modules.each do |content_module| content_module.copy_content_providers_to( new_tool: new_tool) end end end def copy_content_modules_to(new_tool:) ContentModule.transaction do content_modules.where(type: LESSON_MODULE) .find_each do |content_module| content_module.copy_content_providers_to( new_tool: new_tool) end end end end ContentModule.class_eval do def copy_content_providers_to(new_tool:) ContentModule.transaction do new_content_module = ContentModule.create!( position: position, title: title, tool: new_tool, type: type) new_content_module.content_providers.destroy_all content_providers.each do |content_provider| content_provider .copy_source_content_to!( new_arm: new_tool.arm, new_content_module: new_content_module) end new_content_module end end end ContentProvider.class_eval do def copy_source_content_to!(new_arm:, new_content_module:) ContentProvider.transaction do ContentProvider.create!( content_module: new_content_module, position: position, show_next_nav: show_next_nav, type: type, source_content: source_content.try(:copy_to, new_arm)) end end end Slideshow.class_eval do def copy_to(new_arm) Slideshow.transaction do slideshow = Slideshow .find_or_create_by!(arm_id: new_arm.id, title: title) copy_slides_to(slideshow) end end private def copy_slides_to(slideshow) Slide.transaction do slides.each do |slide| slide.copy_to!(slideshow) end end slideshow end end Slide.class_eval do def copy_to!(slideshow) slideshow.slides .find_or_create_by!( body: body, is_title_visible: is_title_visible, position: position, title: title, type: type) .update_attributes!(options: options) end end end # rubocop:enable ClassAndModuleChildren end end