Make stuff sortable

This commit is contained in:
david 2024-11-11 04:04:13 +01:00
parent 50e853098b
commit ee5dbcf33e
21 changed files with 161 additions and 28 deletions

View file

@ -8,7 +8,8 @@ class Element < ApplicationRecord
delegate :report, to: :page
after_validation :set_position
before_validation :set_position
before_update :update_positions, if: :position_changed?
# Calculate actual conformity level:
# - if a success_criterion has result :failed -> the confirmity_level
@ -33,12 +34,20 @@ class Element < ApplicationRecord
@max_level ||= success_criteria.reject(&:not_applicable?).max(&:level)
end
def number
"#{page.position}.#{position}"
end
private
def set_position
Rails.logger.debug("element: position #{position}")
self.position ||= (page.elements.pluck(:position).max || 0) + 1
end
def number
"#{page.position}.#{position}"
def update_positions
if position_was
page.elements.where("position > ?", position_was).update_all("position = position - 1")
end
page.elements.where(position: position..).update_all("position = position + 1")
end
end