middleman-pagination 1.0.7 → 1.0.8.beta1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6585d00919a91cfbdddc63b60109f4472f7eb005
4
- data.tar.gz: 616eda1fe14317f7cbaf8b4f26304c5be10aab78
3
+ metadata.gz: b831157223541650867242a75896caca0f81903d
4
+ data.tar.gz: 0564a1276c93a5df3c9d0184ebc66f6fc7985f99
5
5
  SHA512:
6
- metadata.gz: 2700db7a4c0f1d3b264dbdd0ba59f5444962dfbb2fa363ec7e562576113231ad17e9fa261a8e506d4030e4fab59413a22c95f517f1a46330f5b1c8d5caf5a4e0
7
- data.tar.gz: 79ac07a2e77556d20586a315e9b36fc8af03fd7f4a65c3c188c254a80550ccc173370448f95e464fa2a7b6e2d0d92797c99e4f1ffc38e9b05e35349a765e6a5b
6
+ metadata.gz: d0983b61e7e671b5d684552c863bdca87826848dc52885ee0e5366163dbbb52ff0f40b28008400b0ecbdb03f1a4e09f64636835b2faf0fc0e82724d5abd85be1
7
+ data.tar.gz: 2ba41a1bea041d3affb87ffdd518d3838468b00209b3be23d2560610b42fad075af7db610b2bffad1a182d6deca8d089b5afe416c246c740fee986cf97476dc1
data/README.md CHANGED
@@ -4,8 +4,10 @@
4
4
  [![Code Climate](https://codeclimate.com/github/Aupajo/middleman-pagination.png)](https://codeclimate.com/github/Aupajo/middleman-pagination)
5
5
  [![Dependency Status](https://gemnasium.com/Aupajo/middleman-pagination.png)](https://gemnasium.com/Aupajo/middleman-pagination)
6
6
 
7
- General-purpose pagination support for Middleman pages. Proxy pages for both pageable resources and pagination indexes are supported.
8
-
7
+ General-purpose pagination support for Middleman.
8
+
9
+ Middleman resources, proxy pages, and any arbitrary collection of objects can be paginated.
10
+
9
11
  ## Installation
10
12
 
11
13
  Add this line to your Middleman site's Gemfile:
@@ -106,12 +108,46 @@ pagination:
106
108
 
107
109
  Your pages would be created at `all-recipes/p/1.html`, `all-recipes/p/2.html`, etc.
108
110
 
111
+ ## Paginate anything
112
+
113
+ *This is an experimental feature available in `1.0.8.beta1`. Change your Gemfile to `gem 'middleman-pagination', '~> 1.0.8.beta1` to try it out.*
114
+
115
+ You can paginate any collection of objects that responds to `each`, by using `pageable_set`:
116
+
117
+ ```ruby
118
+ activate :pagination do
119
+ pageable_set :planets do
120
+ ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']
121
+ end
122
+ end
123
+ ```
124
+
125
+ The set can be used in exactly the same way:
126
+
127
+ ```
128
+ ---
129
+ pagination:
130
+ for: planets
131
+ per_page: 4
132
+ ---
133
+
134
+ Planets (showing <%= pagination.per_page %> per page):
135
+
136
+ <% pagination.each do |planet| %>
137
+ - <%= planet %>
138
+ <% end %>
139
+
140
+ Next page: <%= pagination.next_page.url %>
141
+ ```
142
+
109
143
  ## Getting help
110
144
 
111
145
  Bug? Feature request? You can [open an issue](https://github.com/Aupajo/middleman-pagination/issues), [contact me on Twitter](http://twitter.com/aupajo), or [start a new topic on the Middleman forums](http://forum.middlemanapp.com). All feedback and suggestions welcome.
112
146
 
113
147
  ## TODO
114
148
 
149
+ * Support for paginating Middleman data
150
+ * Support for Middleman's Queryable interface
115
151
  * Custom sorting (e.g. by date)
116
152
  * Add tests for metadata support
117
153
  * Convenience helper methods (e.g. make `pagination.` optional)
@@ -4,7 +4,9 @@ require "middleman/pagination/version"
4
4
  require "middleman/pagination/configuration"
5
5
  require "middleman/pagination/extension_context"
6
6
  require "middleman/pagination/manipulated_resources"
7
+ require "middleman/pagination/pageable"
7
8
  require "middleman/pagination/pageable_context"
9
+ require "middleman/pagination/index_page"
8
10
  require "middleman/pagination/index_path"
9
11
  require "middleman/pagination/in_page_context"
10
12
  require "middleman/pagination/extension"
@@ -8,11 +8,17 @@ module Middleman
8
8
  end
9
9
 
10
10
  def pageable(name, &block)
11
- @pageable[name] = block
11
+ @pageable[name] = Pageable.new(name, resource_filter: block)
12
+ end
13
+
14
+ def pageable_set(name, &block)
15
+ @pageable[name] = Pageable.new(name, set: block)
12
16
  end
13
17
 
14
18
  def each(&block)
15
- @pageable.each(&block)
19
+ @pageable.each do |name, pageable_obj|
20
+ yield pageable_obj
21
+ end
16
22
  end
17
23
  end
18
24
  end
@@ -4,7 +4,7 @@ module Middleman
4
4
  extend Forwardable
5
5
  include Enumerable
6
6
 
7
- def_delegators :pageable_context, :resources,
7
+ def_delegators :pageable_context, :set,
8
8
  :index_resources,
9
9
  :total_page_num,
10
10
  :per_page,
@@ -19,13 +19,13 @@ module Middleman
19
19
  index_resources[page_num - 2] if page_num > 1
20
20
  end
21
21
 
22
- def current_resources
22
+ def subset
23
23
  num_previous = per_page * (page_num - 1)
24
- resources.drop(num_previous).take(per_page)
24
+ set.drop(num_previous).take(per_page)
25
25
  end
26
26
 
27
27
  def each(&block)
28
- current_resources.each(&block)
28
+ subset.each(&block)
29
29
  end
30
30
  end
31
31
  end
@@ -0,0 +1,49 @@
1
+ module Middleman
2
+ module Pagination
3
+ class IndexPage
4
+
5
+ attr_reader :context, :first_index, :pageable_context, :page_num, :symbolic_replacement_path
6
+
7
+ def initialize(context, first_index, pageable_context, page_num, symbolic_replacement_path)
8
+ @context = context
9
+ @first_index = first_index
10
+ @pageable_context = pageable_context
11
+ @page_num = page_num
12
+ @symbolic_replacement_path = symbolic_replacement_path
13
+ end
14
+
15
+ def resource
16
+ res = ::Middleman::Sitemap::Resource.new(sitemap, path, source_file)
17
+ res.add_metadata(metadata)
18
+ res
19
+ end
20
+
21
+ private
22
+
23
+ def source_file
24
+ first_index.source_file
25
+ end
26
+
27
+ def sitemap
28
+ context.sitemap
29
+ end
30
+
31
+ def path
32
+ IndexPath.new(context, first_index.path, page_num, symbolic_replacement_path).to_s
33
+ end
34
+
35
+ def metadata
36
+ { locals: locals }
37
+ end
38
+
39
+ def locals
40
+ { pagination: in_page_context }
41
+ end
42
+
43
+ def in_page_context
44
+ InPageContext.new(pageable_context: pageable_context, page_num: page_num)
45
+ end
46
+
47
+ end
48
+ end
49
+ end
@@ -14,62 +14,11 @@ module Middleman
14
14
 
15
15
  private
16
16
 
17
- def pagination_data(resource, key)
18
- keys = [:pagination, key]
19
-
20
- [resource.data, resource.metadata[:options]].inject(nil) do |result, data_source|
21
- result or keys.inject(data_source) { |source, key| source.try(:[], key) }
22
- end
23
- end
24
-
25
17
  def new_resources
26
- context.configuration.map do |name, filter|
27
- new_resources_for_pageable(name, filter)
18
+ context.configuration.map do |pageable|
19
+ pageable.new_resources(context, original_resources)
28
20
  end.flatten
29
21
  end
30
-
31
- def new_resources_for_pageable(name, filter)
32
- original_resources.map do |resource|
33
- if !resource.ignored? && pagination_data(resource, :for) == name.to_s
34
- new_resources_for_index(resource, filter)
35
- end
36
- end.compact
37
- end
38
-
39
- def new_resources_for_index(first_index, filter)
40
- symbolic_replacement_path = pagination_data(first_index, :path)
41
-
42
- pageable_context = PageableContext.new(
43
- per_page: pagination_data(first_index, :per_page) || 20,
44
- # OPTIMIZE
45
- resources: original_resources.reject(&:ignored?).select(&filter).sort_by(&:path),
46
- index_resources: [first_index]
47
- )
48
-
49
- add_pagination_to(first_index, pageable_context: pageable_context, page_num: 1)
50
-
51
- (2..pageable_context.total_page_num).map do |n|
52
- build_new_index(first_index, pageable_context, n, symbolic_replacement_path)
53
- end
54
- end
55
-
56
- def build_new_index(first_index, pageable_context, page_num, symbolic_replacement_path)
57
- sitemap = context.sitemap
58
- path = IndexPath.new(context, first_index.path, page_num, symbolic_replacement_path).to_s
59
- source_file = first_index.source_file
60
-
61
- new_index = ::Middleman::Sitemap::Resource.new(sitemap, path, source_file)
62
- add_pagination_to(new_index, pageable_context: pageable_context, page_num: page_num)
63
-
64
- pageable_context.index_resources << new_index
65
-
66
- new_index
67
- end
68
-
69
- def add_pagination_to(resource, attributes = {})
70
- in_page_context = InPageContext.new(attributes)
71
- resource.add_metadata(locals: { 'pagination' => in_page_context })
72
- end
73
22
  end
74
23
  end
75
24
  end
@@ -0,0 +1,85 @@
1
+ module Middleman
2
+ module Pagination
3
+ class Pageable
4
+
5
+ attr_reader :name, :resource_filter
6
+
7
+ def initialize(name, options = {})
8
+ @name = name
9
+ @resource_filter = options[:resource_filter]
10
+ @set = options[:set]
11
+ end
12
+
13
+ def new_resources(context, resources)
14
+ pagination_indexes(resources).map do |resource|
15
+ new_pages_for_index(context, resource, resources)
16
+ end.compact
17
+ end
18
+
19
+ private
20
+
21
+ def set(resources)
22
+ if @set
23
+ @set.call
24
+ else
25
+ set_from_resource_filter(resources)
26
+ end
27
+ end
28
+
29
+ def pagination_indexes(resources)
30
+ Enumerator.new do |enum|
31
+ resources.each do |resource|
32
+ if !resource.ignored? && pagination_data(resource, :for) == name.to_s
33
+ enum.yield resource
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ def new_pages_for_index(context, index, resources)
40
+ symbolic_replacement_path = pagination_data(index, :path)
41
+
42
+ pageable_context = PageableContext.new(
43
+ per_page: pagination_data(index, :per_page) || 20,
44
+ set: set(resources),
45
+ index_resources: [index]
46
+ )
47
+
48
+ add_pagination_to(index, pageable_context: pageable_context, page_num: 1)
49
+
50
+ (2..pageable_context.total_page_num).map do |page_num|
51
+ new_index = IndexPage.new(context,
52
+ index,
53
+ pageable_context,
54
+ page_num,
55
+ symbolic_replacement_path).resource
56
+
57
+ pageable_context.index_resources << new_index
58
+
59
+ new_index
60
+ end
61
+ end
62
+
63
+ def add_pagination_to(resource, attributes = {})
64
+ in_page_context = InPageContext.new(attributes)
65
+ resource.add_metadata(locals: { 'pagination' => in_page_context })
66
+ end
67
+
68
+ def pagination_data(resource, key)
69
+ keys = [:pagination, key]
70
+
71
+ [resource.data, resource.metadata[:options]].inject(nil) do |result, data_source|
72
+ result or keys.inject(data_source) { |source, key| source.try(:[], key) }
73
+ end
74
+ end
75
+
76
+ def set_from_resource_filter(resources)
77
+ resources.select do |resource|
78
+ next if resource.ignored?
79
+ resource_filter.call(resource)
80
+ end.sort_by(&:path)
81
+ end
82
+
83
+ end
84
+ end
85
+ end
@@ -2,7 +2,7 @@ module Middleman
2
2
  module Pagination
3
3
  class PageableContext < OpenStruct
4
4
  def total_page_num
5
- (resources.length.to_f / per_page).ceil
5
+ (set.length.to_f / per_page).ceil
6
6
  end
7
7
 
8
8
  def first_page
@@ -1,5 +1,5 @@
1
1
  module Middleman
2
2
  module Pagination
3
- VERSION = "1.0.7"
3
+ VERSION = "1.0.8.beta1"
4
4
  end
5
5
  end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Pagination with data", :feature do
4
+ it "produces pages for a set of resources with a custom path" do
5
+ run_site 'pantheon' do
6
+ activate :pagination do
7
+ pageable_set :gods do
8
+ %w{ Ares Aphrodite Apollo Artemis Athena Demeter Hephaestus Hestia Zeus Hera Poseidon Hermes }
9
+ end
10
+ end
11
+ end
12
+
13
+ visit '/'
14
+ find_on_page 'Ares'
15
+ find_on_page 'Aphrodite'
16
+ find_on_page 'Apollo'
17
+ find_on_page 'Artemis'
18
+
19
+ visit '/pages/2.html'
20
+ find_on_page 'Athena'
21
+ find_on_page 'Demeter'
22
+ find_on_page 'Hephaestus'
23
+ find_on_page 'Hestia'
24
+
25
+ visit '/pages/3.html'
26
+ find_on_page 'Zeus'
27
+ find_on_page 'Hera'
28
+ find_on_page 'Poseidon'
29
+ find_on_page 'Hermes'
30
+ end
31
+ end
@@ -0,0 +1,21 @@
1
+ ---
2
+ pagination:
3
+ for: gods
4
+ per_page: 4
5
+ ---
6
+
7
+ <% pagination.each do |god| %>
8
+ - <%= god %>
9
+ <% end %>
10
+
11
+ Page <%= pagination.page_num %> of <%= pagination.total_page_num %>
12
+
13
+ Showing <%= pagination.per_page %> per page
14
+
15
+ First page: <%= pagination.first_page.url %>
16
+
17
+ Next page: <%= pagination.next_page.try(:url) || 'none' %>
18
+
19
+ Prev page: <%= pagination.prev_page.try(:url) || 'none' %>
20
+
21
+ Last page: <%= pagination.last_page.url %>
@@ -0,0 +1 @@
1
+ <%= yield %>
@@ -11,15 +11,15 @@ describe Middleman::Pagination::Configuration do
11
11
  end
12
12
 
13
13
  describe "#each" do
14
- it "yields the name and the block" do
14
+ it "yields each pageable object" do
15
15
  config = described_class.new
16
16
  block = lambda {}
17
17
  config.pageable(:recipes, &block)
18
18
 
19
19
  collected = []
20
20
 
21
- config.each do |name, block|
22
- collected << [name, block]
21
+ config.each do |pageable|
22
+ collected << [pageable.name, pageable.resource_filter]
23
23
  end
24
24
 
25
25
  expect(collected).to eql([[:recipes, block]])
@@ -35,7 +35,11 @@ describe Middleman::Pagination::Configuration do
35
35
  end
36
36
 
37
37
  result = nil
38
- config.each { |name, block| result = block.call }
38
+
39
+ config.each do |pageable|
40
+ result = pageable.resource_filter.call
41
+ end
42
+
39
43
  expect(result).to be(:ok)
40
44
  end
41
45
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: middleman-pagination
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.7
4
+ version: 1.0.8.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pete Nicholls
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-18 00:00:00.000000000 Z
11
+ date: 2013-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: middleman-core
@@ -113,16 +113,21 @@ files:
113
113
  - lib/middleman/pagination/extension.rb
114
114
  - lib/middleman/pagination/extension_context.rb
115
115
  - lib/middleman/pagination/in_page_context.rb
116
+ - lib/middleman/pagination/index_page.rb
116
117
  - lib/middleman/pagination/index_path.rb
117
118
  - lib/middleman/pagination/manipulated_resources.rb
119
+ - lib/middleman/pagination/pageable.rb
118
120
  - lib/middleman/pagination/pageable_context.rb
119
121
  - lib/middleman/pagination/version.rb
120
122
  - lib/middleman_extension.rb
121
123
  - middleman-pagination.gemspec
122
124
  - spec/features/custom_path_feature_spec.rb
125
+ - spec/features/paginate_any_data_feature_spec.rb
123
126
  - spec/features/pagination_feature_spec.rb
124
127
  - spec/fixtures/mine/source/index.html.erb
125
128
  - spec/fixtures/mine/source/minerals/template.html.erb
129
+ - spec/fixtures/pantheon/source/index.html.erb
130
+ - spec/fixtures/pantheon/source/layouts/layout.erb
126
131
  - spec/fixtures/recipes/source/all-recipes.html.erb
127
132
  - spec/fixtures/recipes/source/custom.html.erb
128
133
  - spec/fixtures/recipes/source/index.html.erb
@@ -155,20 +160,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
155
160
  version: '0'
156
161
  required_rubygems_version: !ruby/object:Gem::Requirement
157
162
  requirements:
158
- - - '>='
163
+ - - '>'
159
164
  - !ruby/object:Gem::Version
160
- version: '0'
165
+ version: 1.3.1
161
166
  requirements: []
162
167
  rubyforge_project:
163
- rubygems_version: 2.0.7
168
+ rubygems_version: 2.0.3
164
169
  signing_key:
165
170
  specification_version: 4
166
171
  summary: Pagination for Middleman pages.
167
172
  test_files:
168
173
  - spec/features/custom_path_feature_spec.rb
174
+ - spec/features/paginate_any_data_feature_spec.rb
169
175
  - spec/features/pagination_feature_spec.rb
170
176
  - spec/fixtures/mine/source/index.html.erb
171
177
  - spec/fixtures/mine/source/minerals/template.html.erb
178
+ - spec/fixtures/pantheon/source/index.html.erb
179
+ - spec/fixtures/pantheon/source/layouts/layout.erb
172
180
  - spec/fixtures/recipes/source/all-recipes.html.erb
173
181
  - spec/fixtures/recipes/source/custom.html.erb
174
182
  - spec/fixtures/recipes/source/index.html.erb