Sha256: d1be1ddb60b572e3d3b5463676de2318af7c21252a878b6b59d9fc883949eaeb

Contents?: true

Size: 949 Bytes

Versions: 12

Compression:

Stored size: 949 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.inject({}) { |h, ea| h[ea.id] = ea; h }
        output = StringIO.new
        output << "digraph G {\n"
        tree_scope.each do |ea|
          if id_to_instance.has_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

12 entries across 12 versions & 1 rubygems

Version Path
closure_tree-4.4.0 lib/closure_tree/digraphs.rb
closure_tree-4.3.0 lib/closure_tree/digraphs.rb
closure_tree-4.2.9 lib/closure_tree/digraphs.rb
closure_tree-4.2.8 lib/closure_tree/digraphs.rb
closure_tree-4.2.7 lib/closure_tree/digraphs.rb
closure_tree-4.2.6 lib/closure_tree/digraphs.rb
closure_tree-4.2.5 lib/closure_tree/digraphs.rb
closure_tree-4.2.4 lib/closure_tree/digraphs.rb
closure_tree-4.2.3 lib/closure_tree/digraphs.rb
closure_tree-4.2.2 lib/closure_tree/digraphs.rb
closure_tree-4.2.1 lib/closure_tree/digraphs.rb
closure_tree-4.2.0 lib/closure_tree/digraphs.rb