a11yist/app/models/checklist_entry.rb
david f21f75ad62
Some checks failed
/ Run tests (push) Failing after 15s
/ Run system tests (push) Failing after 14s
/ Build, push and deploy image (push) Has been skipped
Add wcag en 2.2 import
2024-10-27 23:42:06 +01:00

21 lines
619 B
Ruby

# frozen_string_literal: true
class ChecklistEntry < ApplicationRecord
belongs_to :checklist
belongs_to :check
before_validation :set_position
before_create :update_positions
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