Make stuff sortable
This commit is contained in:
parent
50e853098b
commit
ee5dbcf33e
21 changed files with 161 additions and 28 deletions
|
|
@ -7,6 +7,7 @@ class ChecklistEntry < ApplicationRecord
|
|||
before_validation :set_position
|
||||
before_update :update_positions, if: :position_changed?
|
||||
|
||||
private
|
||||
def set_position
|
||||
self.position ||= (checklist.checklist_entries.pluck(:position).max || 0) + 1
|
||||
end
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1,13 +1,22 @@
|
|||
class Page < ApplicationRecord
|
||||
belongs_to :report, touch: true
|
||||
has_many :elements, dependent: :destroy
|
||||
has_many :elements, -> { order(:position) }, dependent: :destroy
|
||||
|
||||
has_rich_text :comment
|
||||
|
||||
before_validation :set_position
|
||||
before_update :update_positions, if: :position_changed?
|
||||
|
||||
|
||||
private
|
||||
def set_position
|
||||
self.position ||= (report.pages.pluck(:position).max || 0) + 1
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Report < ApplicationRecord
|
||||
has_many :pages, dependent: :destroy
|
||||
has_many :pages, -> { order(:position) }, dependent: :destroy
|
||||
has_many :elements, through: :pages, dependent: :destroy
|
||||
has_rich_text :comment
|
||||
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ class SuccessCriterion < ApplicationRecord
|
|||
enum :level, %i[A AA AAA]
|
||||
|
||||
before_save :set_position
|
||||
before_update :update_positions, if: :position_changed?
|
||||
|
||||
def level_value
|
||||
return nil unless level
|
||||
|
|
@ -26,9 +27,20 @@ class SuccessCriterion < ApplicationRecord
|
|||
"HEADER"
|
||||
end
|
||||
|
||||
def number
|
||||
[ page.position, element.position, position ].join(".")
|
||||
end
|
||||
|
||||
private
|
||||
def set_position
|
||||
self.position ||= (element.success_criteria.pluck(:position).max || 0) + 1
|
||||
Rails.logger.debug("set position: "+position.to_s)
|
||||
end
|
||||
|
||||
def update_positions
|
||||
if position_was
|
||||
element.success_criteria.where("position > ?", position_was).update_all("position = position - 1")
|
||||
end
|
||||
element.success_criteria.where(position: position..).update_all("position = position + 1")
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue