5
6
7
8
9
10
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/ancestry/has_ancestry.rb', line 5
def has_ancestry(options = {})
unless options.is_a? Hash
raise Ancestry::AncestryException, I18n.t("ancestry.option_must_be_hash")
end
= options.keys - [:ancestry_column, :orphan_strategy, :cache_depth, :depth_cache_column, :touch, :counter_cache, :primary_key_format, :update_strategy, :ancestry_format]
if (key = .first)
raise Ancestry::AncestryException, I18n.t("ancestry.unknown_option", key: key.inspect, value: options[key].inspect)
end
ancestry_format = options[:ancestry_format] || Ancestry.default_ancestry_format
if ![:materialized_path, :materialized_path2].include?(ancestry_format)
raise Ancestry::AncestryException, I18n.t("ancestry.unknown_format", value: ancestry_format)
end
orphan_strategy = options[:orphan_strategy] || :destroy
class_variable_set('@@ancestry_column', options[:ancestry_column] || :ancestry)
cattr_reader :ancestry_column, instance_reader: false
primary_key_format = options[:primary_key_format].presence || Ancestry.default_primary_key_format
class_variable_set('@@ancestry_delimiter', '/')
cattr_reader :ancestry_delimiter, instance_reader: false
class_variable_set('@@ancestry_base_class', self)
cattr_reader :ancestry_base_class, instance_reader: false
cattr_accessor :touch_ancestors
self.touch_ancestors = options[:touch] || false
include Ancestry::InstanceMethods
extend Ancestry::ClassMethods
extend Ancestry::HasAncestry.ancestry_format_module(ancestry_format)
attribute ancestry_column, default: ancestry_root
validates ancestry_column, ancestry_validation_options(primary_key_format)
update_strategy = options[:update_strategy] || Ancestry.default_update_strategy
include Ancestry::MaterializedPathPg
validate :ancestry_exclude_self
validate :ancestry_depth_of_descendants, if: :ancestry_changed?
if update_strategy == :sql
after_update :update_descendants_with_new_ancestry_sql, if: :ancestry_changed?
else
after_update :update_descendants_with_new_ancestry, if: :ancestry_changed?
end
orphan_strategy_helper = "apply_orphan_strategy_#{orphan_strategy}"
if method_defined?(orphan_strategy_helper)
alias_method :apply_orphan_strategy, orphan_strategy_helper
before_destroy :apply_orphan_strategy
elsif orphan_strategy.to_s != "none"
raise Ancestry::AncestryException, I18n.t("ancestry.invalid_orphan_strategy")
end
if options[:cache_depth] == :virtual
depth_cache_sql = options[:depth_cache_column]&.to_s || 'ancestry_depth'
elsif options[:cache_depth]
cattr_accessor :depth_cache_column
self.depth_cache_column =
if options[:cache_depth] == true
options[:depth_cache_column]&.to_s || 'ancestry_depth'
else
options[:cache_depth].to_s
end
if options[:depth_cache_column]
ActiveSupport::Deprecation.warn("has_ancestry :depth_cache_column is deprecated. Use :cache_depth instead.")
end
before_validation :cache_depth
before_save :cache_depth
validates_numericality_of depth_cache_column, :greater_than_or_equal_to => 0, :only_integer => true, :allow_nil => false
depth_cache_sql = depth_cache_column
else
depth_cache_sql = ancestry_depth_sql
end
scope :before_depth, lambda { |depth| where("#{depth_cache_sql} < ?", depth) }
scope :to_depth, lambda { |depth| where("#{depth_cache_sql} <= ?", depth) }
scope :at_depth, lambda { |depth| where("#{depth_cache_sql} = ?", depth) }
scope :from_depth, lambda { |depth| where("#{depth_cache_sql} >= ?", depth) }
scope :after_depth, lambda { |depth| where("#{depth_cache_sql} > ?", depth) }
if options[:counter_cache]
cattr_accessor :counter_cache_column
self.counter_cache_column = options[:counter_cache] == true ? 'children_count' : options[:counter_cache].to_s
after_create :increase_parent_counter_cache, if: :has_parent?
after_destroy :decrease_parent_counter_cache, if: :has_parent?
after_update :update_parent_counter_cache
end
if options[:touch]
after_touch :touch_ancestors_callback
after_destroy :touch_ancestors_callback
after_save :touch_ancestors_callback, if: :saved_changes?
end
end
|