a11yist/app/models/checklist_entry.rb
david 63fc206c27
Some checks failed
/ Run tests (push) Successful in 8m56s
/ Run system tests (push) Failing after 1h0m48s
/ Build, push and deploy image (push) Successful in 7m47s
A lot :)
2024-09-05 22:54:38 +02:00

28 lines
937 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 normalize_positions
checklist.checklist_entries.where.not(id:).find_by(position:)&.update_attribute(:position, position_was)
# checklist.checklist_entries.order(:position).each_with_index do |entry, index|
# entry.update_column(:position, index + 1) if entry.position != index + 1
# end
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