Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

524 fix answer cloning issue #525

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions app/controllers/rules_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ def update

def destroy
if @rule.update(deleted_at: Time.zone.now)
@rule.additional_answers.destroy_all
rlakey marked this conversation as resolved.
Show resolved Hide resolved
@rule.reviews.destroy_all
@rule.satisfied_by.destroy_all
render json: { toast: 'Successfully deleted control.' }
else
render json: {
Expand Down Expand Up @@ -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' }
Expand Down
16 changes: 15 additions & 1 deletion app/models/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down