Skip to content

Commit 62432b9

Browse files
Merge pull request #2944 from DMPRoadmap/bug_template_customization
bug fix: customization upgrade #2898
2 parents 30f4590 + 08eb0bb commit 62432b9

File tree

1 file changed

+36
-53
lines changed

1 file changed

+36
-53
lines changed

app/services/template/upgrade_customization_service.rb

Lines changed: 36 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
class Template
44

55
# Service object to upgrade a customization Template with new changes from the original
6-
# funder Template. Remember: {target_template} is a customization of funder Template.
6+
# funder Template. Remember: {updated_template} is a customization of funder Template.
77
#
8-
# - Duplicate the init template (Duplication called {#customized_template})
8+
# - Duplicate the init template (Duplication called {#@copy_of_original_customizations})
99
#
1010
# - Create a new customisation of funder template (Customization called
11-
# {#target_template})
11+
# {#updated_template})
1212
#
13-
# - Take each phase on the {#target_template} and iterate to find if there's a
14-
# corresponding one in {#customized_template}
13+
# - Take each phase on the {#updated_template} and iterate to find if there's a
14+
# corresponding one in {#@copy_of_original_customizations}
1515
# - Test for each of corresponding phase in source
1616
# - Copy over each of the modifiable sections from source to the target
1717
# - Re-number the sections if necessary to keep the display order (number) the same
1818
# - Copy each of the questions and annotations exactly
1919
# - For each unmodifiable section, copy over any modifiable questions from target
2020
#
21-
# - Copy each of the modifiable sections from the {#customized_template} to the
22-
# {#target_template}
21+
# - Copy each of the modifiable sections from the {#@copy_of_original_customizations} to the
22+
# {#updated_template}
2323
#
2424
class UpgradeCustomizationService
2525

@@ -31,12 +31,6 @@ class NotACustomizationError < StandardError
3131
class NoFunderTemplateError < StandardError
3232
end
3333

34-
##
35-
# The Template we're upgrading
36-
#
37-
# Returns {Template}
38-
attr_reader :init_template
39-
4034
# Initialize a new instance and run the script
4135
#
4236
# template - The Template we're upgrading
@@ -50,83 +44,72 @@ def self.call(template)
5044

5145
# Initialize a new record
5246
#
53-
# template - The Template we're upgrading. Sets the value for {#init_template}
47+
# template - The Template we're upgrading.
5448
#
5549
def initialize(template)
56-
@init_template = template
50+
@source_template = template
51+
52+
@original_funder_template = Template.published(@source_template.customization_of).first
53+
54+
@copy_of_original_customizations = @source_template.deep_copy(
55+
attributes: { version: @source_template.version + 1, published: false }
56+
)
57+
58+
@updated_template = init_updated_template
5759
end
5860

5961
# Run the script
6062
#
6163
# Returns {Template}
6264
def call
6365
Template.transaction do
64-
if init_template.customization_of.blank?
66+
if @source_template.customization_of.blank?
6567
raise NotACustomizationError,
6668
_("upgrade_customization! requires a customised template")
6769
end
68-
if funder_template.nil?
70+
if @original_funder_template.nil?
6971
# rubocop:disable Layout/LineLength
7072
raise NoFunderTemplateError,
7173
_("upgrade cannot be carried out since there is no published template of its current funder")
7274
# rubocop:enable Layout/LineLength
7375
end
7476

75-
# Merges modifiable sections or questions from source into target_template object
76-
target_template.phases.map do |funder_phase|
77+
# Merges modifiable sections or questions from source into updated_template object
78+
@updated_template.phases.map do |funder_phase|
7779
# Search for the phase in the source template whose versionable_id matches the
7880
# customization_phase
7981
customized_phase = find_matching_record_in_collection(
8082
record: funder_phase,
81-
collection: customized_template.phases
83+
collection: @copy_of_original_customizations.phases
8284
)
83-
# a) If the Org's template ({#customized_template}) has the Phase...
85+
# a) If the Org's template ({@copy_of_original_customizations}) has the Phase...
8486
next unless customized_phase.present?
8587

86-
# b) If the Org's template ({#customized_template}) doesn't have this Phase.
87-
# This is not a problem, since {#customization_template} should have this
88+
# b) If the Org's template ({@copy_of_original_customizations}) doesn't have this Phase.
89+
# This is not a problem, since {@copy_of_original_customizations} should have this
8890
# Phase copied over from {#template_phase}.
8991
copy_modifiable_sections_for_phase(customized_phase, funder_phase)
9092
sort_sections_within_phase(funder_phase)
9193
end
9294
copy_custom_annotations_for_questions
9395
end
94-
target_template
96+
@updated_template
9597
end
9698

9799
private
98100

99-
# The funder Template for this {#template}
100-
#
101-
# Returns Template
102-
def funder_template
103-
@funder_template ||= Template.published(init_template.customization_of).first
104-
end
105-
106-
# A copy of the Template we're currently upgrading. Preserves modifiable flags from
107-
# the self template copied
108-
#
109-
#
110-
# Returns {Template}
111-
def customized_template
112-
@customized_template ||= init_template.deep_copy(attributes: {
113-
version: init_template.version + 1,
114-
published: false
115-
})
116-
end
117-
118101
# Creates a new customisation for the published template whose family_id {#template}
119102
# is a customization of
120103
#
121104
# Returns {Template}
122-
def target_template
123-
@target_template ||= funder_template.deep_copy(
105+
def init_updated_template
106+
@updated_template = @original_funder_template&.deep_copy(
124107
attributes: {
125-
version: customized_template.version,
126-
published: customized_template.published,
127-
family_id: customized_template.family_id,
128-
customization_of: customized_template.customization_of,
129-
org: customized_template.org,
108+
version: @copy_of_original_customizations.version,
109+
published: @copy_of_original_customizations.published,
110+
family_id: @copy_of_original_customizations.family_id,
111+
customization_of: @copy_of_original_customizations.customization_of,
112+
org: @copy_of_original_customizations.org,
130113
visibility: Template.visibilities[:organisationally_visible],
131114
is_default: false
132115
}, modifiable: false, save: true
@@ -162,8 +145,8 @@ def copy_modifiable_sections_for_phase(source_phase, target_phase)
162145
end
163146

164147
def copy_custom_annotations_for_questions
165-
init_template.annotations.where(org: template_org).each do |custom_annotation|
166-
target_question = target_template.questions.find_by(
148+
@source_template.annotations.where(org: template_org).each do |custom_annotation|
149+
target_question = @updated_template.questions.find_by(
167150
versionable_id: custom_annotation.question.versionable_id
168151
)
169152
target_question.annotations << custom_annotation if target_question.present?
@@ -175,7 +158,7 @@ def sort_sections_within_phase(phase)
175158
end
176159

177160
def template_org
178-
init_template.org
161+
@source_template.org
179162
end
180163

181164
end

0 commit comments

Comments
 (0)