Skip to content

Commit e0e8342

Browse files
committed
descendants with forced reload
1 parent f3696bb commit e0e8342

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

vendor/engines/your_platform/app/models/page.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def to_param
4040
#
4141
def <<(child)
4242
unless child.in? self.children
43-
if child.in? self.cached_descendants
43+
if child.in? self.descendants(true)
4444
link = DagLink.where(
4545
ancestor_type: 'Page', ancestor_id: self.id,
4646
descendant_type: child.class.name, descendant_id: child.id

vendor/engines/your_platform/app/models/structureable.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,14 @@ def delete_cached_descendants
131131

132132
def cached_descendants
133133
Rails.cache.fetch([self, 'descendants'], expires_in: 1.week) do
134-
descendant_pages(true) if self.respond_to?(:descendant_pages)
135-
descendant_groups(true) if self.respond_to?(:descendant_groups)
136-
descendants
134+
descendants(true)
137135
end
138136
end
137+
138+
def descendants(force_reload = false)
139+
descendant_pages(true) if force_reload && self.respond_to?(:descendant_pages)
140+
descendant_groups(true) if force_reload && self.respond_to?(:descendant_groups)
141+
descendants
142+
end
139143
end
140144
end

vendor/engines/your_platform/spec/models/structureable_spec.rb

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@
3636
end
3737
subject { @node.descendants }
3838
it { should == [] }
39-
describe "after adding child" do
39+
describe 'after adding child' do
4040
before do
4141
@child = create(:page)
4242
@node.child_pages << @child
4343
end
4444
it { should include @child }
4545
end
46-
describe "after adding grandchildren" do
46+
describe 'after adding grandchildren' do
4747
before do
4848
@child = create(:page)
4949
@grandchild = create(:page)
@@ -60,15 +60,15 @@
6060
end
6161
subject { @node.cached_descendants }
6262
it { should == [] }
63-
describe "after adding child" do
63+
describe 'after adding child' do
6464
before do
6565
@node.cached_descendants
6666
@child = create(:page)
6767
@node.child_pages << @child
6868
end
6969
it { should include @child }
7070
end
71-
describe "after adding grandchildren" do
71+
describe 'after adding grandchildren' do
7272
before do
7373
@node.cached_descendants
7474
@child = create(:page)
@@ -78,7 +78,7 @@
7878
end
7979
it { should include @grandchild }
8080
end
81-
describe "after removing grandchildren" do
81+
describe 'after removing grandchildren' do
8282
before do
8383
@child = create(:page)
8484
@grandchild = create(:page)
@@ -89,6 +89,40 @@
8989
end
9090
it { should_not include @grandchild }
9191
end
92+
describe 'after multiple adding and removing' do
93+
before do
94+
@p1 = create(:page)
95+
@p2 = create(:page)
96+
@p3 = create(:page)
97+
@p4 = create(:page)
98+
@p5 = create(:page)
99+
@p6 = create(:page)
100+
@p7 = create(:page)
101+
@node.cached_descendants
102+
@node.child_pages << @p1
103+
@p1.child_pages << @p2
104+
@node.cached_descendants
105+
@p2.child_pages << @p4
106+
@node.cached_descendants
107+
@node.child_pages << @p3
108+
@node.cached_descendants
109+
@node.child_pages << @p5
110+
@p5.child_pages << @p6
111+
@node.cached_descendants
112+
@p2.destroy_dag_links
113+
@node.cached_descendants
114+
@p5.child_pages << @p7
115+
@node.cached_descendants
116+
@p6.destroy_dag_links
117+
end
118+
it { should include @p1 }
119+
it { should_not include @p2 }
120+
it { should include @p3 }
121+
it { should_not include @p4 }
122+
it { should include @p5 }
123+
it { should_not include @p6 }
124+
it { should include @p7 }
125+
end
92126
end
93127
end
94128

0 commit comments

Comments
 (0)