From 5cdb86ca129d520dece1e553aec6a91fd62b3ac6 Mon Sep 17 00:00:00 2001 From: octopusinvitro Date: Tue, 11 Apr 2017 16:34:08 +0100 Subject: [PATCH] Add a method to finder to find unpublished documents This method allows the scraper route to send the unpublished documents to the view so that they can be scraped and pushed to the static repo, so that they can be loaded as a preview without being accessible for users or linked from the site --- lib/document/finder.rb | 12 ++++++++++-- tests/document/finder.rb | 12 ++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/document/finder.rb b/lib/document/finder.rb index 509ea644..49618c23 100644 --- a/lib/document/finder.rb +++ b/lib/document/finder.rb @@ -21,11 +21,15 @@ def find_or_empty end def find_all - filenames.map { |filename| create_document(filename) }.select(&:published?) + create_documents.select(&:published?) + end + + def find_previews + create_documents.reject(&:published?) end def find_featured - find_all.select(&:featured?) + create_documents.select(&:featured?) end def multiple? @@ -49,6 +53,10 @@ def raise_error_if_no_files_found raise Document::NoFilesFoundError, message if none? end + def create_documents + @create_documents ||= filenames.map { |filename| create_document(filename) } + end + def filenames @filenames ||= Dir.glob(pattern).sort end diff --git a/tests/document/finder.rb b/tests/document/finder.rb index 56c58c15..5c1e7381 100644 --- a/tests/document/finder.rb +++ b/tests/document/finder.rb @@ -33,6 +33,18 @@ end end + it 'can find unpublished documents' do + published = '--- +published: true +---' + unpublished = '--- +published: false +---' + Dir.stub :glob, [new_tempfile(published), new_tempfile(unpublished)] do + finder.find_previews.count.must_equal(1) + end + end + it 'creates a document with the right url' do contents = '--- slug: a-slug