zorab47 / active_admin-sortable_tree

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

Fix collection sort when not be an integer #83

Closed lucasVoboril closed 4 months ago

lucasVoboril commented 4 years ago

Problem

When we use ancestry gem, this gem use ancestry field as string, so #sort_by method trouble problems when compare strings with ids

Solution

I'm using this sortable configuration

  sortable \
    tree: true,
    sorting_attribute: :ancestry,
    parent_method: :parent,
    children_method: :children,
    roots_method: :roots,
    roots_collection: proc { collection.where(ancestry: nil) }

And I change this line. Instead of

      def build(page_presenter, collection)
        # ...
        @collection = @collection.sort_by do |a|
          a.send(options[:sorting_attribute]) || 1
        end
        # ...
      end

I fix this problem with #to_i

      def build(page_presenter, collection)
        # ...
        @collection = @collection.sort_by do |a|
          a.send(options[:sorting_attribute].to_i) || 1
        end
        # ...
      end
zorab47 commented 4 years ago

Could your model expose an integer version of the attribute; then use that as the sorting attribute?

lucasVoboril commented 4 years ago

:smile: Yes, I think we can do that!
Sincerely, it hadn't occurred to me to do it that way Thanks