Do not touch protocol if step or checklist is checked [SCI-7584]

This commit is contained in:
Martin Artnik 2023-01-04 12:33:11 +01:00
parent 9e75a54635
commit 15543d5222
2 changed files with 29 additions and 3 deletions

View file

@ -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|

View file

@ -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|