2024-10-31 23:13:18 +01:00
|
|
|
class Page < ApplicationRecord
|
2024-11-01 03:59:49 +01:00
|
|
|
belongs_to :report, touch: true
|
2024-11-11 04:04:13 +01:00
|
|
|
has_many :elements, -> { order(:position) }, dependent: :destroy
|
2024-10-31 23:13:18 +01:00
|
|
|
|
|
|
|
|
has_rich_text :comment
|
2024-11-01 03:26:46 +01:00
|
|
|
|
|
|
|
|
before_validation :set_position
|
2024-11-11 04:04:13 +01:00
|
|
|
before_update :update_positions, if: :position_changed?
|
|
|
|
|
|
2024-11-01 03:26:46 +01:00
|
|
|
|
|
|
|
|
private
|
|
|
|
|
def set_position
|
|
|
|
|
self.position ||= (report.pages.pluck(:position).max || 0) + 1
|
|
|
|
|
end
|
2024-11-11 04:04:13 +01:00
|
|
|
|
|
|
|
|
def update_positions
|
|
|
|
|
if position_was
|
|
|
|
|
report.pages.where("position > ?", position_was).update_all("position = position - 1")
|
|
|
|
|
end
|
|
|
|
|
report.pages.where(position: position..).update_all("position = position + 1")
|
|
|
|
|
end
|
2024-10-31 23:13:18 +01:00
|
|
|
end
|