Class: Occams::Content::Tags::Siblings

Inherits:
Occams::Content::Tag show all
Defined in:
lib/occams/content/tags/siblings.rb

Overview

Nav Tag for rendering previous and next sibling links relative to current page

{{ cms:siblings }}
{{ cms:siblings style: "font-style: italic", exclude: "404-page, search-page" }}

To customize your siblings style, add a ‘siblings’ id to your CSS, e.g #siblings

color: #006633;
font-size: 95%;
margin-top: 12px;
font-style: italic;

and/or pass in style overrides with the ‘style’ parameter (see above)

To exclude siblings, list their slugs with the ‘exclude’ parameter as comma-delimited string, e.g. as above - exclude: “404-page, search-page”

style and exclude parameters are optional

Instance Attribute Summary collapse

Attributes inherited from Occams::Content::Tag

#context, #params, #source

Instance Method Summary collapse

Methods inherited from Occams::Content::Tag

#allow_erb?, #nodes, #render

Constructor Details

#initialize(context:, params: [], source: nil) ⇒ Siblings

Returns a new instance of Siblings.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/occams/content/tags/siblings.rb', line 24

def initialize(context:, params: [], source: nil)
  super
  @locals = params.extract_options!
  @style = ''
  @style = "<style>#siblings {#{@locals['style']}}</style>" if @locals['style']
  @exclude = []
  @exclude = @locals['exclude'].split(',') if @locals['exclude']
  @links = ''
  # ActiveRecord_Associations_CollectionProxy
  @sibs = context.self_and_siblings.order(:position).to_ary
  unless Rails.env == 'development'
    @sibs.delete_if { |sib| !sib.is_published }
  end
  @sibs.delete_if { |sib| @exclude.include? sib.slug }
end

Instance Attribute Details

Returns the value of attribute links.



22
23
24
# File 'lib/occams/content/tags/siblings.rb', line 22

def links
  @links
end

#localsObject (readonly)

Returns the value of attribute locals.



21
22
23
# File 'lib/occams/content/tags/siblings.rb', line 21

def locals
  @locals
end

#sibsObject (readonly)

Returns the value of attribute sibs.



21
22
23
# File 'lib/occams/content/tags/siblings.rb', line 21

def sibs
  @sibs
end

#styleObject (readonly)

Returns the value of attribute style.



21
22
23
# File 'lib/occams/content/tags/siblings.rb', line 21

def style
  @style
end

Instance Method Details

#contentObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/occams/content/tags/siblings.rb', line 40

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
      next unless @sibs.index(context) # current page is excluded

      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
  format("#{@style}#{@links}")
end