Skip to content

Commit

Permalink
Fix nav tags for creating child pages
Browse files Browse the repository at this point in the history
  • Loading branch information
avonderluft committed Sep 7, 2023
1 parent 64be90b commit a5fd059
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
25 changes: 13 additions & 12 deletions lib/occams/content/tags/children.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
43 changes: 23 additions & 20 deletions lib/occams/content/tags/siblings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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> &laquo;&nbsp;<em>Previous</em> &bull; "
prevp = true
elsif sib_idx == page_idx + 1
@links += '&bull;' unless prevp
@links += "<em>Next</em>&nbsp;&raquo; <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> &laquo;&nbsp;<em>Previous</em> &bull; "
prevp = true
elsif sib_idx == @sibs.index(context) + 1
@links += '&bull;' unless prevp
@links += "<em>Next</em>&nbsp;&raquo; <a href=#{sib.url(relative: true)}>#{sib.label}</a>"
end
end
@links += '</div>'
end
@links += '</div>'
end

def content
format("#{@style}#{@links}")
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/occams/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Occams
VERSION = '1.0.6'
VERSION = '1.0.6.1'
end
8 changes: 8 additions & 0 deletions test/lib/content/tags/siblings_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit a5fd059

Please sign in to comment.