a11yist/app/models/checklist_entry.rb

22 lines
595 B
Ruby
Raw Permalink Normal View History

2024-09-05 22:54:38 +02:00
# frozen_string_literal: true
class ChecklistEntry < ApplicationRecord
belongs_to :checklist
belongs_to :check
2024-07-20 16:52:12 +02:00
before_validation :set_position
2024-09-05 22:54:38 +02:00
before_update :update_positions, if: :position_changed?
2024-07-20 16:52:12 +02:00
2024-11-11 04:04:13 +01:00
private
2024-07-20 16:52:12 +02:00
def set_position
2024-09-05 22:54:38 +02:00
self.position ||= (checklist.checklist_entries.pluck(:position).max || 0) + 1
2024-07-20 16:52:12 +02:00
end
2024-09-05 22:54:38 +02:00
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