Class: Orthoses::Ancestry

Inherits:
Object
  • Object
show all
Defined in:
lib/orthoses/ancestry.rb,
lib/orthoses/ancestry/version.rb

Constant Summary collapse

VERSION =
'0.1.0'

Instance Method Summary collapse

Constructor Details

#initialize(loader) ⇒ Ancestry

Returns a new instance of Ancestry.



7
8
9
# File 'lib/orthoses/ancestry.rb', line 7

def initialize(loader)
  @loader = loader
end

Instance Method Details

#callObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/orthoses/ancestry.rb', line 11

def call
  tracer = CallTracer::Lazy.new
  store = tracer.trace('Ancestry::HasAncestry#has_ancestry') do
    @loader.call
  end

  tracer.captures.each do |capture|
    base_name = Utils.module_name(capture.method.receiver) or next
    rel = "#{base_name}::ActiveRecord_Relation"

    options = capture.argument[:options] || {}
    ancestry_format = options[:ancestry_format] || ::Ancestry.default_ancestry_format

    has_ancestry_sig = 'extend Ancestry::HasAncestry'
    unless store['ActiveRecord::Base'].body.include?(has_ancestry_sig)
      store['ActiveRecord::Base'] << has_ancestry_sig
    end

    store[base_name] << "include Ancestry::InstanceMethods[#{base_name}, #{rel}, Integer]"
    store[base_name] << "extend Ancestry::ClassMethods[#{base_name}, #{rel}, Integer]"

    if ancestry_format == :materialized_path2
      store[base_name] << "extend Ancestry::MaterializedPath2[#{base_name}, #{rel}, Integer]"
      store[base_name] << 'include Ancestry::MaterializedPathPg'
      store[base_name] << "include Ancestry::MaterializedPath::InstanceMethods[#{base_name}, Integer]"
      store[base_name] << 'include Ancestry::MaterializedPath2::InstanceMethods[Integer]'

      store[rel] << "include Ancestry::ClassMethods[#{base_name}, #{rel}, Integer]"
      store[rel] << "include Ancestry::MaterializedPath2[#{base_name}, #{rel}, Integer]"
    else
      store[base_name] << "extend Ancestry::MaterializedPath[#{base_name}, #{rel}, Integer]"
      store[base_name] << 'include Ancestry::MaterializedPathPg'
      store[base_name] << "include Ancestry::MaterializedPath::InstanceMethods[#{base_name}, Integer]"

      store[rel] << "include Ancestry::ClassMethods[#{base_name}, #{rel}, Integer]"
      store[rel] << "include Ancestry::MaterializedPath[#{base_name}, #{rel}, Integer]"
    end

    store[base_name] << 'def self.ancestry_column: () -> Symbol'
    store[base_name] << 'def self.ancestry_delimiter: () -> String'
    store[base_name] << "def self.ancestry_base_class: () -> singleton(#{base_name})"

    store[base_name] << 'def self.touch_ancestors: () -> bool'
    store[base_name] << 'def touch_ancestors: () -> bool'
    store[base_name] << 'def self.touch_ancestors=: (bool val) -> bool'
    store[base_name] << 'def touch_ancestors=: (bool val) -> bool'

    if options[:cache_depth] && options[:cache_depth] != :virtual
      store[base_name] << 'def self.depth_cache_column: () -> String'
      store[base_name] << 'def depth_cache_column: () -> String'
      store[base_name] << 'def self.depth_cache_column=: (String val) -> String'
      store[base_name] << 'def depth_cache_column=: (String val) -> String'
    end

    if options[:counter_cache]
      store[base_name] << 'def self.counter_cache_column: () -> String'
      store[base_name] << 'def counter_cache_column: () -> String'
      store[base_name] << 'def self.counter_cache_column=: (String val) -> String'
      store[base_name] << 'def counter_cache_column=: (String val) -> String'
    end

    orphan_strategy = options[:orphan_strategy] || :destroy
    unless orphan_strategy.to_s == 'none'
      store[base_name] << "alias apply_orphan_strategy apply_orphan_strategy_#{orphan_strategy}"
    end
  end

  store
end