aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/compliance_control_set_cloner.rb93
-rw-r--r--lib/tasks/erd.rake4
2 files changed, 95 insertions, 2 deletions
diff --git a/lib/compliance_control_set_cloner.rb b/lib/compliance_control_set_cloner.rb
new file mode 100644
index 000000000..12e1eccb5
--- /dev/null
+++ b/lib/compliance_control_set_cloner.rb
@@ -0,0 +1,93 @@
+class ComplianceControlSetCloner
+
+ # Naming Convention: As we are in a domain with quite long names we
+ # abbreviate compliance_control to cc and
+ # compliance_check to cck iff used as prefixes.
+
+ attr_reader :organisation_id, :source_set_id
+
+ def copy source_set_id, organisation_id
+ @source_set_id = source_set_id
+ @organisation_id = organisation_id
+ copy_set
+ end
+
+
+ private
+
+ # Workers
+ # -------
+
+ # Copy Set:
+ def copy_set
+ # Force lazy creation of target_set, just in case source_set is _empty_.
+ target_set
+ copy_controls
+ copy_blocks
+ end
+
+ # Copy Blocks:
+ def copy_block source_block
+ target_set.compliance_control_blocks.create(
+ name: name_of_copy(:compliance_control_blocks, source_block.name),
+ condition_attributes: source_block.condition_attributes).tap do | target_block |
+ relink_checks_to_block source_block, target_block
+ end
+ end
+ def copy_blocks
+ source_set.compliance_control_blocks.order(:id).each(&method(:copy_block))
+ end
+ def relink_checks_to_block source_block, target_block
+ source_block
+ .compliance_controls
+ .order(:id)
+ .each do | source_control |
+ control_id_map[source_control.id]
+ .update(compliance_control_block_id: target_block.id)
+ end
+ end
+
+ # Copy Controls:
+ def copy_controls
+ source_set.compliance_controls.order(:id).each(&method(:copy_control))
+ end
+ def copy_control(compliance_control)
+ target_set.compliance_controls.create(
+ code: compliance_control.code,
+ comment: compliance_control.comment,
+ control_attributes: compliance_control.control_attributes,
+ criticity: compliance_control.criticity,
+ name: name_of_copy(:compliance_controls, compliance_control.name),
+ origin_code: compliance_control.origin_code,
+ type: compliance_control.type
+ ).tap do | control |
+ control_id_map.update compliance_control.id => control
+ end
+ end
+
+ def name_of_copy resource, name
+ [I18n.t("#{resource}.clone.prefix"), name].join(' ')
+ end
+
+ # Lazy Values
+ # -----------
+ def organisation
+ @__organisation__ ||= Organisation.find(organisation_id)
+ end
+ def source_set
+ @__source_set__ ||= ComplianceControlSet.find(source_set_id)
+ end
+ def target_set
+ @__target_set__ ||= ComplianceControlSet.create!(
+ organisation: organisation,
+ name: name_of_copy(:compliance_control_sets, source_set.name)
+ )
+ end
+ def control_id_map
+ # Map: compliance_control_id -> compliance_control (origin_id -> copied object)
+ @__control_id_to_check__ ||= Hash.new
+ end
+ def referential
+ @__referential__ ||= Referential.find(referential_id)
+ end
+end
diff --git a/lib/tasks/erd.rake b/lib/tasks/erd.rake
index 6b79967de..e2665374e 100644
--- a/lib/tasks/erd.rake
+++ b/lib/tasks/erd.rake
@@ -7,9 +7,9 @@ namespace :generate do
sh "bundle exec rake erd only='Organisation,StopAreaReferential,StopAreaReferentialSync,StopAreaReferentialSyncMessage,StopAreaReferentialMembership,LineReferential,LineReferentialSync,LineReferentialSyncMessage,LineReferentialMembership' filename='referentiels_externes' title='Référentiels externes'"
sh "bundle exec rake erd only='NetexImport,Import,WorkbenchImport,ImportResource,ImportMessage' filename='import' title='Import'"
sh "bundle exec rake erd only='ComplianceControlSet,ComplianceControlBlock,ComplianceControl,ComplianceCheckSet,ComplianceCheckBlock,ComplianceCheck,ComplianceCheckResource,ComplianceCheckMessage' filename='validation' title='Validation'"
+ sh "bundle exec rake erd only='Organisation,Workbench,ReferentialSuite,Referential' filename='merge' title='Merge'"
#sh "bundle exec rake erd only='VehicleJourney,VehicleJourneyExport' filename='export' title='Export'"
- #sh "bundle exec rake erd only='' filename='intégration' title='Integration'"
- #sh "bundle exec rake erd only='' filename='fusion' title='Fusion'"
+ #sh "bundle exec rake erd only='' filename='integration' title='Integration'"
#sh "bundle exec rake erd only='' filename='publication' title='Publication'"
end