Don't store pages with no entries

pull/15/head
Thibaut 11 years ago
parent 7c1bd6b236
commit 706270d89c

@ -34,17 +34,18 @@ module Docs
end end
def index_page(id) def index_page(id)
if page = new.build_page(id) if (page = new.build_page(id)) && page[:entries].present?
yield page[:store_path], page[:output] yield page[:store_path], page[:output]
index = EntryIndex.new index = EntryIndex.new
index.add page[:entries] index.add page[:entries]
index
end end
index
end end
def index_pages def index_pages
index = EntryIndex.new index = EntryIndex.new
new.build_pages do |page| new.build_pages do |page|
next if page[:entries].blank?
yield page[:store_path], page[:output] yield page[:store_path], page[:output]
index.add page[:entries] index.add page[:entries]
end end

@ -7,7 +7,7 @@ module Docs
def entries def entries
entries = [] entries = []
entries << default_entry if include_default_entry? entries << default_entry if root_page? || include_default_entry?
entries.concat(additional_entries) entries.concat(additional_entries)
build_entries(entries) build_entries(entries)
end end

@ -129,19 +129,36 @@ class DocsDocTest < MiniTest::Spec
end end
end end
it "yields the page's :store_path and :output" do context "and it has :entries" do
doc.index_page('') { |*args| @args = args } it "yields the page's :store_path and :output" do
assert_equal [page[:store_path], page[:output]], @args doc.index_page('') { |*args| @args = args }
end assert_equal [page[:store_path], page[:output]], @args
end
it "returns an EntryIndex" do it "returns an EntryIndex" do
assert_instance_of Docs::EntryIndex, doc.index_page('') {} assert_instance_of Docs::EntryIndex, doc.index_page('') {}
end
describe "the index" do
it "contains the page's entries" do
index = doc.index_page('') {}
assert_equal page[:entries], index.entries
end
end
end end
describe "the index" do context "and it doesn't have :entries" do
it "contains the page's entries" do before do
index = doc.index_page('') {} page[:entries] = []
assert_equal page[:entries], index.entries end
it "doesn't yield" do
doc.index_page('') { |*_| @yield = true }
refute @yield
end
it "returns nil" do
assert_nil doc.index_page('') {}
end end
end end
end end
@ -184,21 +201,39 @@ class DocsDocTest < MiniTest::Spec
end end
end end
it "yields each page's :store_path and :output" do it "yields pages that have :entries" do
doc.index_pages { |*args| (@args ||= []) << args } doc.index_pages { |*args| (@args ||= []) << args }
assert_equal pages.length, @args.length assert_equal pages.length, @args.length
assert_equal [page[:store_path], page[:output]], @args.first assert_equal [page[:store_path], page[:output]], @args.first
end end
it "returns an EntryIndex" do it "doesn't yield pages that don't have :entries" do
assert_instance_of Docs::EntryIndex, doc.index_pages {} pages.first[:entries] = []
doc.index_pages { |*args| (@args ||= []) << args }
assert_equal pages.length - 1, @args.length
end
describe "and at least one has :entries" do
it "returns an EntryIndex" do
assert_instance_of Docs::EntryIndex, doc.index_pages {}
end
describe "the index" do
it "contains all the pages' entries" do
index = doc.index_pages {}
assert_equal pages.length, index.entries.length
assert_includes index.entries, entry
end
end
end end
describe "the index" do context "and none have :entries" do
it "contains all pages' entries" do before do
index = doc.index_pages {} pages.each { |page| page[:entries] = [] }
assert_equal pages.length, index.entries.length end
assert_includes index.entries, entry
it "returns nil" do
assert_nil doc.index_pages {}
end end
end end
end end

@ -26,7 +26,7 @@ class EntriesFilterTest < MiniTest::Spec
it "includes the default entry when #include_default_entry? is true" do it "includes the default entry when #include_default_entry? is true" do
stub(filter).include_default_entry? { true } stub(filter).include_default_entry? { true }
assert_equal 1, entries.length refute_empty entries
end end
it "doesn't include the default entry when #include_default_entry? is false" do it "doesn't include the default entry when #include_default_entry? is false" do
@ -34,6 +34,12 @@ class EntriesFilterTest < MiniTest::Spec
assert_empty entries assert_empty entries
end end
it "always includes the default entry when #root_page? is true" do
stub(filter).include_default_entry? { false }
stub(filter).root_page? { true }
refute_empty entries
end
describe "the default entry" do describe "the default entry" do
it "has the #name, #path and #type" do it "has the #name, #path and #type" do
assert_equal 'name', entries.first.name assert_equal 'name', entries.first.name

Loading…
Cancel
Save