forked from stefankroes/ancestry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks_test.rb
113 lines (95 loc) · 3.76 KB
/
hooks_test.rb
1
2
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
107
108
109
110
111
112
113
require_relative '../environment'
class ArrangementTest < ActiveSupport::TestCase
# https://github.com/stefankroes/ancestry/issues/267
def test_has_ancestry_in_after_save
AncestryTestDatabase.with_model(
:orphan_strategy => :adopt,
:extra_columns => {:name => :string, :name_path => :string}
) do |model|
model.class_eval do
before_save :before_save_hook
def before_save_hook
self.name_path = parent ? "#{parent.name_path}/#{name}" : name
nil
end
end
m1 = model.create!(:name => "parent")
m2 = model.create(:parent => m1, :name => "child")
m3 = model.create(:parent_id => m2.id, :name => "grandchild")
assert_equal("parent", m1.reload.name_path)
assert_equal("parent/child", m2.reload.name_path)
assert_equal("parent/child/grandchild", m3.reload.name_path)
m2.destroy
m3.reload
assert_equal([m1.id, m3.id], m3.path_ids)
assert_equal("parent/grandchild", m3.name_path)
end
end
def test_update_descendants_with_changed_parent_value
return if Ancestry.default_update_strategy == :sql # sql stragery doesn't trigger callbacks
AncestryTestDatabase.with_model(
extra_columns: { name: :string, name_path: :string }
) do |model|
model.class_eval do
before_save :update_name_path
def update_name_path
self.name_path = [parent&.name_path, name].compact.join('/')
end
end
m1 = model.create!( name: "parent" )
m2 = model.create( parent: m1, name: "child" )
m3 = model.create( parent: m2, name: "grandchild" )
m4 = model.create( parent: m3, name: "grandgrandchild" )
assert_equal([m1.id], m2.ancestor_ids)
assert_equal("parent", m1.reload.name_path)
assert_equal("parent/child", m2.reload.name_path)
assert_equal("parent/child/grandchild", m3.reload.name_path)
assert_equal("parent/child/grandchild/grandgrandchild", m4.reload.name_path)
m5 = model.create!( name: "changed" )
m2.update!( parent_id: m5.id )
assert_equal("changed", m5.reload.name_path)
assert_equal([m5.id], m2.reload.ancestor_ids)
assert_equal("changed/child", m2.reload.name_path)
assert_equal([m5.id,m2.id], m3.reload.ancestor_ids)
assert_equal("changed/child/grandchild", m3.reload.name_path)
assert_equal([m5.id,m2.id,m3.id], m4.reload.ancestor_ids)
assert_equal("changed/child/grandchild/grandgrandchild", m4.reload.name_path)
end
end
def test_has_ancestry_detects_changes_in_after_save
AncestryTestDatabase.with_model(:extra_columns => {:name => :string, :name_path => :string}) do |model|
model.class_eval do
after_save :after_hook
attr_reader :modified
def after_hook
@modified = ancestry_changed?
nil
end
end
m1 = model.create!(:name => "parent")
m2 = model.create!(:parent => m1, :name => "child")
m2.parent = nil
m2.save!
assert_equal(model.ancestry_options[:ancestry_format] == :materialized_path2 ? true : false, m1.modified)
assert_equal(true, m2.modified)
end
end
def test_has_ancestry_detects_changes_in_before_save
AncestryTestDatabase.with_model(:extra_columns => {:name => :string, :name_path => :string}) do |model|
model.class_eval do
before_save :before_hook
attr_reader :modified
def before_hook
@modified = ancestry_changed?
nil
end
end
m1 = model.create!(:name => "parent")
m2 = model.create!(:parent => m1, :name => "child")
m2.parent = nil
m2.save!
assert_equal(model.ancestry_options[:ancestry_format] == :materialized_path2 ? true : false, m1.modified)
assert_equal(true, m2.modified)
end
end
end