No release in over 3 years
Low commit activity in last 3 years
SortableTree provides sorting of lists and hierarchies from ActiveAdmin index views.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 5.0, < 6.1
>= 0
 Project Readme

ActiveAdmin::SortableTree

This fork allows to work with awesome nested set & adds custom HTML in label

Build Status

This gem adds a tree and a list view to your ActiveAdmin resource index, both sortable via drag'n'drop.

ActiveAdmin::SortableTree Example

Installation

# Gemfile
gem "activeadmin"
gem "rs-active_admin-sortable_tree"
# app/assets/javascripts/active_admin.js
//= require active_admin/sortable

# app/assets/stylesheets/active_admin.scss
@import "active_admin/sortable";

Usage (Tree)

Admin:

# app/admin/page.rb
ActiveAdmin.register Page do
  sortable tree: true

  index :as => :sortable do
    label :title # item content
        actions
  end
end

Custom HTML in label

# app/admin/home_item.rb
ActiveAdmin.register HomeItem do
  member_action :toggle, method: :get do
    resource.update_attribute :enabled, !resource.enabled

    render js: "$('#home_item_#{resource.id} .status_tag').removeClass('yes').removeClass('no').addClass('#{resource.enabled ? :yes : :no}').text('#{resource.enabled ? "Вкл" : "Выкл"}')"
  end

  index as: :sortable do
    label do |r|
      span(r.id) +
      span(r.name) + span(" ") +
      a("data-href": toggle_admin_home_item_path(r), class: "status_tag #{r.enabled ? :yes : :no}") do
        r.enabled ? "Вкл" : "Выкл"
      end
    end
  end
end
// app/assets/javascripts/active_admin.js

$(document).on("click", ".status_tag", function(e) {
  e.preventDefault()
  e.stopPropagation();
  var $t = $(this)
  $.ajax({
    url: $t.data("href"),
    success: function (r) {
      eval(r)
    },
    error: function(e) {
      console.error(e)
    }
  })
})

Model: ActiveAdmin::SortableTree is agnostic to the tree implementation. All you have to do is expose a sorting attribute and a few tree methods (:parent, :children and :roots). Let's say you use Ancestry:

class Page < ActiveRecord::Base
  attr_accessible :title, :body, :position
  has_ancestry :orphan_strategy => :rootify
end

You can configure these methods if you need:

ActiveAdmin.register Page do
  sortable tree: true,
           sorting_attribute: :position,
           parent_method: :parent,
           children_method: :children,
           roots_method: :roots,
           roots_collection: proc { current_user.pages.roots }
  # …
end

The option roots_collection provides full control on how to find the root nodes of your sortable tree and is evaluated within the context of the controller. Please note that roots_collection will override what is specified in roots_method.

Usage (List)

Admin:

# app/admin/page.rb
ActiveAdmin.register Page do
  sortable

  index :as => :sortable do
    label :title # item content
    actions
  end
end

Model: Sortable list assumes you have a :position field in your resource. Of course it's configurable:

ActiveAdmin.register Page do
  sortable tree: false, # default
           sorting_attribute: :my_position_field
  # …
end

Note: If you are using the acts_as_list gem to manage a :position field (not required, but allows for other nice programmatic manipulation of ordered model lists), you must ensure a zero-based index for your list using the top_of_list option:

class Page < ActiveRecord::Base
  # Make this list act like a zero-indexed array to avoid off-by-one errors in your sorting
  acts_as_list top_of_list: 0
end

Usage (generic ActiveAdmin index)

Currently supports only IndexAsBlock, more to come!

Admin:

# app/admin/page.rb
ActiveAdmin.register Page do
  sortable

  index :as => :block do |page|
    # item content
  end
end

Model: Same as list view (see above)

Customization

Full options list with defaults

ActiveAdmin.register Page do
  sortable tree: true,
           max_levels: 0,               # infinite indent levels
           protect_root: false,         # allow root items to be dragged
           sorting_attribute: :position,
           parent_method: :parent,
           children_method: :children,
           roots_method: :roots,
           roots_collection: nil,       # proc to specifiy retrieval of roots
           sortable: true,              # Disable sorting (use only 'tree' functionality)
           collapsible: false,          # show +/- buttons to collapse children
           start_collapsed: false,      # when collapsible, start with all roots collapsed
end

Actions

In IndexAsSortable you can add custom actions (with or without the defaults):

index :as => :sortable do
  actions defaults: false do |page|
    link_to "Custom action", my_custom_path(page)
  end
end

Ajax Callback Config

It exposes three Ajax Events: ajaxDone, ajaxFail and ajaxAlways, which correspond to jQuery ajax callbacks: done, fail and always.

To subscribe Ajax callback:

ActiveAdminSortableEvent.add('ajaxDone', function (){
  // do what you want
})

Upgrading to SortableTree 2.0 from 1.0

Upgrading from SortableTree 1.x requires manually specifying assets in your app/assets/javascripts/active_admin.js and app/assets/stylesheets/active_admin.scss files.

Dependencies

ActiveAdmin::SortableTree 2.0 supports ActiveAdmin 1.0.0+. For previous versions of ActiveAdmin use older SortableTree versions from the 1.x branch.

Note: If you experience issues with drag and drop capability, you may need to specify the version for your ActiveAdmin installation. It is reported working using v0.6.6, or if you are using v1.0.0.pre, it is reported working on this commit b3a9f4b or later.

# Gemfile
gem 'activeadmin', github: 'activeadmin', ref: 'b3a9f4b'

Semantic Versioning

ActiveAdmin::SortableTree follows semantic versioning.

Alternatives

Copyright

Copyright © 2013 Francesco Disperati, Cantiere Creativo. See the file MIT-LICENSE for details. See the full list list of contributors.