# 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 LEARN_TOOL = "Tools::Learn" TOOL_TITLE = "LEARN" Arm.class_eval do 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_arm: new_arm, new_tool: new_tool) end end # Extend BitCore classes # rubocop:disable ClassAndModuleChildren module ::BitCore Tool.class_eval do LESSON_MODULE = "ContentModules::LessonModule" def copy_content_modules_to(new_arm:, new_tool:) ContentModule.transaction do content_modules.where(type: LESSON_MODULE).each do |content_module| content_module.copy_content_providers_to( new_arm: new_arm, new_tool: new_tool) end end end end ContentModule.class_eval do def copy_content_providers_to(new_arm:, 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_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 # rubocop:disable DoubleNegation 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)) # rubocop:enable DoubleNegation end end end Slideshow.class_eval do def copy_to(new_arm) Slideshow.transaction do slideshow = Slideshow.create!(arm: new_arm, title: title) copy_slides_to!(slideshow) slideshow end end private def copy_slides_to!(slideshow) Slide.transaction do slides.each do |slide| slide.copy_to!(slideshow) end end end end Slide.class_eval do def copy_to!(slideshow) Slide.create!( body: body, is_title_visible: is_title_visible, options: options, position: position, slideshow: slideshow, title: title, type: type) end end end # rubocop:enable ClassAndModuleChildren end end