rails_admin_nestable
rails_admin_nestable copied to clipboard
Ability to make sorting with different models
Example of usage:
I have two models:
-
CrewingTypeDocument
- sort of categories
class CrewingTypeDocument < ApplicationRecord
# position_field: :position
has_one :parent, -> { none }, class_name: 'CrewingTypeDocument'
has_many :children, class_name: 'CrewingDocument'
# ...
end
-
CrewingDocument
- sort of items, belongs to categories
class CrewingDocument < ApplicationRecord
# position_field: :position
belongs_to :parent, class_name: "CrewingTypeDocument"
has_many :children, -> {none}, class_name: 'CrewingDocument'
class << self
def arrange(_options)
CrewingTypeDocument.order(:position).includes(:children).each_with_object({}) do |item, hash|
hash[item] = item.children.order(:position)
end
end
end
end
In such configuration you can have sorting tree in CrewingDocument
model where top level will be CrewingTypeDocument
, second level - CrewingDocument
.
WARNING: if you want to sort groups and items in same form, you should use same position_field
names
RailsAdmin configuration:
RailsAdmin.config do |config|
config.actions do
# ...
nestable
end
config.model 'CrewingDocument' do
# ...
nestable_tree live_update: :only, position_field: :position
end
end
Example:
- sorting items:
- sorting groups: