Sha256: d3b431b586b3d1ae54b2fbf8906de5f0d4ebf253eddb49eb2f642fb2690d9b5e

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

class SortableNestedSetController < ActionController::Base
  def sns_sort
    category_class = params[:category_class].classify.constantize

    matches = params[:moved].match(/(\w+)_(\d+)$/)
    moved_type = matches[1]
    moved_id = matches[2].to_i
    moved_parent = nil
    tree = {} # category_id => [children of move_type]

    for node in params[:tree].scan(/(\w+)\[(\d+)\]=(\w+)/)
      type = node[0]
      next unless type == moved_type

      id = node[1].to_i
      parent = (node[2] == 'root') ? nil : node[2].to_i

      moved_parent = parent if id == moved_id

      tree[parent] ||= []
      tree[parent] << id
    end

    if moved_type == category_class.sns_items_type.to_s.singularize
      item = category_class.sns_items_type.to_s.classify.constantize.find(moved_id)
      item.remove_from_list if item.respond_to?(:remove_from_list) # acts_as_list support
      item.update_attribute(category_class.name.foreign_key, moved_parent)
      item.insert_at(tree[moved_parent].index(moved_id)) if item.respond_to?(:insert_at) # acts_as_list support
    else
      moved_type.classify.constantize.find(moved_id).move(moved_parent, tree[moved_parent])
    end

    render :nothing => true
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sortable_nested_set-0.4.2 app/controllers/sortable_nested_set_controller.rb
sortable_nested_set-0.4.1 app/controllers/sortable_nested_set_controller.rb
sortable_nested_set-0.4.0 app/controllers/sortable_nested_set_controller.rb
sortable_nested_set-0.3.0 app/controllers/sortable_nested_set_controller.rb