active_admin-sortable_tree icon indicating copy to clipboard operation
active_admin-sortable_tree copied to clipboard

Fix collection sort when not be an integer

Open lucasVoboril opened this issue 6 years ago • 2 comments

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

lucasVoboril avatar Dec 04 '19 14:12 lucasVoboril

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

zorab47 avatar Mar 11 '20 15:03 zorab47

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

lucasVoboril avatar Mar 11 '20 15:03 lucasVoboril