middleman-pagination 1.0.8.beta1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +53 -11
- data/lib/middleman/pagination/configuration.rb +13 -2
- data/lib/middleman/pagination/extension_context.rb +1 -1
- data/lib/middleman/pagination/index_page.rb +5 -5
- data/lib/middleman/pagination/index_path.rb +5 -5
- data/lib/middleman/pagination/pageable.rb +10 -22
- data/lib/middleman/pagination/version.rb +1 -1
- data/spec/features/custom_path_feature_spec.rb +1 -1
- data/spec/features/paginate_any_data_feature_spec.rb +22 -2
- data/spec/features/pagination_feature_spec.rb +6 -6
- data/spec/fixtures/pantheon/data/roman_gods.yml +44 -0
- data/spec/middleman/pagination/configuration_spec.rb +4 -22
- data/spec/middleman/pagination/extension_context_spec.rb +5 -0
- data/spec/middleman/pagination/manipulated_resources_spec.rb +1 -1
- data/spec/support/middleman_server_helpers.rb +3 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a2f30b43ccef991c6a62a958834e01be01e087c
|
4
|
+
data.tar.gz: 0885570cd76963b8332b4c86d420931dda5670c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f4c4dceda27114398941c2d49a6de4f1c5ae47a210aca794cb2caceebdb2782349fc081e5fff9ddc381a48172609850011a044b2da64d50d0135c3e13638462d
|
7
|
+
data.tar.gz: 55f9fc280beee965abd73c181d08ae1cbbf4333d0dde4a12159a9adc458b00f97128c467ccba7828daa413381cc3f03abb4ba22454647e61bf22b295e39d3654
|
data/README.md
CHANGED
@@ -39,7 +39,7 @@ Inside your `config.rb`:
|
|
39
39
|
|
40
40
|
```ruby
|
41
41
|
activate :pagination do
|
42
|
-
|
42
|
+
pageable_resource :recipes do |page|
|
43
43
|
# Match any page that lives in the "recipes" directory
|
44
44
|
page.path.start_with?('recipes/')
|
45
45
|
end
|
@@ -80,12 +80,12 @@ You can define as many different types of pageable resources as you like, with w
|
|
80
80
|
|
81
81
|
```ruby
|
82
82
|
activate :pagination do
|
83
|
-
|
83
|
+
pageable_resource :staff do |page|
|
84
84
|
# Match any page whose URL includes "/staff/"
|
85
85
|
page.url.include?('/staff/')
|
86
86
|
end
|
87
87
|
|
88
|
-
|
88
|
+
pageable_resource :news do |page|
|
89
89
|
# Match any page that has a "news" property in its frontmatter
|
90
90
|
page.data.news.present?
|
91
91
|
end
|
@@ -108,11 +108,55 @@ pagination:
|
|
108
108
|
|
109
109
|
Your pages would be created at `all-recipes/p/1.html`, `all-recipes/p/2.html`, etc.
|
110
110
|
|
111
|
-
## Paginate
|
111
|
+
## Paginate data
|
112
|
+
|
113
|
+
You aren't limited to just pages. You can paginate over [Middleman Local Data](http://middlemanapp.com/advanced/local-data/), too.
|
114
|
+
|
115
|
+
Let's say you had a file called `roman_gods.yml` in your `data` directory:
|
116
|
+
|
117
|
+
```
|
118
|
+
- name: Jupiter
|
119
|
+
title: King of the Gods
|
120
|
+
- name: Juno
|
121
|
+
title: Queen of the Gods
|
122
|
+
- name: Neptune
|
123
|
+
title: God of the Sea
|
124
|
+
- name: Pluto
|
125
|
+
title: God of Death
|
126
|
+
|
127
|
+
...snip...
|
128
|
+
```
|
112
129
|
|
113
|
-
|
130
|
+
You can produce pagination by using `pageable_set`:
|
114
131
|
|
115
|
-
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
activate :pagination do
|
135
|
+
pageable_set :gods do
|
136
|
+
data.roman_gods
|
137
|
+
end
|
138
|
+
end
|
139
|
+
```
|
140
|
+
|
141
|
+
In your template:
|
142
|
+
|
143
|
+
```erb
|
144
|
+
---
|
145
|
+
pagination:
|
146
|
+
for: gods
|
147
|
+
per_page: 10
|
148
|
+
---
|
149
|
+
|
150
|
+
<% pagination.each do |god| %>
|
151
|
+
- <%= god.name %> (<%= god.title %>)
|
152
|
+
<% end %>
|
153
|
+
|
154
|
+
<%= link_to "Next page", pagination.next_page.url if pagination.next_page %>
|
155
|
+
```
|
156
|
+
|
157
|
+
## Paginate anything
|
158
|
+
|
159
|
+
In fact, you can paginate any collection of objects that responds to `each`, by using `pageable_set`:
|
116
160
|
|
117
161
|
```ruby
|
118
162
|
activate :pagination do
|
@@ -124,7 +168,7 @@ end
|
|
124
168
|
|
125
169
|
The set can be used in exactly the same way:
|
126
170
|
|
127
|
-
```
|
171
|
+
```erb
|
128
172
|
---
|
129
173
|
pagination:
|
130
174
|
for: planets
|
@@ -137,7 +181,7 @@ Planets (showing <%= pagination.per_page %> per page):
|
|
137
181
|
- <%= planet %>
|
138
182
|
<% end %>
|
139
183
|
|
140
|
-
Next page
|
184
|
+
<%= link_to "Next page", pagination.next_page.url if pagination.next_page %>
|
141
185
|
```
|
142
186
|
|
143
187
|
## Getting help
|
@@ -146,13 +190,11 @@ Bug? Feature request? You can [open an issue](https://github.com/Aupajo/middlema
|
|
146
190
|
|
147
191
|
## TODO
|
148
192
|
|
149
|
-
* Support for paginating Middleman data
|
150
|
-
* Support for Middleman's Queryable interface
|
151
193
|
* Custom sorting (e.g. by date)
|
152
194
|
* Add tests for metadata support
|
153
195
|
* Convenience helper methods (e.g. make `pagination.` optional)
|
154
196
|
* Pagination link generator (e.g. `Pages: 1 2 [3] ... 7 8 9`)
|
155
|
-
* Adopt Middleman's Queryable interface
|
197
|
+
* Adopt Middleman's Queryable interface (potentially requires changes to Middleman first)
|
156
198
|
|
157
199
|
## Contributing
|
158
200
|
|
@@ -8,11 +8,21 @@ module Middleman
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def pageable(name, &block)
|
11
|
-
|
11
|
+
warn "`pageable` is deprecated, use `pageable_resource` instead"
|
12
|
+
pageable_resource(name, &block)
|
13
|
+
end
|
14
|
+
|
15
|
+
def pageable_resource(name, &block)
|
16
|
+
@pageable[name] = Pageable.new(name) do
|
17
|
+
resources.select do |resource|
|
18
|
+
next if resource.ignored?
|
19
|
+
block.call(resource)
|
20
|
+
end.sort_by(&:path)
|
21
|
+
end
|
12
22
|
end
|
13
23
|
|
14
24
|
def pageable_set(name, &block)
|
15
|
-
@pageable[name] = Pageable.new(name,
|
25
|
+
@pageable[name] = Pageable.new(name, &block)
|
16
26
|
end
|
17
27
|
|
18
28
|
def each(&block)
|
@@ -20,6 +30,7 @@ module Middleman
|
|
20
30
|
yield pageable_obj
|
21
31
|
end
|
22
32
|
end
|
33
|
+
|
23
34
|
end
|
24
35
|
end
|
25
36
|
end
|
@@ -2,10 +2,10 @@ module Middleman
|
|
2
2
|
module Pagination
|
3
3
|
class IndexPage
|
4
4
|
|
5
|
-
attr_reader :
|
5
|
+
attr_reader :extension_context, :first_index, :pageable_context, :page_num, :symbolic_replacement_path
|
6
6
|
|
7
|
-
def initialize(
|
8
|
-
@
|
7
|
+
def initialize(extension_context, first_index, pageable_context, page_num, symbolic_replacement_path)
|
8
|
+
@extension_context = extension_context
|
9
9
|
@first_index = first_index
|
10
10
|
@pageable_context = pageable_context
|
11
11
|
@page_num = page_num
|
@@ -25,11 +25,11 @@ module Middleman
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def sitemap
|
28
|
-
|
28
|
+
extension_context.sitemap
|
29
29
|
end
|
30
30
|
|
31
31
|
def path
|
32
|
-
IndexPath.new(
|
32
|
+
IndexPath.new(extension_context, first_index.path, page_num, symbolic_replacement_path).to_s
|
33
33
|
end
|
34
34
|
|
35
35
|
def metadata
|
@@ -1,10 +1,10 @@
|
|
1
1
|
module Middleman
|
2
2
|
module Pagination
|
3
3
|
class IndexPath
|
4
|
-
attr_accessor :
|
4
|
+
attr_accessor :extension_context, :original_path, :page_num, :symbolic_path_replacement
|
5
5
|
|
6
|
-
def initialize(
|
7
|
-
@
|
6
|
+
def initialize(extension_context, original_path, page_num, symbolic_path_replacement = nil)
|
7
|
+
@extension_context = extension_context
|
8
8
|
@original_path = original_path
|
9
9
|
@symbolic_path_replacement = symbolic_path_replacement || 'pages/:num'
|
10
10
|
|
@@ -38,8 +38,8 @@ module Middleman
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def index_file_pattern
|
41
|
-
index_file_ext = File.extname(
|
42
|
-
index_file_path =
|
41
|
+
index_file_ext = File.extname(extension_context.index_file)
|
42
|
+
index_file_path = extension_context.index_file.delete(index_file_ext)
|
43
43
|
|
44
44
|
%r{
|
45
45
|
(/)? # An optional slash
|
@@ -2,28 +2,23 @@ module Middleman
|
|
2
2
|
module Pagination
|
3
3
|
class Pageable
|
4
4
|
|
5
|
-
attr_reader :name
|
5
|
+
attr_reader :name
|
6
6
|
|
7
|
-
def initialize(name,
|
7
|
+
def initialize(name, &block)
|
8
8
|
@name = name
|
9
|
-
@
|
10
|
-
@set = options[:set]
|
9
|
+
@set = block
|
11
10
|
end
|
12
11
|
|
13
|
-
def new_resources(
|
12
|
+
def new_resources(extension_context, resources)
|
14
13
|
pagination_indexes(resources).map do |resource|
|
15
|
-
new_pages_for_index(
|
14
|
+
new_pages_for_index(extension_context, resource, resources)
|
16
15
|
end.compact
|
17
16
|
end
|
18
17
|
|
19
18
|
private
|
20
19
|
|
21
|
-
def set(resources)
|
22
|
-
|
23
|
-
@set.call
|
24
|
-
else
|
25
|
-
set_from_resource_filter(resources)
|
26
|
-
end
|
20
|
+
def set(extension_context, resources)
|
21
|
+
OpenStruct.new(resources: resources, data: extension_context.data).instance_eval(&@set)
|
27
22
|
end
|
28
23
|
|
29
24
|
def pagination_indexes(resources)
|
@@ -36,19 +31,19 @@ module Middleman
|
|
36
31
|
end
|
37
32
|
end
|
38
33
|
|
39
|
-
def new_pages_for_index(
|
34
|
+
def new_pages_for_index(extension_context, index, resources)
|
40
35
|
symbolic_replacement_path = pagination_data(index, :path)
|
41
36
|
|
42
37
|
pageable_context = PageableContext.new(
|
43
38
|
per_page: pagination_data(index, :per_page) || 20,
|
44
|
-
set: set(resources),
|
39
|
+
set: set(extension_context, resources),
|
45
40
|
index_resources: [index]
|
46
41
|
)
|
47
42
|
|
48
43
|
add_pagination_to(index, pageable_context: pageable_context, page_num: 1)
|
49
44
|
|
50
45
|
(2..pageable_context.total_page_num).map do |page_num|
|
51
|
-
new_index = IndexPage.new(
|
46
|
+
new_index = IndexPage.new(extension_context,
|
52
47
|
index,
|
53
48
|
pageable_context,
|
54
49
|
page_num,
|
@@ -73,13 +68,6 @@ module Middleman
|
|
73
68
|
end
|
74
69
|
end
|
75
70
|
|
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
71
|
end
|
84
72
|
end
|
85
73
|
end
|
@@ -4,7 +4,7 @@ describe "Pagination with custom path", :feature do
|
|
4
4
|
it "produces pages for a set of resources with a custom path" do
|
5
5
|
run_site 'recipes' do
|
6
6
|
activate :pagination do
|
7
|
-
|
7
|
+
pageable_resource :recipes do |resource|
|
8
8
|
resource.path.start_with?('recipes')
|
9
9
|
end
|
10
10
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe "Pagination with
|
4
|
-
it "produces pages for
|
3
|
+
describe "Pagination with a set", :feature do
|
4
|
+
it "produces pages for an array" do
|
5
5
|
run_site 'pantheon' do
|
6
6
|
activate :pagination do
|
7
7
|
pageable_set :gods do
|
@@ -28,4 +28,24 @@ describe "Pagination with data", :feature do
|
|
28
28
|
find_on_page 'Poseidon'
|
29
29
|
find_on_page 'Hermes'
|
30
30
|
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "Pagination with Middleman data", :feature do
|
34
|
+
it "produces pages from given data" do
|
35
|
+
run_site 'pantheon' do
|
36
|
+
activate :pagination do
|
37
|
+
pageable_set :gods do
|
38
|
+
data.roman_gods
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
visit '/'
|
44
|
+
find_on_page 'Jupiter'
|
45
|
+
find_on_page 'King of the Gods'
|
46
|
+
|
47
|
+
visit '/pages/6.html'
|
48
|
+
find_on_page 'Plutus'
|
49
|
+
find_on_page 'God of Wealth'
|
50
|
+
end
|
31
51
|
end
|
@@ -10,7 +10,7 @@ describe "Simple pagination", :feature do
|
|
10
10
|
it "produces pages for a set of resources" do
|
11
11
|
run_site 'recipes' do
|
12
12
|
activate :pagination do
|
13
|
-
|
13
|
+
pageable_resource :recipes do |resource|
|
14
14
|
resource.path.start_with?('recipes')
|
15
15
|
end
|
16
16
|
end
|
@@ -53,7 +53,7 @@ describe "Pagination with directory indexes", :feature do
|
|
53
53
|
it "produces pages for a set of resources" do
|
54
54
|
run_site 'recipes' do
|
55
55
|
activate :pagination do
|
56
|
-
|
56
|
+
pageable_resource :recipes do |resource|
|
57
57
|
resource.path.start_with?('recipes')
|
58
58
|
end
|
59
59
|
end
|
@@ -85,7 +85,7 @@ describe "Pagination with indexes not named index", :feature do
|
|
85
85
|
it "produces pages for a set of resources" do
|
86
86
|
run_site 'recipes' do
|
87
87
|
activate :pagination do
|
88
|
-
|
88
|
+
pageable_resource :recipes do |resource|
|
89
89
|
resource.path.start_with?('recipes')
|
90
90
|
end
|
91
91
|
end
|
@@ -111,7 +111,7 @@ describe "Pagination with proxied resources and ignored proxy resource template"
|
|
111
111
|
end
|
112
112
|
|
113
113
|
activate :pagination do
|
114
|
-
|
114
|
+
pageable_resource :minerals do |resource|
|
115
115
|
resource.path.start_with?('mineral')
|
116
116
|
end
|
117
117
|
end
|
@@ -143,7 +143,7 @@ describe "Pagination with proxied resources and ignored proxy index", :feature d
|
|
143
143
|
proxy '/alternative/index.html', '/index.html', ignore: true
|
144
144
|
|
145
145
|
activate :pagination do
|
146
|
-
|
146
|
+
pageable_resource :minerals do |resource|
|
147
147
|
resource.path.start_with?('mineral')
|
148
148
|
end
|
149
149
|
end
|
@@ -180,7 +180,7 @@ describe "Pagination with proxied resources and two indexes (one proxied)", :fea
|
|
180
180
|
proxy '/alternative/index.html', '/index.html'
|
181
181
|
|
182
182
|
activate :pagination do
|
183
|
-
|
183
|
+
pageable_resource :minerals do |resource|
|
184
184
|
resource.path.start_with?('mineral')
|
185
185
|
end
|
186
186
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
- name: Jupiter
|
2
|
+
title: King of the Gods
|
3
|
+
- name: Juno
|
4
|
+
title: Queen of the Gods
|
5
|
+
- name: Neptune
|
6
|
+
title: God of the Sea
|
7
|
+
- name: Pluto
|
8
|
+
title: God of Death
|
9
|
+
- name: Apollo
|
10
|
+
title: God of the Sun
|
11
|
+
- name: Diana
|
12
|
+
title: Goddess of the Moon
|
13
|
+
- name: Mars
|
14
|
+
title: God of War
|
15
|
+
- name: Venus
|
16
|
+
title: Goddess of Love
|
17
|
+
- name: Cupid
|
18
|
+
title: God of Love
|
19
|
+
- name: Mercury
|
20
|
+
title: Messenger of the Gods
|
21
|
+
- name: Minerva
|
22
|
+
title: Goddess of Wisdom
|
23
|
+
- name: Ceres
|
24
|
+
title: The Earth Goddess
|
25
|
+
- name: Proserpine
|
26
|
+
title: Goddess of the Underworld
|
27
|
+
- name: Vulcan
|
28
|
+
title: The Smith God
|
29
|
+
- name: Bacchus
|
30
|
+
title: God of Wine
|
31
|
+
- name: Saturn
|
32
|
+
title: God of Time
|
33
|
+
- name: Vesta
|
34
|
+
title: Goddess of the Home
|
35
|
+
- name: Janus
|
36
|
+
title: God of Doors
|
37
|
+
- name: Uranus and Gaia
|
38
|
+
title: Parents of Saturn
|
39
|
+
- name: Maia
|
40
|
+
title: Goddess of Growth
|
41
|
+
- name: Flora
|
42
|
+
title: Goddess of Flowers
|
43
|
+
- name: Plutus
|
44
|
+
title: God of Wealth
|
@@ -5,7 +5,7 @@ describe Middleman::Pagination::Configuration do
|
|
5
5
|
it "accepts a name and a block" do
|
6
6
|
config = described_class.new
|
7
7
|
expect {
|
8
|
-
config.
|
8
|
+
config.pageable_resource(:key) { }
|
9
9
|
}.not_to raise_error
|
10
10
|
end
|
11
11
|
end
|
@@ -14,33 +14,15 @@ describe Middleman::Pagination::Configuration do
|
|
14
14
|
it "yields each pageable object" do
|
15
15
|
config = described_class.new
|
16
16
|
block = lambda {}
|
17
|
-
config.
|
17
|
+
config.pageable_resource(:recipes)
|
18
18
|
|
19
19
|
collected = []
|
20
20
|
|
21
21
|
config.each do |pageable|
|
22
|
-
collected <<
|
22
|
+
collected << pageable.name
|
23
23
|
end
|
24
24
|
|
25
|
-
expect(collected).to eql([
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
context "with instance_eval" do
|
30
|
-
it "acts appropriately" do
|
31
|
-
config = described_class.new
|
32
|
-
|
33
|
-
config.instance_eval do
|
34
|
-
pageable(:recipes) { :ok }
|
35
|
-
end
|
36
|
-
|
37
|
-
result = nil
|
38
|
-
|
39
|
-
config.each do |pageable|
|
40
|
-
result = pageable.resource_filter.call
|
41
|
-
end
|
42
|
-
|
43
|
-
expect(result).to be(:ok)
|
25
|
+
expect(collected).to eql([:recipes])
|
44
26
|
end
|
45
27
|
end
|
46
28
|
end
|
@@ -35,6 +35,11 @@ describe Middleman::Pagination::ExtensionContext do
|
|
35
35
|
it_should_behave_like "a method delegated to the app"
|
36
36
|
end
|
37
37
|
|
38
|
+
describe "#sitemap" do
|
39
|
+
let(:method_name) { :data }
|
40
|
+
it_should_behave_like "a method delegated to the app"
|
41
|
+
end
|
42
|
+
|
38
43
|
describe "#index_file" do
|
39
44
|
let(:method_name) { :index_file }
|
40
45
|
it_should_behave_like "a method delegated to the app"
|
@@ -23,7 +23,7 @@ module Middleman::Pagination
|
|
23
23
|
context "with a pageable configuration and an pagination index" do
|
24
24
|
let(:configuration) {
|
25
25
|
config = Configuration.new
|
26
|
-
config.
|
26
|
+
config.pageable_resource(:recipes, &:is_recipe?)
|
27
27
|
config
|
28
28
|
}
|
29
29
|
|
@@ -9,7 +9,9 @@ module MiddlemanServerHelpers
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def visit(path)
|
12
|
-
|
12
|
+
get(path)
|
13
|
+
expect(last_response.status).to eql(200),
|
14
|
+
"Expected 200 response, got #{last_response.status}: #{last_response.body}"
|
13
15
|
end
|
14
16
|
|
15
17
|
def find_on_page(string)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: middleman-pagination
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pete Nicholls
|
@@ -126,6 +126,7 @@ files:
|
|
126
126
|
- spec/features/pagination_feature_spec.rb
|
127
127
|
- spec/fixtures/mine/source/index.html.erb
|
128
128
|
- spec/fixtures/mine/source/minerals/template.html.erb
|
129
|
+
- spec/fixtures/pantheon/data/roman_gods.yml
|
129
130
|
- spec/fixtures/pantheon/source/index.html.erb
|
130
131
|
- spec/fixtures/pantheon/source/layouts/layout.erb
|
131
132
|
- spec/fixtures/recipes/source/all-recipes.html.erb
|
@@ -160,9 +161,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
160
161
|
version: '0'
|
161
162
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
162
163
|
requirements:
|
163
|
-
- - '
|
164
|
+
- - '>='
|
164
165
|
- !ruby/object:Gem::Version
|
165
|
-
version:
|
166
|
+
version: '0'
|
166
167
|
requirements: []
|
167
168
|
rubyforge_project:
|
168
169
|
rubygems_version: 2.0.3
|
@@ -175,6 +176,7 @@ test_files:
|
|
175
176
|
- spec/features/pagination_feature_spec.rb
|
176
177
|
- spec/fixtures/mine/source/index.html.erb
|
177
178
|
- spec/fixtures/mine/source/minerals/template.html.erb
|
179
|
+
- spec/fixtures/pantheon/data/roman_gods.yml
|
178
180
|
- spec/fixtures/pantheon/source/index.html.erb
|
179
181
|
- spec/fixtures/pantheon/source/layouts/layout.erb
|
180
182
|
- spec/fixtures/recipes/source/all-recipes.html.erb
|