-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathtemplate.rb
More file actions
528 lines (448 loc) · 17.3 KB
/
template.rb
File metadata and controls
528 lines (448 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# frozen_string_literal: true
# == Schema Information
#
# Table name: templates
#
# id :integer not null, primary key
# archived :boolean
# customization_of :integer
# description :text
# is_default :boolean
# links :text
# locale :string
# published :boolean
# title :string
# version :integer
# visibility :integer
# created_at :datetime
# updated_at :datetime
# family_id :integer
# org_id :integer
#
# Indexes
#
# index_templates_on_family_id (family_id)
# index_templates_on_family_id_and_version (family_id,version) UNIQUE
# index_templates_on_org_id (org_id)
# template_organisation_dmptemplate_index (org_id,family_id)
#
# Foreign Keys
#
# fk_rails_... (org_id => orgs.id)
#
# Object that represents a DMP template
# rubocop:disable Metrics/ClassLength
class Template < ApplicationRecord
include GlobalHelpers
extend UniqueRandom
validates_with TemplateLinksValidator
# A standard template should be organisationally visible. Funder templates
# that are meant for external use will be publicly visible. This allows a
# funder to create 'funder' as well as organisational templates. The default
# template should also always be publicly_visible.
enum visibility: %i[organisationally_visible publicly_visible]
# Stores links as an JSON object:
# {funder: [{"link":"www.example.com","text":"foo"}, ...],
# sample_plan: [{"link":"www.example.com","text":"foo"}, ...]}
#
# The links is validated against custom validator allocated at
# validators/template_links_validator.rb
attribute :links, :text, default: { funder: [], sample_plan: [] }
serialize :links, coder: JSON
attribute :published, :boolean, default: false
attribute :archived, :boolean, default: false
attribute :is_default, :boolean, default: false
attribute :version, :integer, default: 0
attribute :customization_of, :integer, default: nil
attribute :family_id, :integer, default: -> { Template.new_family_id }
attribute :visibility, default: Template.visibilities[:organisationally_visible]
# ================
# = Associations =
# ================
belongs_to :org
has_many :plans
has_many :phases, dependent: :destroy
has_many :sections, through: :phases
has_many :questions, through: :sections
has_many :annotations, through: :questions
has_many :question_options, through: :questions
has_many :conditions, through: :questions
# ===============
# = Validations =
# ===============
validates :title, presence: { message: PRESENCE_MESSAGE }
validates :org, presence: { message: PRESENCE_MESSAGE }
validates :locale, presence: { message: PRESENCE_MESSAGE }
validates :version, presence: { message: PRESENCE_MESSAGE },
uniqueness: { message: UNIQUENESS_MESSAGE,
scope: :family_id }
validates :visibility, presence: { message: PRESENCE_MESSAGE }
validates :family_id, presence: { message: PRESENCE_MESSAGE }
# =============
# = Callbacks =
# =============
# TODO: leaving this in for now, as this is better placed as an after_update than
# overwriting the accessors. We want to ensure this template is published
# before we remove the published_version
# That being said, there's a potential race_condition where we have multiple-published-versions
after_update :reconcile_published, if: -> { published? }
# ==========
# = Scopes =
# ==========
scope :archived, -> { where(archived: true) }
scope :unarchived, -> { where(archived: false) }
scope :published, lambda { |family_id = nil|
if family_id.present?
unarchived.where(published: true, family_id: family_id)
else
unarchived.where(published: true)
end
}
# Retrieves the latest templates, i.e. those with maximum version associated.
# It can be filtered down if family_id is passed
scope :latest_version, lambda { |family_id = nil|
unarchived.from(latest_version_per_family(family_id), :current)
.joins(<<~SQL)
INNER JOIN templates ON current.version = templates.version
AND current.family_id = templates.family_id
INNER JOIN orgs ON orgs.id = templates.org_id
SQL
}
# Retrieves the latest customized versions, i.e. those with maximum version
# associated for a set of family_id and an org
scope :latest_customized_version, lambda { |family_id = nil, org_id = nil|
unarchived
.from(latest_customized_version_per_customised_of(family_id, org_id),
:current)
.joins(<<~SQL)
INNER JOIN templates ON current.version = templates.version
AND current.customization_of = templates.customization_of
INNER JOIN orgs ON orgs.id = templates.org_id
SQL
.where(templates: { org_id: org_id })
}
# Retrieves the latest templates, i.e. those with maximum version associated
# for a set of org_id passed
scope :latest_version_per_org, lambda { |org_id = nil|
family_ids = if org_id.respond_to?(:each)
families(org_id).pluck(:family_id)
else
families([org_id]).pluck(:family_id)
end
latest_version(family_ids)
}
# Retrieve all of the latest customizations for the specified org
scope :latest_customized_version_per_org, lambda { |org_id = nil|
family_ids = families(org_id).pluck(:family_id)
latest_customized_version(family_ids, org_id)
}
# Retrieves templates with distinct family_id. It can be filtered down if
# org_id is passed
scope :families, lambda { |org_id = nil|
if org_id.respond_to?(:each)
unarchived.where(org_id: org_id, customization_of: nil).distinct
else
unarchived.where(customization_of: nil).distinct
end
}
# Retrieves the latest version of each customizable funder template (and the
# default template)
scope :latest_customizable, lambda {
funder_ids = Org.funder.pluck(:id)
family_ids = families(funder_ids).distinct
.pluck(:family_id) + [default.family_id]
published(family_ids.uniq)
.where('visibility = :visibility OR is_default = :is_default',
visibility: visibilities[:publicly_visible], is_default: true)
}
# Retrieves unarchived templates with public visibility
# Overwrites the default method from the enum
scope :publicly_visible, lambda {
unarchived.where(visibility: visibilities[:publicly_visible])
}
# Retrieves unarchived templates with organisational visibility
# Overwrites the default method from the enum
scope :organisationally_visible, lambda {
unarchived.where(visibility: visibilities[:organisationally_visible])
}
# Retrieves unarchived templates whose title or org.name includes the term
# passed(We use search_term_orgs as alias for orgs to avoid issues with
# any orgs table join already present in loaded unarchived.)
scope :search, lambda { |term|
unarchived
.joins(<<~SQL)
JOIN orgs AS search_term_orgs ON search_term_orgs.id = templates.org_id
SQL
.where('lower(templates.title) LIKE lower(:term)' \
'OR lower(search_term_orgs.name) LIKE lower(:term)',
term: "%#{term}%")
}
# defines the export setting for a template object
has_settings :export, class_name: 'Settings::Template' do |s|
s.key :export, defaults: Settings::Template::DEFAULT_SETTINGS
end
validates :org, :title, presence: { message: _("can't be blank") }
# =================
# = Class Methods =
# =================
def self.default
where(is_default: true, published: true).last
end
def self.current(family_id)
unarchived.where(family_id: family_id).order(version: :desc).first
end
def self.live(family_id)
if family_id.respond_to?(:each)
unarchived.where(family_id: family_id, published: true)
else
unarchived.where(family_id: family_id, published: true).first
end
end
def self.find_or_generate_version!(template)
if template.latest? && template.generate_version?
template.generate_version!
elsif template.latest? && !template.generate_version?
template
else
raise _('A historical template cannot be retrieved for being modified')
end
end
# Retrieves the latest templates, i.e. those with maximum version associated.
# It can be filtered down if family_id is passed. NOTE, the template objects
# instantiated only contain version and family attributes populated. See
# Template::latest_version scope method for an adequate instantiation of
# template instances.
def self.latest_version_per_family(family_id = nil)
chained_scope = unarchived.select('MAX(version) AS version', :family_id)
chained_scope = chained_scope.where(family_id: family_id) if family_id.present?
chained_scope.group(:family_id)
end
def self.latest_customized_version_per_customised_of(customization_of = nil,
org_id = nil)
chained_scope = select('MAX(version) AS version', :customization_of)
chained_scope = chained_scope.where(customization_of: customization_of)
chained_scope = chained_scope.where(org_id: org_id) if org_id.present?
chained_scope.group(:customization_of)
end
# ===========================
# = Public instance methods =
# ===========================
# Creates a copy of the current template
# raises ActiveRecord::RecordInvalid when save option is true and validations
# fails.
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def deep_copy(attributes: {}, **options)
copy = dup
if attributes.respond_to?(:each_pair)
attributes.each_pair do |attribute, value|
copy.send(:"#{attribute}=", value) if copy.respond_to?(:"#{attribute}=")
end
end
copy.save! if options.fetch(:save, false)
options[:template_id] = copy.id
phases.each { |phase| copy.phases << phase.deep_copy(**options) }
# transfer the conditions to the new template
# done here as the new questions are not accessible when the conditions deep copy
copy.conditions.each do |cond|
if cond.option_list.any?
versionable_ids = QuestionOption.where(id: cond.option_list).pluck(:versionable_id)
cond.option_list = copy.question_options.where(versionable_id: versionable_ids)
.pluck(:id).map(&:to_s)
# TODO: these seem to be stored as strings, not sure if that's required by other code
# TODO: would it be safe to remove conditions without an option list?
end
if cond.remove_data.any?
versionable_ids = Question.where(id: cond.remove_data).pluck(:versionable_id)
cond.remove_data = copy.questions.where(versionable_id: versionable_ids)
.pluck(:id).map(&:to_s)
end
cond.save if cond.changed?
end
copy
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
# Retrieves the template's org or the org of the template this one is derived
# from of it is a customization
def base_org
if customization_of.present?
Template.where(family_id: customization_of).first.org
else
org
end
end
# Is this the latest version of the current Template's family?
#
# Returns Boolean
def latest?
id == Template.latest_version(family_id).pluck('templates.id').first
end
# Determines whether or not a new version should be generated
def generate_version?
published
end
# Determines whether or not a customization for the customizing_org passed
# should be generated
def customize?(customizing_org)
if customizing_org.is_a?(Org) && (org.funder_only? || is_default)
return !Template.unarchived.where(customization_of: family_id,
org: customizing_org).exists?
end
false
end
# Determines whether or not a customized template should be upgraded
def upgrade_customization?
return false unless customization_of?
funder_template = Template.published(customization_of).select(:created_at).first
return false unless funder_template.present?
funder_template.created_at > created_at
end
# Checks to see if the template family has a published version and if its not
# the current template
def draft?
!published && !Template.published(family_id).empty?
end
def removable?
versions = Template.includes(:plans).where(family_id: family_id)
versions.reject { |version| version.plans.empty? }.empty?
end
# Returns a new unpublished copy of self with a new family_id, version = zero
# for the specified org
def generate_copy!(org)
# Assume customizing_org is persisted
raise _('generate_copy! requires an organisation target') unless org.is_a?(Org)
args = {
attributes: {
version: 0,
published: false,
family_id: new_family_id,
org: org,
is_default: false,
title: format(_('Copy of %{template}'), template: title)
}, modifiable: true, save: true
}
deep_copy(**args)
end
# Generates a new copy of self with an incremented version number
def generate_version!
raise _('generate_version! requires a published template') unless published
args = {
attributes: {
version: version + 1,
published: false,
org: org
}, save: true
}
deep_copy(**args)
end
# Generates a new copy of self for the specified customizing_org
def customize!(customizing_org)
# Assume customizing_org is persisted
raise ArgumentError, _('customize! requires an organisation target') unless customizing_org.is_a?(Org)
# Assume self has org associated
raise ArgumentError, _('customize! requires a template from a funder') if !org.funder_only? && !is_default
args = {
attributes: {
version: 0,
published: false,
family_id: new_family_id,
customization_of: family_id,
org: customizing_org,
visibility: Template.visibilities[:organisationally_visible],
is_default: false
}, modifiable: false, save: true
}
deep_copy(**args)
end
# Generates a new copy of self including latest changes from the funder this
# template is customized_of
def upgrade_customization!
Template::UpgradeCustomizationService.call(self)
end
def publish
update(published: true)
end
def publish!
update!(published: true)
end
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def publishability
error = ''
publishable = true
# template must be the most recent draft
if published
error += _('You can not publish a published template. ')
publishable = false
end
unless latest?
error += _('You can not publish a historical version of this template. ')
publishable = false
# all templates have at least one phase
end
if phases.count <= 0
error += _('You can not publish a template without phases. ')
publishable = false
# all phases must have at least 1 section
end
unless phases.map { |p| p.sections.count.positive? }.reduce(true) { |fin, val| fin && val }
error += _('You can not publish a template without sections in a phase. ')
publishable = false
# all sections must have at least one question
end
unless sections.map { |s| s.questions.count.positive? }.reduce(true) { |fin, val| fin && val }
error += _('You can not publish a template without questions in a section. ')
publishable = false
end
if invalid_condition_order
error += _('Conditions in the template refer backwards')
publishable = false
end
[publishable, error]
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
# TODO: refactor to use UniqueRandom
# Generate a new random family identifier
def self.new_family_id
loop do
random = rand 2_147_483_647
break random unless Template.exists?(family_id: random)
end
end
private
# ============================
# = Private instance methods =
# ============================
def new_family_id
Template.new_family_id
end
# Only one version of a template should be published at a time, so if this
# one was published make sure other versions are not
def reconcile_published
# Unpublish all other versions of this template family
Template.published
.where(family_id: family_id)
.where.not(id: id)
.update_all(published: false)
end
def invalid_condition_order
questions.each do |question|
next unless question.option_based?
question.conditions.each do |condition|
next unless condition.action_type == 'remove'
condition.remove_data.each do |rem_id|
rem_question = Question.find(rem_id.to_s)
return true if before(rem_question, question)
end
end
end
false
end
def before(question1, question2)
question1.section.number < question2.section.number ||
(question1.section.number == question2.section.number && question1.number < question2.number)
end
end
# rubocop:enable Metrics/ClassLength