Module: DragOrder::Models::Page

Defined in:
lib/drag_order/models/page.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/drag_order/models/page.rb', line 9

def self.included(base)
  base.class_eval do
    
    before_validation_on_create :set_initial_position
    
    self.reflections[:children].options[:order] = "position ASC"
    
  private
    def set_initial_position
      self.position ||= begin
        if last_sibling = Page.find_by_parent_id(parent_id, :order => [ "position DESC" ])
          last_sibling.position + 1
        else
          0
        end
      end
    end
    
  end
end