Sha256: 90e74a559a288d908f830135f37aab6e65ef2a720bf493c368be7708dc016cd1

Contents?: true

Size: 727 Bytes

Versions: 45

Compression:

Stored size: 727 Bytes

Contents

module Redde::TreeSortable
  def sort(list)
    Sorter.new(list, self).sort
  end

  class Sorter
    attr_accessor :list, :klass
    def initialize(list, klass)
      @list = list
      @klass = klass
    end

    def sort
      tree.each do |parent, cats|
        i = 1
        if parent == 'root'
          pid = nil
        else
          pid = klass.find(parent).id
        end
        cats.each do |cat|
          klass.find(cat).update(parent_id: pid, position: i)
          i += 1
        end
      end
    end

    def tree
      temp = {}
      list.each do |key, value|
        if temp[value]
          temp[value] << key
        else
          temp[value] = [key]
        end
      end
      temp
    end
  end
end

Version data entries

45 entries across 45 versions & 1 rubygems

Version Path
redde-0.4.4 app/models/concerns/redde/tree_sortable.rb
redde-0.4.3 app/models/concerns/redde/tree_sortable.rb
redde-0.4.2 app/models/concerns/redde/tree_sortable.rb
redde-0.4.1 app/models/concerns/redde/tree_sortable.rb
redde-0.4.0 app/models/concerns/redde/tree_sortable.rb
redde-0.3.39 app/models/concerns/redde/tree_sortable.rb
redde-0.3.38 app/models/concerns/redde/tree_sortable.rb
redde-0.3.37 app/models/concerns/redde/tree_sortable.rb
redde-0.3.36 app/models/concerns/redde/tree_sortable.rb
redde-0.3.35 app/models/concerns/redde/tree_sortable.rb
redde-0.3.34 app/models/concerns/redde/tree_sortable.rb
redde-0.3.33 app/models/concerns/redde/tree_sortable.rb
redde-0.3.32 app/models/concerns/redde/tree_sortable.rb
redde-0.3.31 app/models/concerns/redde/tree_sortable.rb
redde-0.3.30 app/models/concerns/redde/tree_sortable.rb
redde-0.3.29 app/models/concerns/redde/tree_sortable.rb
redde-0.3.28 app/models/concerns/redde/tree_sortable.rb
redde-0.3.27 app/models/concerns/redde/tree_sortable.rb
redde-0.3.26 app/models/concerns/redde/tree_sortable.rb
redde-0.3.25 app/models/concerns/redde/tree_sortable.rb