diff --git a/app/controllers/rules_controller.rb b/app/controllers/rules_controller.rb index 883e6539..825e21de 100644 --- a/app/controllers/rules_controller.rb +++ b/app/controllers/rules_controller.rb @@ -74,6 +74,9 @@ def update def destroy if @rule.update(deleted_at: Time.zone.now) + @rule.additional_answers.destroy_all + @rule.reviews.destroy_all + @rule.satisfied_by.destroy_all render json: { toast: 'Successfully deleted control.' } else render json: { @@ -103,9 +106,11 @@ def revert def create_or_duplicate if authorize_author_project.nil? && rule_create_params[:duplicate] - rule = Rule.find(rule_create_params[:id]).amoeba_dup - rule.rule_id = nil - rule + rule = Rule.find(rule_create_params[:id]) + rule.update_single_rule_clone(true) + new_rule = rule.amoeba_dup + new_rule.rule_id = nil + new_rule elsif authorize_admin_project.nil? srg = SecurityRequirementsGuide.find_by(id: @component.security_requirements_guide_id) srg_rule = srg.parsed_benchmark.rule.find { |r| r.ident.reject(&:legacy).first.ident == 'CCI-000366' } diff --git a/app/models/rule.rb b/app/models/rule.rb index 40582f41..b2aa9287 100644 --- a/app/models/rule.rb +++ b/app/models/rule.rb @@ -10,7 +10,7 @@ class Rule < BaseRule nullify :review_requestor_id set locked: false - include_association :additional_answers + include_association :additional_answers, if: :single_rule_clone? end audited except: %i[component_id review_requestor_id created_at updated_at locked inspec_control_file], @@ -53,6 +53,13 @@ class Rule < BaseRule default_scope { where(deleted_at: nil) } + @single_rule_clone = false + + # If rule clone not coming from a "copy component" action, allow "answers" to be also cloned + def update_single_rule_clone(rule_clone) + @single_rule_clone = rule_clone + end + def self.from_mapping(rule_mapping, component_id, idx, srg_rules) rule = super(self, rule_mapping) rule.audits.build(Audited.audit_class.create_initial_rule_audit_from_mapping(component_id)) @@ -244,6 +251,10 @@ def basic_fields private + def single_rule_clone? + @single_rule_clone + end + def export_fixtext satisfied_by.size.positive? ? satisfied_by.first.fixtext : fixtext end @@ -333,6 +344,9 @@ def apply_audit_comment end def update_component_rules_count + # don't update component rules count for copy or duplicate component actions + return unless @single_rule_clone + component.rules_count = component.rules.where(deleted_at: nil).size component.save end