Skip to content

Commit

Permalink
Resource: switch from default proc to preemptive defaults (#359)
Browse files Browse the repository at this point in the history
* Resource: switch from default proc to preemptive defaults

* fix cop

* Fix additional pagination slash bug
  • Loading branch information
jaredcwhite authored Aug 4, 2021
1 parent 4e8faee commit 3b9e0a5
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 42 deletions.
22 changes: 11 additions & 11 deletions bridgetown-core/lib/bridgetown-core/frontmatter_defaults.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# frozen_string_literal: true

module Bridgetown
# This class handles custom defaults for YAML frontmatter settings.
# These are set in bridgetown.config.yml and apply both to internal use (e.g. layout)
# and the data available to liquid.
#
# This class handles custom defaults for YAML frontmatter variables.
# It is exposed via the frontmatter_defaults method on the site class.
# TODO: needs simplification/refactoring.
class FrontmatterDefaults
# Initializes a new instance.
# @return [Bridgetown::Site]
attr_reader :site

def initialize(site)
@site = site
end
Expand Down Expand Up @@ -45,7 +45,7 @@ def ensure_time!(set)
set
end

# Finds a default value for a given setting, filtered by path and type
# TODO: deprecated. See `all` method instead
#
# path - the path (relative to the source) of the page or
# post the default is used in
Expand Down Expand Up @@ -96,8 +96,8 @@ def all(path, type)
private

def merge_data_cascade_for_path(path, merged_data)
absolute_path = @site.in_source_dir(path)
@site.defaults_reader.path_defaults
absolute_path = site.in_source_dir(path)
site.defaults_reader.path_defaults
.select { |k, _v| absolute_path.include? k }
.sort_by { |k, _v| k.length }
.each do |defaults|
Expand Down Expand Up @@ -130,7 +130,7 @@ def applies_path?(scope, path)
end

def glob_scope(sanitized_path, rel_scope_path)
site_source = Pathname.new(@site.source)
site_source = Pathname.new(site.source)
abs_scope_path = site_source.join(rel_scope_path).to_s

glob_cache(abs_scope_path).each do |scope_path|
Expand All @@ -152,7 +152,7 @@ def path_is_subpath?(path, parent_path)
end

def strip_collections_dir(path)
collections_dir = @site.config["collections_dir"]
collections_dir = site.config["collections_dir"]
slashed_coll_dir = collections_dir.empty? ? "/" : "#{collections_dir}/"
return path if collections_dir.empty? || !path.to_s.start_with?(slashed_coll_dir)

Expand Down Expand Up @@ -226,7 +226,7 @@ def matching_sets(path, type)
#
# Returns an array of hashes
def valid_sets
sets = @site.config["defaults"]
sets = site.config["defaults"]
return [] unless sets.is_a?(Array)

sets.map do |set|
Expand Down
31 changes: 16 additions & 15 deletions bridgetown-core/lib/bridgetown-core/resource/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ class Base
def initialize(model:)
@model = model
@site = model.site
self.data = HashWithDotAccess::Hash.new
@data = front_matter_defaults

trigger_hooks(:post_init)
trigger_hooks :post_init
end

# Collection associated with this resource
Expand Down Expand Up @@ -75,20 +75,21 @@ def relations
@relations ||= Bridgetown::Resource::Relations.new(self)
end

# Loads in any default front matter associated with the resource.
#
# @return [HashWithDotAccess::Hash]
def front_matter_defaults
site.frontmatter_defaults.all(
relative_path.to_s,
collection.label.to_sym
).with_dot_access
end

# Merges new data into the existing data hash.
#
# @param new_data [HashWithDotAccess::Hash]
def data=(new_data)
unless new_data.is_a?(HashWithDotAccess::Hash)
raise "#{self.class} data should be of type HashWithDotAccess::Hash"
end

@data = new_data
@data.default_proc = proc do |_, key|
site.frontmatter_defaults.find(
relative_path.to_s,
collection.label.to_sym,
key.to_s
)
end
@data = @data.merge(new_data)
end

# @return [Bridgetown::Resource::Base]
Expand All @@ -107,7 +108,7 @@ def read!

@destination = Destination.new(self) if requires_destination?

trigger_hooks(:post_read)
trigger_hooks :post_read

self
end
Expand Down
1 change: 1 addition & 0 deletions bridgetown-core/test/resources/src/_posts/_defaults.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
defaults_are: present and accounted for
5 changes: 5 additions & 0 deletions bridgetown-core/test/test_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ class TestResource < BridgetownUnitTest
should "include content" do
assert_equal "<p>W00t!</p>\n", @resource.content
end

should "properly load front matter defaults" do
assert_equal "present and accounted for", @resource.data.defaults_are
assert_equal "present and accounted for", {}.merge(@resource.data.to_h)["defaults_are"]
end
end

context "a resource in the posts collection" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def paginate(template, config, site_title, documents_payload)
template.dir
end
first_index_page_url = Utils.ensure_trailing_slash(
Utils.remove_leading_slash(first_index_page_url)
Utils.remove_double_slash(first_index_page_url)
)
paginated_page_url = File.join(first_index_page_url, paginated_page_url)

Expand Down
26 changes: 11 additions & 15 deletions bridgetown-paginate/lib/bridgetown-paginate/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ def self.format_page_title(to_format, title, cur_page_nr = nil, total_page_count
# If the input already has a dot in position zero, it will be
# returned unchanged.
#
# path - a String path
#
# Returns the path with a leading slash
# @param path [String]
# @return [String] the path with a leading slash
def self.ensure_leading_dot(path)
path[0..0] == "." ? path : ".#{path}"
end
Expand All @@ -47,29 +46,26 @@ def self.ensure_leading_dot(path)
# If the input already has a forward slash in position zero, it will be
# returned unchanged.
#
# path - a String path
#
# Returns the path with a leading slash
# @param path [String]
# @return [String] the path with a leading slash
def self.ensure_leading_slash(path)
path[0..0] == "/" ? path : "/#{path}"
end

# Static: Return a String version of the input without a leading slash.
# Static: Return a String version of the input with only one leading slash.
#
# path - a String path
#
# Returns the input without the leading slash
def self.remove_leading_slash(path)
path[0..0] == "/" ? path[1..-1] : path
# @param path [String]
# @return [String] the input without the leading slash
def self.remove_double_slash(path)
path[0..1] == "//" ? path[1..-1] : path
end

# Static: Return a String version of the input which has a trailing slash.
# If the input already has a forward slash at the end, it will be
# returned unchanged.
#
# path - a String path
#
# Returns the path with a trailing slash
# @param path [String]
# @return [String] the path with a trailing slash
def self.ensure_trailing_slash(path)
path[-1] == "/" ? path : "#{path}/"
end
Expand Down

0 comments on commit 3b9e0a5

Please sign in to comment.