MiniTree
MiniTree is a Rails gem to display and handle a treeview. It supports moving/reordering items in the treeview, collapsing/expanding of a subtree and creating/deleting them.
Items in the treeview can be enhanced with links to trigger actions.
MiniTree requires just a basic Rails system. Specifically, besides Stimulus no other Javascript package (i.e jQuery) is expected. Configuration is absent.
MiniTree includes a javascript component to handle the view on the client side as well as code for the server side.
Prerequisites
Some preparation for the usage is required:
- an additional database table storing the tree structure
- a legend method in the model
- a call to MiniTree to initialize the tree structure
- calls in the model to miniTree during creation, update and deletion of an item
# ./app/models/<model>.rb
class <model> < ApplicationRecord
def legend = "#{name} #{id}" # an example
end
# ./app/models/<model>_tree.rb
class <model>Tree < ApplicationRecord
include MiniTree::Utils
end
# ./db/migrate/<nnn>_create_<model>_trees.rb
class Create<Model>Trees < ActiveRecord::Migration[8.0]
def change
create_table :<model>_trees do |t|
t.string :legend
t.integer :parent_id, index: true
t.integer :position, null: false, default: 0
t.boolean :collapsed, default: false
t.string :kind
t.timestamps
end
# add_foreign_key :items, :items, column: :parent_id
end
endYou may specify your view of an item in the treeview:
# ./app/views/application/_mini_tree_title.html.erb
# id and legend are defined
<%= link_to "action", edit_<model>(id:) %>
<%= legend %>Refresh
<Model>Tree.refresh
<Model>Tree.create_item(<id>, <legend>)
<Model>Tree.destroy_item(<id>)
<Model>Tree.update_item(<id>, <legend>)Usage
# Example
<% list = <Model>Tree.all %>
<%= render "mini_trees/index", locals: {list:} %>Installation
As usual:
# Gemfile
...
gem "mini_tree"
...and run "bundle install".
Furthermore, copy manually app/javascript/controllers/tree_controller.js from the gem mini_tree into your own app/javascript/controllers/ directory.
System dependencies
This software has been developed and tested with:
- Ubuntu 24.04
- Ruby 3.4.7
- Rails 8.1.1
See also:
- ./.github/workflows/rake.yml
No particular system dependency is known, i.e. mini_tree is expected to run on other systems without trouble.
Curious
There are quite a lot of TreeViews available. If you are curious you may search in particular for:
- jqTree
- sortableJS
License
Copyright (c) 2025 Dittmar Krall (www.matiq.com), released as open source under the terms of the MIT license.