Sha256: 9842d7ee7f18e890042ae0d217a5af0a06c6f191b5438374bda0f379aad48ff7

Contents?: true

Size: 945 Bytes

Versions: 9

Compression:

Stored size: 945 Bytes

Contents

module ClosureTree
  module Digraphs
    extend ActiveSupport::Concern

    def to_dot_digraph
      self.class.to_dot_digraph(self_and_descendants)
    end

    # override this method in your model class if you want a different digraph label.
    def to_digraph_label
      _ct.has_name? ? read_attribute(_ct.name_column) : to_s
    end

    module ClassMethods
      # Renders the given scope as a DOT digraph, suitable for rendering by Graphviz
      def to_dot_digraph(tree_scope)
        id_to_instance = tree_scope.reduce({}) { |h, ea| h[ea.id] = ea; h }
        output = StringIO.new
        output << "digraph G {\n"
        tree_scope.each do |ea|
          if id_to_instance.key? ea._ct_parent_id
            output << "  #{ea._ct_parent_id} -> #{ea._ct_id}\n"
          end
          output << "  #{ea._ct_id} [label=\"#{ea.to_digraph_label}\"]\n"
        end
        output << "}\n"
        output.string
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
closure_tree-5.2.0 lib/closure_tree/digraphs.rb
closure_tree-5.1.1 lib/closure_tree/digraphs.rb
closure_tree-5.1.0 lib/closure_tree/digraphs.rb
closure_tree-5.0.0 lib/closure_tree/digraphs.rb
closure_tree-4.6.3 lib/closure_tree/digraphs.rb
closure_tree-4.6.2 lib/closure_tree/digraphs.rb
closure_tree-4.6.1 lib/closure_tree/digraphs.rb
closure_tree-4.6.0 lib/closure_tree/digraphs.rb
closure_tree-4.5.0 lib/closure_tree/digraphs.rb