3
4
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
|
# File 'lib/ancestry/has_ancestry.rb', line 3
def has_ancestry options = {}
raise Ancestry::AncestryException.new(I18n.t("ancestry.option_must_be_hash")) unless options.is_a? Hash
options.each do |key, value|
unless [:ancestry_column, :orphan_strategy, :cache_depth, :depth_cache_column, :touch, :counter_cache, :primary_key_format, :update_strategy, :ancestry_format].include? key
raise Ancestry::AncestryException.new(I18n.t("ancestry.unknown_option", key: key.inspect, value: value.inspect))
end
end
if options[:ancestry_format].present? && ![:materialized_path, :materialized_path2].include?( options[:ancestry_format] )
raise Ancestry::AncestryException.new(I18n.t("ancestry.unknown_format", value: options[:ancestry_format]))
end
cattr_accessor :ancestry_column
self.ancestry_column = options[:ancestry_column] || :ancestry
cattr_accessor :ancestry_primary_key_format
self.ancestry_primary_key_format = options[:primary_key_format].presence || Ancestry.default_primary_key_format
cattr_accessor :ancestry_delimiter
self.ancestry_delimiter = '/'
cattr_accessor :ancestry_base_class
self.ancestry_base_class = self
cattr_accessor :touch_ancestors
self.touch_ancestors = options[:touch] || false
include Ancestry::InstanceMethods
extend Ancestry::ClassMethods
cattr_accessor :ancestry_format
self.ancestry_format = options[:ancestry_format] || Ancestry.default_ancestry_format
if ancestry_format == :materialized_path2
extend Ancestry::MaterializedPath2
else
extend Ancestry::MaterializedPath
end
attribute self.ancestry_column, default: self.ancestry_root
validates self.ancestry_column, ancestry_validation_options
update_strategy = options[:update_strategy] || Ancestry.default_update_strategy
include Ancestry::MaterializedPathPg if update_strategy == :sql
cattr_reader :orphan_strategy
self.orphan_strategy = options[:orphan_strategy] || :destroy
validate :ancestry_exclude_self
after_update :update_descendants_with_new_ancestry
before_destroy :apply_orphan_strategy
if options[:cache_depth]
self.cattr_accessor :depth_cache_column
self.depth_cache_column = options[:depth_cache_column] || :ancestry_depth
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
end
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
{:before_depth => '<', :to_depth => '<=', :at_depth => '=', :from_depth => '>=', :after_depth => '>'}.each do |scope_name, operator|
scope scope_name, lambda { |depth|
raise Ancestry::AncestryException.new(I18n.t("ancestry.named_scope_depth_cache",
:scope_name => scope_name
)) unless options[:cache_depth]
where("#{depth_cache_column} #{operator} ?", depth)
}
end
after_touch :touch_ancestors_callback
after_destroy :touch_ancestors_callback
after_save :touch_ancestors_callback, if: :saved_changes?
end
|