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.3.24 app/models/concerns/redde/tree_sortable.rb
redde-0.3.23 app/models/concerns/redde/tree_sortable.rb
redde-0.3.22 app/models/concerns/redde/tree_sortable.rb
redde-0.3.21 app/models/concerns/redde/tree_sortable.rb
redde-0.3.20 app/models/concerns/redde/tree_sortable.rb
redde-0.3.19 app/models/concerns/redde/tree_sortable.rb
redde-0.3.18 app/models/concerns/redde/tree_sortable.rb
redde-0.3.17 app/models/concerns/redde/tree_sortable.rb
redde-0.3.16 app/models/concerns/redde/tree_sortable.rb
redde-0.3.15 app/models/concerns/redde/tree_sortable.rb
redde-0.3.14 app/models/concerns/redde/tree_sortable.rb
redde-0.3.13 app/models/concerns/redde/tree_sortable.rb
redde-0.3.12 app/models/concerns/redde/tree_sortable.rb
redde-0.3.11 app/models/concerns/redde/tree_sortable.rb
redde-0.3.10 app/models/concerns/redde/tree_sortable.rb
redde-0.3.9 app/models/concerns/redde/tree_sortable.rb
redde-0.3.8 app/models/concerns/redde/tree_sortable.rb
redde-0.3.7 app/models/concerns/redde/tree_sortable.rb
redde-0.3.6 app/models/concerns/redde/tree_sortable.rb
redde-0.3.5 app/models/concerns/redde/tree_sortable.rb