Module: DragOrder::Controllers::Admin::PagesController

Defined in:
lib/drag_order/controllers/admin/pages_controller.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/drag_order/controllers/admin/pages_controller.rb', line 6

def self.included(base)
  base.class_eval do
    
    before_filter :drag_assets, :only => [:index, :remove]
    
    def sort
      begin
        parent = Page.find(params[:parent_id])
        params[:children].split(',').each_with_index do |child,index|
          Page.find(child).update_attributes!(
            :position  => index,
            :parent_id => params[:parent_id]
          )
        end
        
        respond_to do |format|
          format.js { render :text => 'Pages successfully sorted.' }
        end
      rescue Exception => e # Without this resource controller will exception when it looks for Page.find('sort.js')
        respond_to do |format|
          format.js { render :text => 'Could not sort Pages.', :status => :unprocessable_entity }
        end
      end
    end
    
    private
    
    def drag_assets
      @javascripts << 'admin/dragdrop.js'
      @javascripts << 'admin/sortable_tree.js'
      @javascripts << 'admin/extensions/drag_order/drag_order.js'
      @stylesheets << 'admin/extensions/drag_order/drag_order.css'
    end
    
  end
end