fix(collection): Add collection name

Fixes #25
This commit is contained in:
Pixelastic 2016-07-08 17:56:06 +02:00
parent dcfb1529ea
commit abc54e11cc
2 changed files with 97 additions and 1 deletions

View File

@ -40,7 +40,7 @@ class AlgoliaSearchRecordExtractor
# In Jekyll v3, Post are actually a specific type of Documents
if type == 'document'
collection_name = @file.collection.instance_variable_get('@label')
collection_name = @file.collection.label
return 'post' if collection_name == 'posts'
end
@ -108,6 +108,18 @@ class AlgoliaSearchRecordExtractor
@file.date.to_time.to_i
end
##
# Get the collection name of a document
def collection
return nil unless @file.respond_to?(:collection)
collection_name = @file.collection.label
# In Jekyll v3, posts are actually a collection
return nil if collection_name == 'posts'
collection_name
end
##
# Get a hash of all front-matter data
def front_matter
@ -153,8 +165,13 @@ class AlgoliaSearchRecordExtractor
title: title,
slug: slug,
date: date,
collection: collection,
tags: tags
}
# Remove empty attributes
shared_attributes = shared_attributes.delete_if do |_, value|
value.nil?
end
# Enriching with page metadata
items = []

View File

@ -264,6 +264,41 @@ describe(AlgoliaSearchRecordExtractor) do
end
end
describe 'collection' do
it 'should get the collection name for documents' do
# Given
input = fixture_document
# When
actual = input.collection
# Then
expect(actual).to eq 'my-collection'
end
it 'should be nil for pages' do
# Given
input = fixture_page
# When
actual = input.collection
# Then
expect(actual).to eq nil
end
it 'should be nil for posts' do
# Given
input = fixture_post
# When
actual = input.collection
# Then
expect(actual).to eq nil
end
end
describe 'front_matter' do
it 'should get a hash of all front matter data' do
# Given
@ -411,6 +446,50 @@ describe(AlgoliaSearchRecordExtractor) do
expect(actual[0][:weight][:heading]).to eq 90
expect(actual[0][:weight][:position]).to eq 0
end
it 'should not contain a collection key for pages' do
# Given
input = fixture_page
# When
actual = input.extract
# Then
expect(actual[0]).not_to have_key(:collection)
end
it 'should not contain a collection key for posts' do
# Given
input = fixture_post
# When
actual = input.extract
# Then
expect(actual[0]).not_to have_key(:collection)
end
it 'should contain the collection name for documents' do
# Given
page = fixture_document
# When
page_data = page.extract
# Then
expect(page_data[0][:collection]).to eq 'my-collection'
end
it 'should not contain a date key for pages' do
# Given
input = fixture_page
# When
actual = input.extract
# Then
expect(actual[0]).not_to have_key(:date)
end
end
describe 'custom_hook_each' do