From 0788cfde1e22242a032c47a6857cb9bc3bdc348f Mon Sep 17 00:00:00 2001 From: BoHung Chiu Date: Sat, 18 Mar 2023 14:34:08 +0800 Subject: [PATCH] Fix bug. --- app/models/seminar_main.rb | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/app/models/seminar_main.rb b/app/models/seminar_main.rb index 3332bd4..c3e4315 100644 --- a/app/models/seminar_main.rb +++ b/app/models/seminar_main.rb @@ -157,7 +157,7 @@ class SeminarMain self.unassigned_seminar_signup_ids = [] self.unassigned_mode_1_seminar_signup_ids = [] if self.copy_id.present? - clone_new(true) + self.clone_new(true) self.created_at = DateTime.now self.updated_at = DateTime.now end @@ -475,7 +475,14 @@ class SeminarMain nil end if !clone_target.nil? && !new_object.nil? - initialize_fields = ["uid","created_at","updated_at"] + if clone_mode + initialize_fields = [] + if new_object.fields.keys.include?("uid") + new_object.generate_uid + end + else + initialize_fields = ["uid","created_at","updated_at"] + end initialize_fields.each do |f| new_object.send("#{f}=",nil) if new_object.fields.keys.include?(f) end @@ -484,9 +491,8 @@ class SeminarMain all_fields = all_fields - relations_fields.map{|k| "#{k}_id"} all_fields = all_fields - relations_fields.map{|k| "#{k.singularize}_ids"} new_object_class_name = new_object.class.to_s.underscore - relations_fields = clone_target.relations.except("impressions").keys - unsort_relation_keys = clone_target.relations.keys unless @parent_level + unsort_relation_keys = clone_target.relations.keys fields_to_delete = [new_object_class_name] tmp_relations_fields = [new_object_class_name] while relations_fields.count > 0 @@ -529,10 +535,16 @@ class SeminarMain tmp_relations_fields << approve_append if approve_append.present? approve_append = nil relations_fields = relations_fields - tmp_relations_fields + relations_fields -= @relations_fields if @relations_fields end relations_fields = tmp_relations_fields fields_to_delete.each{|f| relations_fields.delete(f)} + end + if @parent_level + relations_fields -= @relations_fields + else @clone_mode = clone_mode + @relations_fields = relations_fields end @parent_level = true if clone_mode @@ -576,7 +588,7 @@ class SeminarMain next if self.except_clone_relations.to_s.include?(f) need_clone_relation = clone_target.send(f) clone_relation = new_object.send(f) - clone_relation = need_clone_relation.dup if clone_relation.nil? + clone_relation, need_clone_relation = clone_new_for_object(need_clone_relation.dup, need_clone_relation, true) if clone_relation.nil? initialize_fields.each do |f| clone_relation.send("#{f}=",nil) if clone_relation.fields.keys.include?(f) end @@ -630,11 +642,12 @@ class SeminarMain end if !no_dup_flag || (no_dup_flag && file_flag) new_object_relations = new_object.send(f).to_a - if new_object_relations.count != 0 - if clone_relations.count > new_object_relations.count - clone_relations = clone_relations[0...new_object_relations.count] + new_object_relations_count = new_object_relations.count + if new_object_relations_count != 0 + if clone_relations.count > new_object_relations_count + clone_relations = clone_relations[0...new_object_relations_count] else - clone_relations = clone_relations.concat(new_object.send(f)[clone_relations.count...new_object_relations.count]) + clone_relations = clone_relations.concat(new_object.send(f)[clone_relations.count...new_object_relations_count]) end new_object.send("#{f}=",clone_relations) else