Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Extended by:
Graph::Vertex
Includes:
Adapters::Getters, Graph::Vertex, Hooker
Defined in:
lib/kantox/split.rb

Class Method Summary collapse

Class Method Details

.to_h(levels = 0, collected = []) ⇒ Object



48
49
50
51
52
53
# File 'lib/kantox/split.rb', line 48

def to_h levels = 0, collected = []
  { self: attribute_names.map(&:to_sym) }.merge(vertices.map do |k, v|
    v_deep = Kantox::Split::Utils.omnivorous_to_h v, levels, collected
    [k, v == v_deep ? v : { v => v_deep }]
  end.to_h)
end

.verticesObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/kantox/split.rb', line 34

def vertices
  edges.map do |edge|
    next [edge.name, ::ActiveRecord::Base] if edge.options[:polymorphic]

    while edge && edge.options[:through] do
      edge = edges.detect { |e| e.name == edge.options[:through] }
    end

    edge_singular = (edge.options[:class_name] || edge.name.to_s).singularize.camelize
    edge_singular += 's' if edge_singular[-2..-1] == 'es' # WTF, Rails?
    vtx = edge_singular.constantize rescue edge_singular
    edge && [edge.name, edge.macro == :has_many ? [vtx] : vtx]
  end.compact.to_h
end