diff --git a/CHANGELOG.md b/CHANGELOG.md index 8614b355..6e4fb891 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## v1.0.6.1 - 9/5/2023 + +- Fixed siblings and children nav tags which as written had problems creating child pages in the admin panel + ## v1.0.6 - 9/5/2023 - Fixed [cms:siblings Nav tag](https://github.com/avonderluft/occams/wiki/Content-Tags#siblings) to handle edge case when page(s) are excluded by the `exclude` parameter diff --git a/lib/occams/content/tags/children.rb b/lib/occams/content/tags/children.rb index 27f14ed8..71352bca 100644 --- a/lib/occams/content/tags/children.rb +++ b/lib/occams/content/tags/children.rb @@ -16,7 +16,8 @@ # as comma-delimited string, e.g. as above - exclude: "404-page, search-page" class Occams::Content::Tag::Children < Occams::Content::Tag - attr_reader :list, :style, :locals + attr_reader :style, :page_children, :locals + attr_accessor :list def initialize(context:, params: [], source: nil) super @@ -27,20 +28,20 @@ def initialize(context:, params: [], source: nil) @exclude = @locals['exclude'].split(',') if @locals['exclude'] @list = '' # ActiveRecord_Associations_CollectionProxy - page_children = context.children.order(:position).to_ary - page_children.delete_if { |child| @exclude.include? child.slug } - return unless page_children.any? - - @list = '<ul id="children">' - page_children.each do |c| - next if Rails.env == 'production' && !c.is_published - - @list += "<li><a href=#{c.url(relative: true)}>#{c.label}</a></li>" - end - @list += '</ul>' + @page_children = context.children.order(:position).to_ary + @page_children.delete_if { |child| @exclude.include? child.slug } end def content + if @page_children.any? + @list = '<ul id="children">' + @page_children.each do |c| + next if Rails.env == 'production' && !c.is_published + + @list += "<li><a href=#{c.url(relative: true)}>#{c.label}</a></li>" + end + @list += '</ul>' + end format("#{@style}#{@list}") end end diff --git a/lib/occams/content/tags/siblings.rb b/lib/occams/content/tags/siblings.rb index 41b8b6e4..f7e31d57 100644 --- a/lib/occams/content/tags/siblings.rb +++ b/lib/occams/content/tags/siblings.rb @@ -18,7 +18,8 @@ # style and exclude parameters are optional class Occams::Content::Tag::Siblings < Occams::Content::Tag - attr_reader :locals, :style, :links + attr_reader :locals, :style, :sibs + attr_accessor :links def initialize(context:, params: [], source: nil) super @@ -27,29 +28,31 @@ def initialize(context:, params: [], source: nil) @style = "<style>#siblings {#{@locals['style']}}</style>" if @locals['style'] @exclude = [] @exclude = @locals['exclude'].split(',') if @locals['exclude'] - @links = '<div id="siblings">' + @links = '' + # ActiveRecord_Associations_CollectionProxy + @sibs = context.self_and_siblings.order(:position).to_ary + @sibs.delete_if { |sib| @exclude.include? sib.slug } + end - prevp = false - sibs = context.self_and_siblings.sort_by(&:position) - sibs.delete_if { |sib| @exclude.include? sib.slug } - page_idx = sibs.index(context) - sibs.each do |sib| - sib_idx = sibs.index(sib) - next if sibs.index(sib) == page_idx - next if Rails.env == 'production' && !sib.is_published + def content + if @sibs.count > 1 + @links = '<div id="siblings">' + prevp = false + @sibs.each do |sib| + sib_idx = @sibs.index(sib) + next if sib.slug == context.slug + next if Rails.env == 'production' && !sib.is_published - if sib_idx == page_idx - 1 - @links += "<a href=#{sib.url(relative: true)}>#{sib.label}</a> « <em>Previous</em> • " - prevp = true - elsif sib_idx == page_idx + 1 - @links += '•' unless prevp - @links += "<em>Next</em> » <a href=#{sib.url(relative: true)}>#{sib.label}</a>" + if sib_idx == @sibs.index(context) - 1 + @links += "<a href=#{sib.url(relative: true)}>#{sib.label}</a> « <em>Previous</em> • " + prevp = true + elsif sib_idx == @sibs.index(context) + 1 + @links += '•' unless prevp + @links += "<em>Next</em> » <a href=#{sib.url(relative: true)}>#{sib.label}</a>" + end end + @links += '</div>' end - @links += '</div>' - end - - def content format("#{@style}#{@links}") end end diff --git a/lib/occams/version.rb b/lib/occams/version.rb index c30b9cba..8e248076 100644 --- a/lib/occams/version.rb +++ b/lib/occams/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Occams - VERSION = '1.0.6' + VERSION = '1.0.6.1' end diff --git a/test/lib/content/tags/siblings_test.rb b/test/lib/content/tags/siblings_test.rb index 4c0420c9..d3d386ec 100644 --- a/test/lib/content/tags/siblings_test.rb +++ b/test/lib/content/tags/siblings_test.rb @@ -41,6 +41,14 @@ def test_render assert_equal html, tag.render end + def test_render_with_no_siblings + tag = Occams::Content::Tag::Siblings.new( + context: @page, + params: [] + ) + assert_equal '', tag.render + end + def test_render_with_exclusions tag = Occams::Content::Tag::Siblings.new( context: @third,