a11yist/app/models/checklist_entry.rb
david d1294c2fc4
Some checks failed
/ Run tests (push) Successful in 1m22s
/ Run system tests (push) Failing after 1m33s
/ Build, push and deploy image (push) Successful in 3m22s
Refactorings and gui improvements
2024-11-03 21:58:25 +01:00

20 lines
585 B
Ruby

# frozen_string_literal: true
class ChecklistEntry < ApplicationRecord
belongs_to :checklist
belongs_to :check
before_validation :set_position
before_update :update_positions, if: :position_changed?
def set_position
self.position ||= (checklist.checklist_entries.pluck(:position).max || 0) + 1
end
def update_positions
if position_was
checklist.checklist_entries.where("position > ?", position_was).update_all("position = position - 1")
end
checklist.checklist_entries.where(position: position..).update_all("position = position + 1")
end
end