Sha256: e5522ab393c3695d23cf8e5986f4f842cc35c681827d252d491f248274e5ff21

Contents?: true

Size: 1.71 KB

Versions: 15

Compression:

Stored size: 1.71 KB

Contents

module Gluttonberg
  module DragTree
    module ActionController

      def self.included(base)
           base.extend(ClassMethods)
      end


      module ClassMethods
        def drag_tree(model_class, options = {})
          self.send(:include, Gluttonberg::DragTree::ActionController::ControllerHelperClassMethods)
          self.set_drag_tree(model_class, options)
        end

      end # class methods

      module ControllerHelperClassMethods
        def self.included(klass)
          klass.class_eval do
            @drag_tree_model_class = nil
            @drag_tree_route_name = nil

            def klass.drag_class
              @drag_tree_model_class
            end

            def klass.set_drag_tree(model_class, options = {})
              @drag_tree_route_name = options[:route_name] if options[:route_name]
              @drag_tree_model_class = model_class
            end

            def klass.drag_tree_route_name
              if @drag_tree_route_name then
                @drag_tree_route_name
              else
                "#{self.controller_name}/move_node".to_sym
              end
            end

            def move_node
              if params[:element_ids].blank?
                render :json => {:success => false}
                return
              end
              ids = params[:element_ids].split(",")
              elements = self.class.drag_class.find_by_sorted_ids(ids )
              elements.each_with_index do |element , index|
                attr = {:position => index + 1  }
                element.update_attributes!( attr   )
              end
              render :json => {:success => true}
            end

          end
        end
      end

    end
  end #DragTree
end  # Gluttonberg

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
gluttonberg-core-2.6.4 lib/gluttonberg/drag_tree/action_controller.rb
gluttonberg-core-2.6.3 lib/gluttonberg/drag_tree/action_controller.rb
gluttonberg-core-2.6.2 lib/gluttonberg/drag_tree/action_controller.rb
gluttonberg-core-2.6.1 lib/gluttonberg/drag_tree/action_controller.rb
gluttonberg-core-2.6.0 lib/gluttonberg/drag_tree/action_controller.rb
gluttonberg-core-2.5.9 lib/gluttonberg/drag_tree/action_controller.rb
gluttonberg-core-2.5.8 lib/gluttonberg/drag_tree/action_controller.rb
gluttonberg-core-2.5.7 lib/gluttonberg/drag_tree/action_controller.rb
gluttonberg-core-2.5.6 lib/gluttonberg/drag_tree/action_controller.rb
gluttonberg-core-2.5.5 lib/gluttonberg/drag_tree/action_controller.rb
gluttonberg-core-2.5.4 lib/gluttonberg/drag_tree/action_controller.rb
gluttonberg-core-2.5.3 lib/gluttonberg/drag_tree/action_controller.rb
gluttonberg-core-2.5.2 lib/gluttonberg/drag_tree/action_controller.rb
gluttonberg-core-2.5.1 lib/gluttonberg/drag_tree/action_controller.rb
gluttonberg-core-2.5 lib/gluttonberg/drag_tree/action_controller.rb