nesta-plugin-subpages 0.5.0 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -17,10 +17,10 @@ subdirectories. For example:
17
17
  Page.find_by_path('parent').subpages() => [page-1, page-2, subdir-1]
18
18
 
19
19
  The returned array contains the Page instances themselves, not their
20
- string paths.
20
+ string paths, and they will be sorted based on their titles.
21
21
 
22
- Lastly, there is also `#has_subpages?` helper method that returns true
23
- or false if the page has any subpages.
22
+ Next, there is also `#has_subpages?` helper method that returns true or
23
+ false if the page has any subpages.
24
24
 
25
25
  Subpages are cached within an instance variable the first time that
26
26
  `#subpages` is called, but this should not be a problem in most cases.
@@ -28,6 +28,20 @@ Subpages are cached within an instance variable the first time that
28
28
  Also, there is a helper defined called `subpages_for` that takes either
29
29
  a Page instance or a string path and returns the same array.
30
30
 
31
+ Lastly, if for some reason you do *not* want a subpage listed, merely
32
+ give it the metadata tag of `skip subpage: true` and it will indeed be
33
+ skipped in the list.
34
+
35
+ There are two options that you can pass to `subpages` in a hash to
36
+ modify what it does:
37
+
38
+ :do_sort - Defaults to true; sorts the pages by title.
39
+ :include_subdirs - Defaults to true; includes index pages of
40
+ immediate subdirectories.
41
+
42
+ Also, if you give the method a block, each page will be yielded and the
43
+ method will return nil.
44
+
31
45
  ## Installation
32
46
 
33
47
  Add this line to your application's Gemfile:
@@ -53,7 +67,12 @@ example, in the sidebar, you might do this:
53
67
  %h1 Subpages
54
68
  - display_menu(@page.subpages, :class => "menu")
55
69
 
56
-
70
+ Or, if you wanna iterate of the pages:
71
+
72
+ - @page.subpages do |page|
73
+ = page.title
74
+
75
+
57
76
  # Contributing
58
77
 
59
78
  1. Fork it
@@ -8,10 +8,10 @@ module Nesta
8
8
  # array otherwise.
9
9
  def subpages_for(page_or_path)
10
10
  case page_or_path
11
- when Page
11
+ when Nesta::Page
12
12
  return page_or_path.subpages()
13
13
  when String
14
- page = Page.find_by_path(page_or_path)
14
+ page = Nesta::Page.find_by_path(page_or_path)
15
15
  return page.subpages if page
16
16
  end
17
17
 
@@ -27,10 +27,23 @@ module Nesta
27
27
  # page and any index pages in immediate subdirectories. If
28
28
  # the page is NOT an index page or there are NO subpages, an
29
29
  # empty array is returned.
30
- def subpages
30
+ #
31
+ # The argument is a hash that defines two keys:
32
+ # :do_sort -- sort the results by page title.
33
+ # :include_subdirs -- include index pages in immediate subdirectories.
34
+ #
35
+ # If given a block, the method will yield each page to it and
36
+ # method will return nil. Pages are still cached in @subpages
37
+ # however.
38
+ def subpages(opts={})
31
39
  return [] unless self.index_page?
32
40
  return @subpages if @subpages
33
41
 
42
+ options = {
43
+ :do_sort => true,
44
+ :include_subdirs => true
45
+ }.update(opts)
46
+
34
47
  pth = File.dirname(self.filename)
35
48
  mpth = Page.model_path # Just cacheing these two for
36
49
  fmts = FORMATS.join('|') # use below.
@@ -44,9 +57,15 @@ module Nesta
44
57
  entry = File.join(pth, entry)
45
58
 
46
59
  if File.directory?(entry)
47
- # If no index page is detected, move on to the next entry.
48
- idxpth = File.join(entry, 'index.%s')
49
- next unless FORMATS.detect {|fmt| File.exists?(idxpth % fmt)}
60
+ # If we are allowed to include the sub-directory
61
+ # index pages, do so. If no index page is detected,
62
+ # move on to the next entry.
63
+ if options[:include_subdirs]
64
+ idxpth = File.join(entry, 'index.%s')
65
+ next unless FORMATS.detect {|fmt| File.exists?(idxpth % fmt)}
66
+ else
67
+ next
68
+ end
50
69
  else
51
70
  # Ignore any file that isn't a page.
52
71
  next unless entry.match(/\.(#{fmts})$/)
@@ -62,7 +81,16 @@ module Nesta
62
81
  end
63
82
  end
64
83
 
65
- @subpages = pages.sort {|n, m| n.title.downcase <=> m.title.downcase}
84
+ @subpages = pages
85
+
86
+ if options[:do_sort]
87
+ @subpages.sort! {|n, m| n.title.downcase <=> m.title.downcase}
88
+ end
89
+
90
+ if block_given?
91
+ @subpages.each {|page| yield page}
92
+ return nil
93
+ end
66
94
 
67
95
  return @subpages
68
96
  end
@@ -1,7 +1,7 @@
1
1
  module Nesta
2
2
  module Plugin
3
3
  module Subpages
4
- VERSION = "0.5.0"
4
+ VERSION = "0.5.2"
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nesta-plugin-subpages
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-11 00:00:00.000000000 Z
12
+ date: 2012-07-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nesta