middleman-paginate 0.1.1 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 84dab631363b257b6c8add9d272a9b66446f3972
4
- data.tar.gz: 2954d12a57dfb91a52aa3c392fa387b34bfc7bbb
3
+ metadata.gz: e8d9716b324b38651350e48cea10659bf384b198
4
+ data.tar.gz: 152b81f96508e9e3528a0667c18d62d6fe3e11e0
5
5
  SHA512:
6
- metadata.gz: 7f3386ade439d408e336d92887cf3e6be3f316d6d23e72f97d18912d99d367649153091fc1e49dd064381d02aca435a921252f2f92e7af2bc1cedb1a79b19c26
7
- data.tar.gz: 0f9ce64e7005f872c58af15989257412c5bb3f429ad0cac59043ebe713b0de03ebb1b914095a644f6cd7e94516bf238f4c77aad9e151caa715c2741938f5644d
6
+ metadata.gz: d1f573e86cc015a549fa87015a91b0facbb9a0700f91bd4aab9a260082947d3e7bf3f9cc2e04b880f9e79072d7b2fa7caa98a53f64ff4790a1e9fc1fdb6f86cd
7
+ data.tar.gz: b1ea5c1f61f0418f822068e67f173fae91bd7acf6342e93f311bf0311a6bcb0938323f3a4de8a87d806153a101053e07b0cf5e03290612c1d99bf7b9fae1bdc2
@@ -8,17 +8,6 @@ module Middleman
8
8
 
9
9
  expose_to_config paginate: :paginate
10
10
 
11
- class Helper
12
- attr_reader :page, :total_pages, :base_path, :suffix
13
-
14
- def initialize(page, total_pages, base_path, suffix)
15
- @page = page
16
- @total_pages = total_pages
17
- @base_path = base_path
18
- @suffix = suffix
19
- end
20
- end
21
-
22
11
  class CollectionDescriptor
23
12
  attr_reader :descriptors
24
13
 
@@ -33,42 +22,45 @@ module Middleman
33
22
  end
34
23
  end
35
24
 
25
+ class Pager
26
+ attr_reader :current_page, :total_pages, :per_page
27
+
28
+ def initialize(base_path, suffix, current_page, total_pages, per_page)
29
+ @base_path = base_path
30
+ @suffix = suffix
31
+ @current_page = current_page
32
+ @total_pages = total_pages
33
+ @per_page = per_page
34
+ end
35
+
36
+ def next_page
37
+ current_page < total_pages && current_page + 1
38
+ end
39
+
40
+ def previous_page
41
+ current_page > 1 && current_page - 1
42
+ end
43
+
44
+ def page_path(page = current_page)
45
+ "#{@base_path}#{page == 1 ? '' : @suffix.gsub(/:num/, page.to_s)}.html"
46
+ end
47
+ end
48
+
36
49
  def paginate(collection, base_path, template, per_page: 20, suffix: "/page/:num", locals: {}, data: {})
37
50
  pages = collection.each_slice(per_page).to_a
38
- num_pages = pages.size
39
51
 
40
52
  descriptors = []
41
53
 
42
- path_builder = ->(page) {
43
- "#{base_path}#{page == 1 ? '' : suffix.gsub(/:num/, page.to_s)}.html"
44
- }
45
-
46
54
  pages.each_with_index do |page_collection, i|
47
- page_num = i + 1
48
- next_page_num = page_num < num_pages && page_num + 1
49
- previous_page_num = page_num > 1 && page_num - 1
50
-
51
- page_path = path_builder.call(page_num)
52
- next_page_path = next_page_num && path_builder.call(next_page_num)
53
- previous_page_path = previous_page_num && path_builder.call(previous_page_num)
54
-
55
- meta = ::Middleman::Util.recursively_enhance(
56
- page_number: page_num,
57
- num_pages: num_pages,
58
- per_page: per_page,
59
- next_page_num: next_page_num,
60
- next_page_path: next_page_path,
61
- previous_page_num: previous_page_num,
62
- previous_page_path: previous_page_path
63
- )
55
+ pager = Pager.new(base_path, suffix, i + 1, pages.size, per_page)
64
56
 
65
57
  opts = {
66
- locals: locals.merge(items: page_collection, pager: meta),
58
+ locals: locals.merge(items: page_collection, pager: pager),
67
59
  data: data
68
60
  }
69
61
 
70
62
  descriptors << Middleman::Sitemap::Extensions::ProxyDescriptor.new(
71
- Middleman::Util.normalize_path(page_path),
63
+ Middleman::Util.normalize_path(pager.page_path),
72
64
  Middleman::Util.normalize_path(template),
73
65
  opts
74
66
  )
@@ -1,5 +1,5 @@
1
1
  module Middleman
2
2
  module Paginate
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: middleman-paginate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefano Verna
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-29 00:00:00.000000000 Z
11
+ date: 2016-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -113,4 +113,3 @@ signing_key:
113
113
  specification_version: 4
114
114
  summary: A simple helper to generate custom paginated content with Middleman
115
115
  test_files: []
116
- has_rdoc: