From e0e83422fb1d063534f6ec417d907651b16db4d0 Mon Sep 17 00:00:00 2001 From: Joachim Back Date: Mon, 11 Aug 2014 20:28:34 +0200 Subject: [PATCH] descendants with forced reload --- .../engines/your_platform/app/models/page.rb | 2 +- .../your_platform/app/models/structureable.rb | 10 +++-- .../spec/models/structureable_spec.rb | 44 ++++++++++++++++--- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/vendor/engines/your_platform/app/models/page.rb b/vendor/engines/your_platform/app/models/page.rb index 522e50018..70bb068ca 100644 --- a/vendor/engines/your_platform/app/models/page.rb +++ b/vendor/engines/your_platform/app/models/page.rb @@ -40,7 +40,7 @@ def to_param # def <<(child) unless child.in? self.children - if child.in? self.cached_descendants + if child.in? self.descendants(true) link = DagLink.where( ancestor_type: 'Page', ancestor_id: self.id, descendant_type: child.class.name, descendant_id: child.id diff --git a/vendor/engines/your_platform/app/models/structureable.rb b/vendor/engines/your_platform/app/models/structureable.rb index 6265e3406..2a08466fa 100644 --- a/vendor/engines/your_platform/app/models/structureable.rb +++ b/vendor/engines/your_platform/app/models/structureable.rb @@ -131,10 +131,14 @@ def delete_cached_descendants def cached_descendants Rails.cache.fetch([self, 'descendants'], expires_in: 1.week) do - descendant_pages(true) if self.respond_to?(:descendant_pages) - descendant_groups(true) if self.respond_to?(:descendant_groups) - descendants + descendants(true) end end + + def descendants(force_reload = false) + descendant_pages(true) if force_reload && self.respond_to?(:descendant_pages) + descendant_groups(true) if force_reload && self.respond_to?(:descendant_groups) + descendants + end end end diff --git a/vendor/engines/your_platform/spec/models/structureable_spec.rb b/vendor/engines/your_platform/spec/models/structureable_spec.rb index fc3ffebc9..0622dbab7 100644 --- a/vendor/engines/your_platform/spec/models/structureable_spec.rb +++ b/vendor/engines/your_platform/spec/models/structureable_spec.rb @@ -36,14 +36,14 @@ end subject { @node.descendants } it { should == [] } - describe "after adding child" do + describe 'after adding child' do before do @child = create(:page) @node.child_pages << @child end it { should include @child } end - describe "after adding grandchildren" do + describe 'after adding grandchildren' do before do @child = create(:page) @grandchild = create(:page) @@ -60,7 +60,7 @@ end subject { @node.cached_descendants } it { should == [] } - describe "after adding child" do + describe 'after adding child' do before do @node.cached_descendants @child = create(:page) @@ -68,7 +68,7 @@ end it { should include @child } end - describe "after adding grandchildren" do + describe 'after adding grandchildren' do before do @node.cached_descendants @child = create(:page) @@ -78,7 +78,7 @@ end it { should include @grandchild } end - describe "after removing grandchildren" do + describe 'after removing grandchildren' do before do @child = create(:page) @grandchild = create(:page) @@ -89,6 +89,40 @@ end it { should_not include @grandchild } end + describe 'after multiple adding and removing' do + before do + @p1 = create(:page) + @p2 = create(:page) + @p3 = create(:page) + @p4 = create(:page) + @p5 = create(:page) + @p6 = create(:page) + @p7 = create(:page) + @node.cached_descendants + @node.child_pages << @p1 + @p1.child_pages << @p2 + @node.cached_descendants + @p2.child_pages << @p4 + @node.cached_descendants + @node.child_pages << @p3 + @node.cached_descendants + @node.child_pages << @p5 + @p5.child_pages << @p6 + @node.cached_descendants + @p2.destroy_dag_links + @node.cached_descendants + @p5.child_pages << @p7 + @node.cached_descendants + @p6.destroy_dag_links + end + it { should include @p1 } + it { should_not include @p2 } + it { should include @p3 } + it { should_not include @p4 } + it { should include @p5 } + it { should_not include @p6 } + it { should include @p7 } + end end end