Class: Nesta::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/nesta-plugin-subpages/init.rb

Instance Method Summary collapse

Instance Method Details

#has_subpages?Boolean

Returns true if the page is an index page AND there are any subpages, false otherwise.

Returns:

  • (Boolean)


100
101
102
103
# File 'lib/nesta-plugin-subpages/init.rb', line 100

def has_subpages?
  return false unless self.index_page?
  return self.subpages.empty? ? false : true
end

#subpages(opts = {}) ⇒ Object

This method grabs any pages that are siblings of an index page and any index pages in immediate subdirectories. If the page is NOT an index page or there are NO subpages, an empty array is returned.

The argument is a hash that defines two keys:

:do_sort         -- sort the results by page title.
:include_subdirs -- include index pages in immediate subdirectories.

If given a block, the method will yield each page to it and method will return nil. Pages are still cached in @subpages however.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/nesta-plugin-subpages/init.rb', line 38

def subpages(opts={})
  return [] unless self.index_page?
  return @subpages if @subpages

  options = {
    :do_sort         => true,
    :include_subdirs => true
  }.update(opts)

  pth   = File.dirname(self.filename)
  mpth  = Page.model_path   # Just cacheing these two for
  fmts  = FORMATS.join('|') # use below.
  pages = []

  Dir.foreach(pth) do |entry|
    # Skip the dot-dirs and any index page in the current
    # directory.
    next if entry == '.' or entry == '..' or entry.match(/^index/)

    entry = File.join(pth, entry)

    if File.directory?(entry)
      # If we are allowed to include the sub-directory
      # index pages, do so. If no index page is detected, 
      # move on to the next entry.
      if options[:include_subdirs]
        idxpth = File.join(entry, 'index.%s')
        next unless FORMATS.detect {|fmt| File.exists?(idxpth % fmt)}
      else
        next
      end
    else
      # Ignore any file that isn't a page.
      next unless entry.match(/\.(#{fmts})$/)
    end

    entry = entry.sub("#{mpth}/", '').sub(/\.(#{fmts})/, '')
    page  = Page.find_by_path(entry)

    # If we have a page and we haven't been asked skip 
    # this page, add it to the list.
    if page and not page.('skip subpage')
      pages.push(page)
    end
  end

  @subpages = pages

  if options[:do_sort]
    @subpages.sort! {|n, m| n.title.downcase <=> m.title.downcase}
  end

  if block_given?
    @subpages.each {|page| yield page}
    return nil
  end

  return @subpages
end