middleman-pagination 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +96 -0
- data/Rakefile +6 -0
- data/lib/middleman-pagination.rb +1 -0
- data/lib/middleman/pagination.rb +11 -0
- data/lib/middleman/pagination/configuration.rb +19 -0
- data/lib/middleman/pagination/extension.rb +21 -0
- data/lib/middleman/pagination/extension_context.rb +21 -0
- data/lib/middleman/pagination/in_page_context.rb +32 -0
- data/lib/middleman/pagination/manipulated_resources.rb +65 -0
- data/lib/middleman/pagination/pageable_context.rb +17 -0
- data/lib/middleman/pagination/version.rb +5 -0
- data/lib/middleman_extension.rb +2 -0
- data/middleman-pagination.gemspec +28 -0
- data/spec/features/pagination_feature_spec.rb +90 -0
- data/spec/fixtures/recipes/source/index.html.erb +21 -0
- data/spec/fixtures/recipes/source/layouts/layout.erb +1 -0
- data/spec/fixtures/recipes/source/recipes/bacon.html.erb +5 -0
- data/spec/fixtures/recipes/source/recipes/cake.html.erb +5 -0
- data/spec/fixtures/recipes/source/recipes/gelato.html.erb +5 -0
- data/spec/fixtures/recipes/source/recipes/mead.html.erb +5 -0
- data/spec/fixtures/recipes/source/recipes/sandwich.html.erb +5 -0
- data/spec/fixtures/recipes/source/recipes/waffles.html.erb +5 -0
- data/spec/fixtures/recipes/source/recipes/yeti.html.erb +5 -0
- data/spec/middleman/pagination/configuration_spec.rb +42 -0
- data/spec/middleman/pagination/extension_context_spec.rb +42 -0
- data/spec/middleman/pagination/manipulated_resources_spec.rb +52 -0
- data/spec/spec_helper.rb +13 -0
- data/spec/support/middleman_server_helpers.rb +41 -0
- metadata +175 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fe0923ecf34b591da48f68db56accf21a08c464c
|
4
|
+
data.tar.gz: 069471197145f7573a3b6f697caa7b04c420b0db
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7ff7468e609f467d947bf3262edeaad95bff8a22ad167ad9ad193d74b4b18071f3c06b4bbf42be80eb12a74700a4e1fb0e1f401ee2c77acf02ace7a44c854bc7
|
7
|
+
data.tar.gz: 812a870bf801ba01fe2da1efbd2c99f236fa4a1c1abb113eb83593a117d16c7f7dbb73e5f32cb2436456a31df753e796438bcb1d1639202a77bedbe953c427e1
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Pete Nicholls
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
# Middleman Pagination
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/Aupajo/middleman-pagination.png?branch=master)](https://travis-ci.org/Aupajo/middleman-pagination)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/Aupajo/middleman-pagination.png)](https://codeclimate.com/github/Aupajo/middleman-pagination)
|
5
|
+
[![Dependency Status](https://gemnasium.com/Aupajo/middleman-pagination.png)](https://gemnasium.com/Aupajo/middleman-pagination)
|
6
|
+
|
7
|
+
**Warning: this extension is brand-new, and the API liable to change until the prerelease identifier is dropped.**
|
8
|
+
|
9
|
+
General-purpose pagination support for Middleman pages.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Coming soon.
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
Let's say you have a set of recipes that you want to create pagination for:
|
18
|
+
|
19
|
+
source/
|
20
|
+
all-recipes.html
|
21
|
+
recipes/
|
22
|
+
apple-pie.html
|
23
|
+
bacon.html
|
24
|
+
cake.html
|
25
|
+
|
26
|
+
You can create named pageable resources based on a criteria. In this case, we're going to use any page that lives inside the `recipes` as the criteria for paging through recipes. Inside your `config.rb`:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
activate :pagination do
|
30
|
+
pageable :recipes do |page|
|
31
|
+
# Match any page that lives in the "recipes" directory
|
32
|
+
page.path.start_with?('recipes/')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
**Note:** If you're using the directory indexes extension, place it *after* `activate :directory_indexes`.
|
38
|
+
|
39
|
+
Now, let's set up a *pagination index*. Inside `all-recipes.html`:
|
40
|
+
|
41
|
+
```erb
|
42
|
+
---
|
43
|
+
pagination:
|
44
|
+
for: recipes
|
45
|
+
per_page: 20
|
46
|
+
---
|
47
|
+
|
48
|
+
<% pagination.each do |recipe| %>
|
49
|
+
- <%= link_to recipe.data.title, recipe.url %>
|
50
|
+
<% end %>
|
51
|
+
|
52
|
+
Page <%= pagination.page_num %> of <%= pagination.total_page_num %>
|
53
|
+
|
54
|
+
Showing <%= pagination.per_page %> per page
|
55
|
+
|
56
|
+
<%= link_to "First page", pagination.first_page.url %>
|
57
|
+
|
58
|
+
<%= link_to "Prev page", pagination.prev_page.url if pagination.prev_page %>
|
59
|
+
|
60
|
+
<%= link_to "Next page", pagination.next_page.url if pagination.next_page %>
|
61
|
+
|
62
|
+
<%= link_to "Last page", pagination.first_page.url %>
|
63
|
+
```
|
64
|
+
|
65
|
+
**Note:** the `for` and `per_page` properties must be indented for the `pagination` frontmatter.
|
66
|
+
|
67
|
+
|
68
|
+
You can define as many different types of pageable resources as you like:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
activate :pagination do
|
72
|
+
pageable :staff do |page|
|
73
|
+
# Match any page whose URL includes "/staff/"
|
74
|
+
page.url.include?('/staff/')
|
75
|
+
end
|
76
|
+
|
77
|
+
pageable :news do |page|
|
78
|
+
# Match any page that has "news" in its frontmatter
|
79
|
+
page.data.news.present?
|
80
|
+
end
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
## TODO
|
85
|
+
|
86
|
+
* Customisable path
|
87
|
+
* Custom sorting (e.g. by date)
|
88
|
+
* Convenience helper methods (e.g. make `pagination.` optional)
|
89
|
+
|
90
|
+
## Contributing
|
91
|
+
|
92
|
+
1. Fork it
|
93
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
94
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
95
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
96
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'middleman/pagination'
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require "middleman-core"
|
2
|
+
require "ostruct"
|
3
|
+
require "middleman/pagination/version"
|
4
|
+
require "middleman/pagination/configuration"
|
5
|
+
require "middleman/pagination/extension_context"
|
6
|
+
require "middleman/pagination/manipulated_resources"
|
7
|
+
require "middleman/pagination/pageable_context"
|
8
|
+
require "middleman/pagination/in_page_context"
|
9
|
+
require "middleman/pagination/extension"
|
10
|
+
|
11
|
+
::Middleman::Extensions.register(:pagination, ::Middleman::Pagination::Extension)
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Middleman
|
2
|
+
module Pagination
|
3
|
+
class Configuration
|
4
|
+
include Enumerable
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@pageable = {}
|
8
|
+
end
|
9
|
+
|
10
|
+
def pageable(name, &block)
|
11
|
+
@pageable[name] = block
|
12
|
+
end
|
13
|
+
|
14
|
+
def each(&block)
|
15
|
+
@pageable.each(&block)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "middleman-core/extensions"
|
2
|
+
|
3
|
+
module Middleman
|
4
|
+
module Pagination
|
5
|
+
class Extension < Middleman::Extension
|
6
|
+
def initialize(app, options = {}, &block)
|
7
|
+
@context = ExtensionContext.new(self)
|
8
|
+
super
|
9
|
+
end
|
10
|
+
|
11
|
+
def manipulate_resource_list(resources)
|
12
|
+
mainpulated = ManipulatedResources.new(@context, resources)
|
13
|
+
mainpulated.resource_list
|
14
|
+
end
|
15
|
+
|
16
|
+
def setup_options(options, &block)
|
17
|
+
@context.configuration.instance_eval(&block)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Middleman
|
2
|
+
module Pagination
|
3
|
+
class ExtensionContext
|
4
|
+
extend Forwardable
|
5
|
+
|
6
|
+
def_delegators :app, :sitemap, :index_file
|
7
|
+
|
8
|
+
def initialize(extension)
|
9
|
+
@extension = extension
|
10
|
+
end
|
11
|
+
|
12
|
+
def configuration
|
13
|
+
@configuration ||= Configuration.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def app
|
17
|
+
@extension.app
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Middleman
|
2
|
+
module Pagination
|
3
|
+
class InPageContext < OpenStruct
|
4
|
+
extend Forwardable
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
def_delegators :pageable_context, :resources,
|
8
|
+
:index_resources,
|
9
|
+
:total_page_num,
|
10
|
+
:per_page,
|
11
|
+
:first_page,
|
12
|
+
:last_page
|
13
|
+
|
14
|
+
def next_page
|
15
|
+
index_resources[page_num]
|
16
|
+
end
|
17
|
+
|
18
|
+
def prev_page
|
19
|
+
index_resources[page_num - 2] if page_num > 1
|
20
|
+
end
|
21
|
+
|
22
|
+
def current_resources
|
23
|
+
num_previous = per_page * (page_num - 1)
|
24
|
+
resources.drop(num_previous).take(per_page)
|
25
|
+
end
|
26
|
+
|
27
|
+
def each(&block)
|
28
|
+
current_resources.each(&block)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Middleman
|
2
|
+
module Pagination
|
3
|
+
class ManipulatedResources
|
4
|
+
attr_reader :context, :original_resources
|
5
|
+
|
6
|
+
def initialize(context, resources)
|
7
|
+
@context = context
|
8
|
+
@original_resources = resources
|
9
|
+
end
|
10
|
+
|
11
|
+
def resource_list
|
12
|
+
original_resources + new_resources
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def new_resources
|
18
|
+
context.configuration.map do |name, filter|
|
19
|
+
new_resources_for_pageable(name, filter)
|
20
|
+
end.flatten
|
21
|
+
end
|
22
|
+
|
23
|
+
def new_resources_for_pageable(name, filter)
|
24
|
+
original_resources.map do |resource|
|
25
|
+
if resource.data.pagination.try(:for) == name.to_s
|
26
|
+
new_resources_for_index(resource, filter)
|
27
|
+
end
|
28
|
+
end.compact
|
29
|
+
end
|
30
|
+
|
31
|
+
def new_resources_for_index(first_index, filter)
|
32
|
+
pageable_context = PageableContext.new(
|
33
|
+
per_page: first_index.data.pagination.per_page || 20,
|
34
|
+
resources: original_resources.select(&filter).sort_by(&:path),
|
35
|
+
index_resources: [first_index]
|
36
|
+
)
|
37
|
+
|
38
|
+
add_pagination_to(first_index, pageable_context: pageable_context, page_num: 1)
|
39
|
+
|
40
|
+
(2..pageable_context.total_page_num).map do |n|
|
41
|
+
build_new_index(first_index, pageable_context, n)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def build_new_index(first_index, pageable_context, page_num)
|
46
|
+
sitemap = context.sitemap
|
47
|
+
pattern = %r{(^|/)#{Regexp.escape(context.index_file)}}
|
48
|
+
path = first_index.path.sub(pattern, "pages/#{page_num}.html")
|
49
|
+
source_file = first_index.source_file
|
50
|
+
|
51
|
+
new_index = ::Middleman::Sitemap::Resource.new(sitemap, path, source_file)
|
52
|
+
add_pagination_to(new_index, pageable_context: pageable_context, page_num: page_num)
|
53
|
+
|
54
|
+
pageable_context.index_resources << new_index
|
55
|
+
|
56
|
+
new_index
|
57
|
+
end
|
58
|
+
|
59
|
+
def add_pagination_to(resource, attributes = {})
|
60
|
+
in_page_context = InPageContext.new(attributes)
|
61
|
+
resource.add_metadata(:locals => { 'pagination' => in_page_context })
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Middleman
|
2
|
+
module Pagination
|
3
|
+
class PageableContext < OpenStruct
|
4
|
+
def total_page_num
|
5
|
+
(resources.length.to_f / per_page).ceil
|
6
|
+
end
|
7
|
+
|
8
|
+
def first_page
|
9
|
+
index_resources.first
|
10
|
+
end
|
11
|
+
|
12
|
+
def last_page
|
13
|
+
index_resources.last
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'middleman/pagination/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "middleman-pagination"
|
8
|
+
spec.version = Middleman::Pagination::VERSION
|
9
|
+
spec.authors = ["Pete Nicholls"]
|
10
|
+
spec.email = ["pete@metanation.com"]
|
11
|
+
spec.description = "General-purpose pagination support for Middleman pages."
|
12
|
+
spec.summary = "Pagination for Middleman pages."
|
13
|
+
spec.homepage = "https://github.com/Aupajo/middleman-pagination"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency "middleman-core", "~> 3.1.6"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "middleman", "~> 3.1.6"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
spec.add_development_dependency "rack-test"
|
28
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Simple pagination", :feature do
|
4
|
+
it "activates without any issues" do
|
5
|
+
expect {
|
6
|
+
run_site('recipes') { activate :pagination }
|
7
|
+
}.not_to raise_error
|
8
|
+
end
|
9
|
+
|
10
|
+
it "produces pages for a set of resources" do
|
11
|
+
run_site 'recipes' do
|
12
|
+
activate :pagination do
|
13
|
+
pageable :recipes do |resource|
|
14
|
+
resource.path.start_with?('recipes')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
visit '/'
|
20
|
+
find_on_page 'Showing 2 per page'
|
21
|
+
find_on_page 'First page: /'
|
22
|
+
find_on_page 'Last page: /pages/4.html'
|
23
|
+
|
24
|
+
find_on_page 'Bacon'
|
25
|
+
find_on_page 'Cake'
|
26
|
+
find_on_page 'Page 1 of 4'
|
27
|
+
find_on_page 'Prev page: none'
|
28
|
+
find_on_page 'Next page: /pages/2.html'
|
29
|
+
|
30
|
+
visit '/pages/2.html'
|
31
|
+
find_on_page 'Gelato'
|
32
|
+
find_on_page 'Mead'
|
33
|
+
find_on_page 'Page 2 of 4'
|
34
|
+
find_on_page 'Prev page: /'
|
35
|
+
find_on_page 'Next page: /pages/3.html'
|
36
|
+
|
37
|
+
visit '/pages/3.html'
|
38
|
+
find_on_page 'Sandwich'
|
39
|
+
find_on_page 'Waffles'
|
40
|
+
find_on_page 'Page 3 of 4'
|
41
|
+
find_on_page 'Prev page: /pages/2.html'
|
42
|
+
find_on_page 'Next page: /pages/4.html'
|
43
|
+
|
44
|
+
visit '/pages/4.html'
|
45
|
+
find_on_page 'Yeti'
|
46
|
+
find_on_page 'Page 4 of 4'
|
47
|
+
find_on_page 'Prev page: /pages/3.html'
|
48
|
+
find_on_page 'Next page: none'
|
49
|
+
end
|
50
|
+
|
51
|
+
def visit(path)
|
52
|
+
expect(get(path)).to be_ok
|
53
|
+
end
|
54
|
+
|
55
|
+
def find_on_page(string)
|
56
|
+
expect(last_response.body).to include(string)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "Pagination with directory indexes", :feature do
|
61
|
+
it "produces pages for a set of resources" do
|
62
|
+
run_site 'recipes' do
|
63
|
+
activate :pagination do
|
64
|
+
pageable :recipes do |resource|
|
65
|
+
resource.path.start_with?('recipes')
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
activate :directory_indexes
|
70
|
+
end
|
71
|
+
|
72
|
+
visit '/'
|
73
|
+
find_on_page 'First page: /'
|
74
|
+
find_on_page 'Prev page: none'
|
75
|
+
find_on_page 'Last page: /pages/4/'
|
76
|
+
find_on_page 'Next page: /pages/2/'
|
77
|
+
|
78
|
+
visit '/pages/2/'
|
79
|
+
find_on_page 'Prev page: /'
|
80
|
+
find_on_page 'Next page: /pages/3/'
|
81
|
+
|
82
|
+
visit '/pages/3/'
|
83
|
+
find_on_page 'Prev page: /pages/2/'
|
84
|
+
find_on_page 'Next page: /pages/4/'
|
85
|
+
|
86
|
+
visit '/pages/4/'
|
87
|
+
find_on_page 'Prev page: /pages/3/'
|
88
|
+
find_on_page 'Next page: none'
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
---
|
2
|
+
pagination:
|
3
|
+
for: recipes
|
4
|
+
per_page: 2
|
5
|
+
---
|
6
|
+
|
7
|
+
<% pagination.each do |recipe| %>
|
8
|
+
- <%= recipe.data.title %>
|
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 %>
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Middleman::Pagination::Configuration do
|
4
|
+
describe "#pageable" do
|
5
|
+
it "accepts a name and a block" do
|
6
|
+
config = described_class.new
|
7
|
+
expect {
|
8
|
+
config.pageable(:key) { }
|
9
|
+
}.not_to raise_error
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#each" do
|
14
|
+
it "yields the name and the block" do
|
15
|
+
config = described_class.new
|
16
|
+
block = lambda {}
|
17
|
+
config.pageable(:recipes, &block)
|
18
|
+
|
19
|
+
collected = []
|
20
|
+
|
21
|
+
config.each do |name, block|
|
22
|
+
collected << [name, block]
|
23
|
+
end
|
24
|
+
|
25
|
+
expect(collected).to eql([[:recipes, block]])
|
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
|
+
config.each { |name, block| result = block.call }
|
39
|
+
expect(result).to be(:ok)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Middleman::Pagination::ExtensionContext do
|
4
|
+
describe "#app" do
|
5
|
+
it "returns the extension's app" do
|
6
|
+
app_one, app_two = double, double
|
7
|
+
extension = double(app: app_one)
|
8
|
+
|
9
|
+
context = described_class.new(extension)
|
10
|
+
expect(context.app).to be(app_one)
|
11
|
+
|
12
|
+
extension.stub(app: app_two)
|
13
|
+
expect(context.app).to be(app_two)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "#configuration" do
|
18
|
+
it "returns a configuration object" do
|
19
|
+
context = described_class.new(double)
|
20
|
+
expect(context.configuration).to be_a(Middleman::Pagination::Configuration)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
shared_examples_for "a method delegated to the app" do
|
25
|
+
it "return's the app's method" do
|
26
|
+
app = double(method_name => :delegated)
|
27
|
+
extension = double(app: app)
|
28
|
+
context = described_class.new(extension)
|
29
|
+
expect(context.send(method_name)).to be(:delegated)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#sitemap" do
|
34
|
+
let(:method_name) { :sitemap }
|
35
|
+
it_should_behave_like "a method delegated to the app"
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "#index_file" do
|
39
|
+
let(:method_name) { :index_file }
|
40
|
+
it_should_behave_like "a method delegated to the app"
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Middleman::Pagination
|
4
|
+
describe ManipulatedResources do
|
5
|
+
let(:context) { double(:context, configuration: configuration).as_null_object }
|
6
|
+
let(:configuration) { Configuration.new }
|
7
|
+
let(:resource_list) { [] }
|
8
|
+
|
9
|
+
describe "#initialize" do
|
10
|
+
it "accepts a context and a resource list" do
|
11
|
+
expect { described_class.new(context, resource_list) }.not_to raise_error
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#resource_list" do
|
16
|
+
context "with an empty configuration" do
|
17
|
+
it "returns resources without modification" do
|
18
|
+
manipulated = described_class.new(context, resource_list)
|
19
|
+
expect(manipulated.resource_list).to eql(resource_list)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context "with a pageable configuration and an pagination index" do
|
24
|
+
let(:configuration) {
|
25
|
+
config = Configuration.new
|
26
|
+
config.pageable(:recipes, &:is_recipe?)
|
27
|
+
config
|
28
|
+
}
|
29
|
+
|
30
|
+
let(:pagination_index) {
|
31
|
+
pagination_data = double(for: 'recipes', per_page: 2)
|
32
|
+
resource = double(:resource, path: 'index.html', is_recipe?: false).as_null_object
|
33
|
+
resource.stub_chain(:data, pagination: pagination_data)
|
34
|
+
resource
|
35
|
+
}
|
36
|
+
|
37
|
+
let(:resource_list) {
|
38
|
+
[pagination_index] +
|
39
|
+
7.times.map do |n|
|
40
|
+
double(:resource, path: "recipe-#{n}.html", is_recipe?: true).as_null_object
|
41
|
+
end
|
42
|
+
}
|
43
|
+
|
44
|
+
it "adds new index pages" do
|
45
|
+
expect(resource_list).to have(8).pages
|
46
|
+
manipulated = described_class.new(context, resource_list)
|
47
|
+
expect(manipulated.resource_list).to have(11).pages
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'middleman/pagination'
|
3
|
+
|
4
|
+
# Include support files
|
5
|
+
support_files_pattern = File.expand_path('../support/**/*.rb', __FILE__)
|
6
|
+
Dir.glob(support_files_pattern).each { |f| require f }
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
# Will be the default in RSpec 3
|
10
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
11
|
+
|
12
|
+
config.include MiddlemanServerHelpers, :feature
|
13
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'middleman/rack'
|
2
|
+
require 'rack/test'
|
3
|
+
|
4
|
+
module MiddlemanServerHelpers
|
5
|
+
include Rack::Test::Methods
|
6
|
+
|
7
|
+
def app
|
8
|
+
@app.call
|
9
|
+
end
|
10
|
+
|
11
|
+
def visit(path)
|
12
|
+
expect(get(path)).to be_ok
|
13
|
+
end
|
14
|
+
|
15
|
+
def find_on_page(string)
|
16
|
+
expect(last_response.body).to include(string)
|
17
|
+
end
|
18
|
+
|
19
|
+
def run_site(path, &block)
|
20
|
+
setup_environment(path)
|
21
|
+
|
22
|
+
@app = lambda do
|
23
|
+
instance = Middleman::Application.server.inst do
|
24
|
+
# Require the pagination extension after the
|
25
|
+
# server has booted, as would typically happen.
|
26
|
+
require File.expand_path('../../../lib/middleman_extension', __FILE__)
|
27
|
+
|
28
|
+
instance_exec(&block)
|
29
|
+
end
|
30
|
+
|
31
|
+
instance.class.to_rack_app
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def setup_environment(path)
|
38
|
+
ENV['MM_ROOT'] = File.expand_path("../../fixtures/#{path}", __FILE__)
|
39
|
+
ENV['TEST'] = "true"
|
40
|
+
end
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: middleman-pagination
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pete Nicholls
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: middleman-core
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.1.6
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.1.6
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: middleman
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.1.6
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.1.6
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rack-test
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: General-purpose pagination support for Middleman pages.
|
98
|
+
email:
|
99
|
+
- pete@metanation.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- .gitignore
|
105
|
+
- .travis.yml
|
106
|
+
- Gemfile
|
107
|
+
- LICENSE.txt
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- lib/middleman-pagination.rb
|
111
|
+
- lib/middleman/pagination.rb
|
112
|
+
- lib/middleman/pagination/configuration.rb
|
113
|
+
- lib/middleman/pagination/extension.rb
|
114
|
+
- lib/middleman/pagination/extension_context.rb
|
115
|
+
- lib/middleman/pagination/in_page_context.rb
|
116
|
+
- lib/middleman/pagination/manipulated_resources.rb
|
117
|
+
- lib/middleman/pagination/pageable_context.rb
|
118
|
+
- lib/middleman/pagination/version.rb
|
119
|
+
- lib/middleman_extension.rb
|
120
|
+
- middleman-pagination.gemspec
|
121
|
+
- spec/features/pagination_feature_spec.rb
|
122
|
+
- spec/fixtures/recipes/source/index.html.erb
|
123
|
+
- spec/fixtures/recipes/source/layouts/layout.erb
|
124
|
+
- spec/fixtures/recipes/source/recipes/bacon.html.erb
|
125
|
+
- spec/fixtures/recipes/source/recipes/cake.html.erb
|
126
|
+
- spec/fixtures/recipes/source/recipes/gelato.html.erb
|
127
|
+
- spec/fixtures/recipes/source/recipes/mead.html.erb
|
128
|
+
- spec/fixtures/recipes/source/recipes/sandwich.html.erb
|
129
|
+
- spec/fixtures/recipes/source/recipes/waffles.html.erb
|
130
|
+
- spec/fixtures/recipes/source/recipes/yeti.html.erb
|
131
|
+
- spec/middleman/pagination/configuration_spec.rb
|
132
|
+
- spec/middleman/pagination/extension_context_spec.rb
|
133
|
+
- spec/middleman/pagination/manipulated_resources_spec.rb
|
134
|
+
- spec/spec_helper.rb
|
135
|
+
- spec/support/middleman_server_helpers.rb
|
136
|
+
homepage: https://github.com/Aupajo/middleman-pagination
|
137
|
+
licenses:
|
138
|
+
- MIT
|
139
|
+
metadata: {}
|
140
|
+
post_install_message:
|
141
|
+
rdoc_options: []
|
142
|
+
require_paths:
|
143
|
+
- lib
|
144
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
145
|
+
requirements:
|
146
|
+
- - '>='
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: '0'
|
149
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
150
|
+
requirements:
|
151
|
+
- - '>='
|
152
|
+
- !ruby/object:Gem::Version
|
153
|
+
version: '0'
|
154
|
+
requirements: []
|
155
|
+
rubyforge_project:
|
156
|
+
rubygems_version: 2.0.7
|
157
|
+
signing_key:
|
158
|
+
specification_version: 4
|
159
|
+
summary: Pagination for Middleman pages.
|
160
|
+
test_files:
|
161
|
+
- spec/features/pagination_feature_spec.rb
|
162
|
+
- spec/fixtures/recipes/source/index.html.erb
|
163
|
+
- spec/fixtures/recipes/source/layouts/layout.erb
|
164
|
+
- spec/fixtures/recipes/source/recipes/bacon.html.erb
|
165
|
+
- spec/fixtures/recipes/source/recipes/cake.html.erb
|
166
|
+
- spec/fixtures/recipes/source/recipes/gelato.html.erb
|
167
|
+
- spec/fixtures/recipes/source/recipes/mead.html.erb
|
168
|
+
- spec/fixtures/recipes/source/recipes/sandwich.html.erb
|
169
|
+
- spec/fixtures/recipes/source/recipes/waffles.html.erb
|
170
|
+
- spec/fixtures/recipes/source/recipes/yeti.html.erb
|
171
|
+
- spec/middleman/pagination/configuration_spec.rb
|
172
|
+
- spec/middleman/pagination/extension_context_spec.rb
|
173
|
+
- spec/middleman/pagination/manipulated_resources_spec.rb
|
174
|
+
- spec/spec_helper.rb
|
175
|
+
- spec/support/middleman_server_helpers.rb
|