Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to make sorting with different models #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

yuri-val
Copy link

@yuri-val yuri-val commented Mar 8, 2023

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:

  1. sorting items:
    image
  2. sorting groups:
    image

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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [126/80]

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')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

@@ -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

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/EmptyLinesAroundBlockBody: Extra empty line detected at block body beginning.

@@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/HashSyntax: Use the new Ruby 1.9 hash syntax.
Metrics/LineLength: Line is too long. [111/80]

@@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

@@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RescueModifier: Avoid using rescue in its modifier form.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Metrics/LineLength: Line is too long. [83/80]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants