From af553cf99182e96a4a663ad7362548185aaa1952 Mon Sep 17 00:00:00 2001 From: Yuri V Date: Wed, 8 Mar 2023 13:00:25 +0200 Subject: [PATCH] Ability to make sorting with different models --- lib/rails_admin_nestable/helper.rb | 7 ++++--- lib/rails_admin_nestable/nestable.rb | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/rails_admin_nestable/helper.rb b/lib/rails_admin_nestable/helper.rb index 79310f1..1aaf347 100755 --- a/lib/rails_admin_nestable/helper.rb +++ b/lib/rails_admin_nestable/helper.rb @@ -5,11 +5,12 @@ def nested_tree_nodes(tree_nodes = []) tree_nodes.map do |tree_node, sub_tree_nodes| li_classes = 'dd-item dd3-item' - content_tag :li, class: li_classes, :'data-id' => tree_node.id do + content_tag :li, class: li_classes, :'data-id' => tree_node.id, :'data-type' => tree_node.class.name do + + output = content_tag(:div, 'drag', class: 'dd-handle dd3-handle') - output = content_tag :div, 'drag', class: 'dd-handle dd3-handle' output += content_tag :div, class: 'dd3-content' do - content = link_to object_label(tree_node), edit_path(@abstract_model, tree_node.id) + content = link_to object_label(tree_node), edit_path(RailsAdmin::AbstractModel.new(tree_node.class), tree_node.id) content += content_tag :div, action_links(tree_node), class: 'pull-right links' end diff --git a/lib/rails_admin_nestable/nestable.rb b/lib/rails_admin_nestable/nestable.rb index d308d2e..d98393e 100755 --- a/lib/rails_admin_nestable/nestable.rb +++ b/lib/rails_admin_nestable/nestable.rb @@ -32,7 +32,8 @@ class Nestable < Base # Methods def update_tree(tree_nodes, parent_node = nil) tree_nodes.each do |key, value| - model = @abstract_model.model.find(value['id'].to_s) + type_class = value['type'].constantize rescue @abstract_model.model + model = type_class.find(value['id'].to_s) model.parent = parent_node || nil model.send("#{@position_field}=".to_sym, (key.to_i + 1)) if @position_field.present? model.save!(validate: @enable_callback)