Add `type` attribute to Document instances (#7406)

Merge pull request 7406
This commit is contained in:
Ashwin Maroli 2019-05-16 21:14:39 +05:30 committed by jekyllbot
parent db2de73a0d
commit 49ffbbd4c7
4 changed files with 19 additions and 7 deletions

View File

@ -5,7 +5,7 @@ module Jekyll
include Comparable
extend Forwardable
attr_reader :path, :site, :extname, :collection
attr_reader :path, :site, :extname, :collection, :type
attr_accessor :content, :output
def_delegator :self, :read_post_data, :post_read
@ -44,6 +44,8 @@ module Jekyll
@path = path
@extname = File.extname(path)
@collection = relations[:collection]
@type = @collection.label.to_sym
@has_yaml_header = nil
if draft?
@ -53,7 +55,7 @@ module Jekyll
end
data.default_proc = proc do |_, key|
site.frontmatter_defaults.find(relative_path, collection.label, key)
site.frontmatter_defaults.find(relative_path, type, key)
end
trigger_hooks(:post_init)
@ -462,10 +464,7 @@ module Jekyll
end
def merge_defaults
defaults = @site.frontmatter_defaults.all(
relative_path,
collection.label.to_sym
)
defaults = @site.frontmatter_defaults.all(relative_path, type)
merge_data!(defaults, :source => "front matter defaults") unless defaults.empty?
end

View File

@ -10,7 +10,7 @@ module Jekyll
def_delegators :@doc,
:site, :name, :ext, :extname,
:collection, :related_posts,
:collection, :related_posts, :type,
:coffeescript_file?, :yaml_file?,
:url, :next_doc, :previous_doc

View File

@ -58,6 +58,10 @@ class TestDocument < JekyllUnitTest
assert_equal "configuration", @document.basename_without_ext
end
should "know its type" do
assert_equal :methods, @document.type
end
should "know whether it's a YAML file" do
assert_equal false, @document.yaml_file?
end

View File

@ -56,6 +56,15 @@ class TestExcerpt < JekyllUnitTest
end
end
context "#type" do
should "return the post's type" do
assert_equal @excerpt.type, @post.type
end
should "return a symbol" do
assert_same @excerpt.type.class, Symbol
end
end
context "#to_s" do
should "return rendered output" do
assert_equal @excerpt.output, @excerpt.to_s