Make stuff sortable
This commit is contained in:
parent
50e853098b
commit
ee5dbcf33e
21 changed files with 161 additions and 28 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue