zorab47 / active_admin-sortable_tree

Show ActiveAdmin index as a nested tree with drag'n'drop
MIT License
162 stars 127 forks source link

Nested Resource Shows all items in the sortable index view #30

Closed asecondwill closed 10 years ago

asecondwill commented 10 years ago

I'm trying to make a menu management system for aa using your (awesome) gem. It should be simple enough, however I've hit a snag where the sortable view is showing all of the records, not just the ones for the nested resource.

Is that my user error or is this something the gem doesn't provide? (yet?)

ActiveAdmin.register MenuItem do
    config.filters = false
    config.paginate = false
  belongs_to :menu
    sortable tree: true,
           collapsible: true,      # hides +/- buttons
           start_collapsed: true

    permit_params :title, :url, :menu_id

   index  as: :table do
     column :title
   end

    index as: :sortable do
        label "Title" do |menu_item|
            link_to menu_item.title, edit_admin_menu_menu_item_path( menu_item.menu, menu_item )
        end
        actions defaults: false do |menu_item|
            link_to "Delete", admin_menu_menu_item_path( menu_item.menu, menu_item ), method: "delete", confirm: "Are you sure?"
        end
    end

    form do |f|
        f.inputs "Details" do
            f.input :title
            f.input :url
            f.input :menu_id, :as => :hidden
        end

        f.actions
    end

    controller do
        def create
            create! do |format|
                format.html { redirect_to_index }
            end
        end

        def update
            update! do |format|
                format.html { redirect_to_index }
            end
        end

        private

            def redirect_to_index
              @menu = Menu.find params[:menu_id]
        redirect_to admin_menu_menu_items_path( @menu )
            end
    end
end
zorab47 commented 10 years ago

By default the sortable tree finds all roots for the model specified, regardless of nesting. This behavior may be customized through the roots_collection option.

ActiveAdmin.register MenuItem do
  belongs_to :menu
  sortable tree: true,
           # Only display the parent menu's roots
           roots_collection: proc { parent.menu_items.roots }
end
asecondwill commented 10 years ago

@zorab47 - thanks so much. i've now got multiple hierarchical menu's working : )