Sha256: 413e30a3a4fa0e3ab621b9a5976ad120fef31f131ff71cb8c4768e85689c2d93

Contents?: true

Size: 758 Bytes

Versions: 3

Compression:

Stored size: 758 Bytes

Contents

require 'peoplefinder'

class Peoplefinder::GroupHierarchy
  def initialize(root)
    @root = root
  end

  def to_hash
    return {} unless @root
    export(@root)
  end

  def to_group_id_list
    @root.subtree_ids
  end

private

  def export(group)
    {
      id: group.id,
      name: group.name,
      url: group_path(group),
      children: children(group).map { |g| export(g) }
    }
  end

  def children(group)
    child_map[group.id].sort_by(&:name)
  end

  def group_path(lg)
    Peoplefinder::Engine.routes.url_helpers.group_path(id: lg.to_param)
  end

  def child_map
    @child_map ||= Hash.new { |h, k| h[k] = [] }.tap { |hash|
      Peoplefinder::Group.all.each do |group|
        hash[group.parent_id] << group
      end
    }
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
peoplefinder-0.1.1 app/models/peoplefinder/group_hierarchy.rb
peoplefinder-0.1.0 app/models/peoplefinder/group_hierarchy.rb
peoplefinder-0.0.2 app/models/peoplefinder/group_hierarchy.rb