Class: AutoAdmin::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/auto_admin/node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Node

Returns a new instance of Node.



43
44
45
# File 'lib/auto_admin/node.rb', line 43

def initialize(klass)
  self.klass = klass
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass.



40
41
42
# File 'lib/auto_admin/node.rb', line 40

def klass
  @klass
end

Class Method Details

.nodesObject



4
5
6
7
8
9
10
11
12
13
# File 'lib/auto_admin/node.rb', line 4

def nodes
  @nodes ||= begin
    Rails.application.eager_load!
    ActiveRecord::Base.descendants.each_with_object({}) do |klass, hash|
      next if klass.abstract_class? || klass.name.split('::').first == 'ActiveRecord'

      hash[klass.name] = AutoAdmin::Node.new(klass)
    end
  end
end

.preload_associations!Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/auto_admin/node.rb', line 15

def preload_associations! # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  return true if @preloading

  @preloading = true
  @preload_associations ||= begin
    nodes.values.each do |node|
      node.klass.reflect_on_all_associations(:has_many).each do |association|
        node.has_many association unless association.options[:through]
      end
      node.klass.reflect_on_all_associations(:has_one).each do |association|
        node.has_one association
      end
      node.klass.reflect_on_all_associations(:belongs_to).each do |association|
        node.belongs_to association unless association.polymorphic?
      end
    end
    true
  end
end

.root_nodesObject



35
36
37
# File 'lib/auto_admin/node.rb', line 35

def root_nodes
  nodes.values.select { |n| n.belongs_to.none? }.sort_by { |n| -n.weight }
end

Instance Method Details

#attributesObject



63
64
65
# File 'lib/auto_admin/node.rb', line 63

def attributes
  klass.column_names - primary_keys - belongs_to.map(&:node).map { |hm| hm.to_s.foreign_key }
end

#belongs_to(association = nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/auto_admin/node.rb', line 99

def belongs_to(association = nil)
  @belongs_to ||= Set.new
  if association
    node = self.class.nodes[association.klass.name]
    @belongs_to << Association.new(node, association.name)
  else
    self.class.preload_associations!
  end
  @belongs_to
end

#define_routes!(ctx) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/auto_admin/node.rb', line 47

def define_routes!(ctx)
  ctx.resources resource_name, controller: 'resources', defaults: { node: klass.name }, only: %i(index show), shallow: true do
    has_many.map(&:node).each do |child_node|
      child_node.define_routes!(ctx)
    end
  end
end

#has_many(association = nil) ⇒ Object

rubocop:disable Metrics/AbcSize



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/auto_admin/node.rb', line 75

def has_many(association = nil) # rubocop:disable Metrics/AbcSize
  @has_many ||= Set.new
  if association
    node = self.class.nodes[association.klass.name]
    node.belongs_to << Association.new(self, association.inverse_of) if association.inverse_of
    @has_many << Association.new(node, association.name)
  else
    self.class.preload_associations!
  end
  @has_many
end

#has_one(association = nil) ⇒ Object

rubocop:disable Metrics/AbcSize



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/auto_admin/node.rb', line 87

def has_one(association = nil) # rubocop:disable Metrics/AbcSize
  @has_one ||= Set.new
  if association
    node = self.class.nodes[association.klass.name]
    node.belongs_to << Association.new(self, association.inverse_of) if association.inverse_of
    @has_one << Association.new(node, association.name)
  else
    self.class.preload_associations!
  end
  @has_one
end

#polymorphic_pathObject



59
60
61
# File 'lib/auto_admin/node.rb', line 59

def polymorphic_path
  [resource_name.to_sym]
end

#primary_keysObject



67
68
69
# File 'lib/auto_admin/node.rb', line 67

def primary_keys
  klass.respond_to?(:primary_keys) ? klass.primary_keys : [klass.primary_key]
end

#resource_nameObject



55
56
57
# File 'lib/auto_admin/node.rb', line 55

def resource_name
  klass.name.demodulize.underscore.pluralize
end

#weightObject



71
72
73
# File 'lib/auto_admin/node.rb', line 71

def weight
  @weight ||= has_many.map(&:node).sum(&:weight) + 1
end