Skip to content

Commit

Permalink
add method to find existing_child_types, use default_child instead of…
Browse files Browse the repository at this point in the history
… Page in case you want to override it
  • Loading branch information
saturnflyer committed Nov 28, 2010
1 parent afed3a2 commit b7e3124
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
12 changes: 7 additions & 5 deletions app/models/archive_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ class ArchivePage < Page
cattr_accessor :allowed_children
cattr_accessor :single_use_children
@@single_use_children = [ArchiveDayIndexPage, ArchiveMonthIndexPage, ArchiveYearIndexPage, FileNotFoundPage]
@@allowed_children = [Page, *@@single_use_children]
@@allowed_children = [self.new.default_child, *@@single_use_children]

def allowed_children
allowed = @@allowed_children.dup
existing_types = children.all(:select => 'DISTINCT class_name, virtual, title').map(&:class_name)
overlap = allowed.map(&:to_s) & existing_types
overlap = @@allowed_children & (existing_child_types - [default_child])

allowed - overlap.map!(&:constantize)
(@@allowed_children - overlap)
end

def existing_child_types
children(:select => 'DISTINCT class_name', :order => nil).collect{|p| p.class }.uniq
end

description %{
Expand Down
10 changes: 9 additions & 1 deletion spec/models/archive_page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@
its(:single_use_children){ should == [ArchiveDayIndexPage, ArchiveMonthIndexPage, ArchiveYearIndexPage, FileNotFoundPage]}
its(:allowed_children){ should == [Page, *ArchivePage.single_use_children]}

describe '#existing_child_types' do
it 'should return a unique array of classes of the page children' do
archive.existing_child_types.should == archive.children.all(:select => 'DISTINCT class_name').collect{|p| p.class }
end
end

describe '#allowed_children' do
context 'when no children exist' do
subject{ ArchivePage.new }
its(:allowed_children){ should == ArchivePage.allowed_children }
end
it 'should remove any existing single_use_children from the allowed_children' do
archive.allowed_children.should == [Page, FileNotFoundPage]
existing_except_default = archive.existing_child_types - [archive.default_child]
used_allowed_children = ArchivePage.allowed_children & existing_except_default
archive.allowed_children.should == (ArchivePage.allowed_children - used_allowed_children)
end
end
end

0 comments on commit b7e3124

Please sign in to comment.