From 15543d52220712b2fcc96efae9069b81a806b2f6 Mon Sep 17 00:00:00 2001 From: Martin Artnik Date: Wed, 4 Jan 2023 12:33:11 +0100 Subject: [PATCH] Do not touch protocol if step or checklist is checked [SCI-7584] --- app/models/checklist_item.rb | 16 ++++++++++++++-- app/models/step.rb | 16 +++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/app/models/checklist_item.rb b/app/models/checklist_item.rb index db055e221..c99610e63 100644 --- a/app/models/checklist_item.rb +++ b/app/models/checklist_item.rb @@ -8,8 +8,7 @@ class ChecklistItem < ApplicationRecord validates :position, uniqueness: { scope: :checklist }, unless: -> { position.nil? } belongs_to :checklist, - inverse_of: :checklist_items, - touch: true + inverse_of: :checklist_items belongs_to :created_by, foreign_key: 'created_by_id', class_name: 'User', @@ -21,8 +20,21 @@ class ChecklistItem < ApplicationRecord after_destroy :update_positions + # conditional touch excluding checked updates + after_destroy :touch_checklist + after_save :touch_checklist + private + def touch_checklist + # if only checklist item checked attribute changed, do not touch checklist + return if saved_changes.keys.sort == %w(checked updated_at) + + # rubocop:disable Rails/SkipsModelValidations + checklist.touch + # rubocop:enable Rails/SkipsModelValidations + end + def update_positions transaction do checklist.checklist_items.order(position: :asc).each_with_index do |checklist_item, i| diff --git a/app/models/step.rb b/app/models/step.rb index 2fa1f93ad..856537dbe 100644 --- a/app/models/step.rb +++ b/app/models/step.rb @@ -24,9 +24,13 @@ class Step < ApplicationRecord before_destroy :cascade_before_destroy after_destroy :adjust_positions_after_destroy, unless: -> { skip_position_adjust } + # conditional touch excluding step completion updates + after_destroy :touch_protocol + after_save :touch_protocol + belongs_to :user, inverse_of: :steps belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User', optional: true - belongs_to :protocol, inverse_of: :steps, touch: true + belongs_to :protocol, inverse_of: :steps has_many :step_orderable_elements, inverse_of: :step, dependent: :destroy has_many :checklists, inverse_of: :step, dependent: :destroy has_many :step_comments, foreign_key: :associated_id, dependent: :destroy @@ -174,8 +178,18 @@ class Step < ApplicationRecord new_step end end + private + def touch_protocol + # if only step completion attributes were changed, do not touch protocol + return if saved_changes.keys.sort == %w(completed completed_on updated_at) + + # rubocop:disable Rails/SkipsModelValidations + protocol.touch + # rubocop:enable Rails/SkipsModelValidations + end + def adjust_positions_after_destroy re_index_following_steps protocol.steps.where('position > ?', position).order(:position).each do |step|