Skip to content

Commit

Permalink
descendants with forced reload
Browse files Browse the repository at this point in the history
  • Loading branch information
jbx26 committed Aug 11, 2014
1 parent f3696bb commit e0e8342
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
2 changes: 1 addition & 1 deletion vendor/engines/your_platform/app/models/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions vendor/engines/your_platform/app/models/structureable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
44 changes: 39 additions & 5 deletions vendor/engines/your_platform/spec/models/structureable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -60,15 +60,15 @@
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)
@node.child_pages << @child
end
it { should include @child }
end
describe "after adding grandchildren" do
describe 'after adding grandchildren' do
before do
@node.cached_descendants
@child = create(:page)
Expand All @@ -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)
Expand All @@ -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

0 comments on commit e0e8342

Please sign in to comment.