Class: RailsLens::Extensions::ClosureTreeExt

Inherits:
Base
  • Object
show all
Defined in:
lib/rails_lens/extensions/closure_tree_ext.rb

Constant Summary

Constants inherited from Base

Base::INTERFACE_VERSION

Instance Attribute Summary

Attributes inherited from Base

#model_class

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

compatible?, gem_available?, #initialize, interface_version, #safe_method_call, #validate_array_result, #validate_hash_result

Constructor Details

This class inherits a constructor from RailsLens::Extensions::Base

Class Method Details

.detect?Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
# File 'lib/rails_lens/extensions/closure_tree_ext.rb', line 10

def self.detect?
  if gem_available?(gem_name)
    require gem_name
    true
  else
    false
  end
end

.gem_nameObject



6
7
8
# File 'lib/rails_lens/extensions/closure_tree_ext.rb', line 6

def self.gem_name
  'closure_tree'
end

Instance Method Details

#annotateObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rails_lens/extensions/closure_tree_ext.rb', line 19

def annotate
  return nil unless model_uses_closure_tree?

  lines = []
  lines << '[closure_tree]'
  lines << "parent_column = \"#{parent_column_name}\""
  lines << "hierarchy_table = \"#{hierarchy_table_name}\""

  lines << "order_column = \"#{order_column}\"" if order_column

  lines << "depth_column = \"#{depth_column}\"" if depth_column && has_column?(depth_column)

  lines.join("\n")
end

#erd_additionsObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rails_lens/extensions/closure_tree_ext.rb', line 68

def erd_additions
  return default_erd_additions unless model_uses_closure_tree?

  {
    relationships: [
      {
        type: 'hierarchy',
        from: table_name,
        to: hierarchy_table_name,
        label: 'closure table',
        style: 'dashed'
      }
    ],
    badges: ['tree'],
    attributes: {
      tree_type: 'closure_tree',
      hierarchy_table: hierarchy_table_name
    }
  }
end

#notesObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rails_lens/extensions/closure_tree_ext.rb', line 34

def notes
  return [] unless model_uses_closure_tree?

  notes = []

  # Check parent column index
  notes << NoteCodes.note(parent_column_name, NoteCodes::INDEX) unless has_index?(parent_column_name)

  # Check hierarchy table existence and indexes
  if hierarchy_table_exists?
    unless has_hierarchy_indexes?
      notes << NoteCodes.note(hierarchy_table_name, NoteCodes::COMP_INDEX)
    end

    unless has_hierarchy_depth_index?
      notes << NoteCodes.note('generations', NoteCodes::INDEX)
    end
  else
    notes << NoteCodes.note(hierarchy_table_name, NoteCodes::MISSING)
  end

  # Check for counter cache
  if should_have_counter_cache? && !has_counter_cache?
    notes << NoteCodes.note('children', NoteCodes::COUNTER_CACHE)
  end

  # Check depth column
  if model_class.respond_to?(:cache_depth?) && model_class.cache_depth? && !has_column?(depth_column)
    notes << NoteCodes.note(depth_column, NoteCodes::DEPTH_CACHE)
  end

  notes
end