From 7ce047e5bb9b652ddf1ccde2760c1df274a6c158 Mon Sep 17 00:00:00 2001 From: bivanalhar Date: Tue, 28 Feb 2023 15:56:49 +0800 Subject: [PATCH] feat(duplication_tracing): add functionality to trace duplication for lesson plan items --- app/models/course/lesson_plan/item.rb | 2 +- lib/autoload/duplicator.rb | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/models/course/lesson_plan/item.rb b/app/models/course/lesson_plan/item.rb index 1ecd45099b7..0b70b22e91f 100644 --- a/app/models/course/lesson_plan/item.rb +++ b/app/models/course/lesson_plan/item.rb @@ -109,7 +109,7 @@ class Course::LessonPlan::Item < ApplicationRecord belongs_to :course, inverse_of: :lesson_plan_items has_many :todos, class_name: Course::LessonPlan::Todo.name, inverse_of: :item, dependent: :destroy has_one :duplication_traceable, class_name: DuplicationTraceable::LessonPlanItem.name, - inverse_of: :lesson_plan_item, dependent: :destroy + inverse_of: :lesson_plan_item, dependent: :destroy, foreign_key: :lesson_plan_item_id delegate :start_at, :start_at=, :start_at_changed?, :bonus_end_at, :bonus_end_at=, :bonus_end_at_changed?, :end_at, :end_at=, :end_at_changed?, diff --git a/lib/autoload/duplicator.rb b/lib/autoload/duplicator.rb index 469dce20bd6..c614b3b9f15 100644 --- a/lib/autoload/duplicator.rb +++ b/lib/autoload/duplicator.rb @@ -94,13 +94,21 @@ def duplicate_object(source_object) # rubocop:disable Metrics/AbcSize @duplicated_objects[key] = duplicate duplicate.initialize_duplicate(self, key) - # Set duplication source, if it's being tracked for this class. + # Set duplication source, if it's being tracked for this class or has its actable class being tracked if duplicate.class.method_defined?(:duplication_traceable) traceable = duplicate.class.reflect_on_association(:duplication_traceable).options[:class_name].constantize duplicate.duplication_traceable = traceable.initialize_with_dest(duplicate, source_id: source_object.id, creator: @options[:current_user], updater: @options[:current_user]) + elsif duplicate.respond_to?(:acting_as) && duplicate.acting_as.class.method_defined?(:duplication_traceable) + dup_parent = duplicate.acting_as + traceable = dup_parent.class.reflect_on_association(:duplication_traceable).options[:class_name].constantize + dup_parent.duplication_traceable = traceable.initialize_with_dest(dup_parent, + source_id: source_object.acting_as.id, + creator: @options[:current_user], + updater: @options[:current_user]) + byebug end end end