pagination_render_logic 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ .DS_Store
@@ -1 +1,119 @@
1
- It will hold the logic required for rendering different kinds of pagination templates.
1
+ = pagination_render_logic
2
+ I needed a gem that will provide only the logic for displaying different styles of
3
+ pagination so i have built this gem to do exactly that. So far i have created only the
4
+ digg style pagination but i plan to add Google, simple with Previous and Next page only
5
+ and other...
6
+
7
+ Here is how the digg pagination looks like
8
+ # First Previous 1 2 3 ... 22 23 24 25 26 [27] 28 29 30 31 32 ... 48 49 50 Next Last
9
+
10
+ The logic is simple:
11
+ The chunk 1 2 3 ... i call first_page_section
12
+ The chunk ... 48 49 50 i call last_page_section
13
+ The chunk 22 23 24 25 26 [27] 28 29 30 31 32 is middle_section
14
+
15
+ The responsibility of the gem is also simple, it should provide the answers of the following
16
+ questions in order to render this type of pagination easy. Note that when i say links i
17
+ refer to the page numbers.
18
+
19
+ 1. Do we have links at all?
20
+ 2. Do we need to render the first_page_section and if true give me the links
21
+ (note that the dots are not included in the links we add them manually)
22
+ 3. Do we have a last_page_section and if true give me the links
23
+ 4. Give me the middle_section since it is always present if we have links.
24
+ 5. And the first_page if has_first_page, next_page if has_next_page, previous_page etc.
25
+
26
+ Usage Example:
27
+ Here is how i use this gem in my sinatra blog paginate helper.
28
+ first i require this gem in my config.ru
29
+
30
+ require 'paginate-simple'
31
+ require 'pagination_render_logic/digg'
32
+
33
+ Note that pagination_render_logic does not depend on paginate-simple i simply use it for
34
+ convenience.
35
+ In order to use my renderer i have created a class in my lib dir which is autoloaded by default.
36
+
37
+ class PaginationRenderer
38
+ extend PaginationRenderLogic::Digg
39
+ end
40
+
41
+ And here is the sinatra paginatinon helper
42
+
43
+ require 'sinatra/base'
44
+
45
+ module Sinatra
46
+ module PaginationHelper
47
+ def paginate(resource)
48
+ data = []
49
+ PaginationRenderer.config! :total_pages => Paginator.num_of_pages,
50
+ :current_page => Paginator.current_page,
51
+ :after_first_page => 3,
52
+ :arround_current_page => 5
53
+
54
+ if PaginationRenderer.has_links?
55
+ data << '<div class="pagination">'
56
+ data << "<a href =\"/#{resource}/?page=#{PaginationRenderer.first_page}\">First</a>" if PaginationRenderer.has_first_page?
57
+ data << "<a href =\"/#{resource}/?page=#{PaginationRenderer.previous_page}\">Previous</a>" if PaginationRenderer.has_previous_page?
58
+ if PaginationRenderer.has_first_page_section?
59
+ PaginationRenderer.first_page_section.each do |page|
60
+ data << (PaginationRenderer.current_page == page ? "<span>#{page}</span>" : "<a href =\"/#{resource}/?page=#{page}\">#{page}</a>")
61
+ end
62
+ data << "<span>...</span>"
63
+ end
64
+ PaginationRenderer.middle_section.each do |page|
65
+ data << (PaginationRenderer.current_page == page ? "<span>#{page}</span>" : "<a href =\"/#{resource}/?page=#{page}\">#{page}</a>")
66
+ end
67
+ if PaginationRenderer.has_last_page_section?
68
+ data << "<span>...</span>"
69
+ PaginationRenderer.last_page_section.each do |page|
70
+ data << (PaginationRenderer.current_page == page ? "<span>#{page}</span>" : "<a href =\"/#{resource}/?page=#{page}\">#{page}</a>")
71
+ end
72
+ end
73
+ data << "<a href =\"/#{resource}/?page=#{PaginationRenderer.next_page}\">Next</a>" if PaginationRenderer.has_next_page?
74
+ data << "<a href =\"/#{resource}/?page=#{PaginationRenderer.last_page}\">Last</a>" if PaginationRenderer.has_last_page?
75
+ data << '</div>'
76
+ end
77
+ data.join(' ')
78
+ end
79
+ end
80
+
81
+ # helpers PaginationHelper
82
+ end
83
+
84
+ and i use it like this in my haml file
85
+
86
+ = paginate 'posts'
87
+
88
+
89
+ Although there is a lot of code here it is quite simple to use it. Let me explain...
90
+ First we configure the renderer like this
91
+
92
+ PaginationRenderer.config! :total_pages => Paginator.num_of_pages,
93
+ :current_page => Paginator.current_page,
94
+ :after_first_page => 3,
95
+ :arround_current_page => 5
96
+
97
+ It takes four arguments:
98
+ 1. total_page which is the number of pages we wish to render
99
+ 2. current_page which is the current page we are visiting
100
+ 3. after_first_page is the number of links we want to see after the first page
101
+ 4. arround_current_page is the number of links we want to see around the current page.
102
+
103
+ And then depending on the has_first_page_section?, has_last_page_section? we build the
104
+ links that we need.
105
+
106
+ == Contributing to pagination_render_logic
107
+
108
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
109
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
110
+ * Fork the project
111
+ * Start a feature/bugfix branch
112
+ * Commit and push until you are happy with your contribution
113
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
114
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
115
+
116
+ == Copyright
117
+
118
+ Copyright (c) 2011 filip. See LICENSE.txt for
119
+ further details.
@@ -13,8 +13,8 @@ module PaginationRenderLogic
13
13
  @current_page = args[:current_page] || 1
14
14
  end
15
15
 
16
- def config!
17
- config
16
+ def config!(args = {})
17
+ config(args)
18
18
  setup
19
19
  end
20
20
 
@@ -35,18 +35,34 @@ module PaginationRenderLogic
35
35
  @current_page < @total_pages
36
36
  end
37
37
 
38
+ def next_page
39
+ @current_page + 1
40
+ end
41
+
38
42
  def has_last_page?
39
43
  @current_page < @total_pages
40
44
  end
41
45
 
46
+ def last_page
47
+ @total_pages
48
+ end
49
+
42
50
  def has_first_page?
43
51
  @current_page > 1
44
52
  end
45
53
 
54
+ def first_page
55
+ 1
56
+ end
57
+
46
58
  def has_previous_page?
47
59
  @current_page > 1
48
60
  end
49
61
 
62
+ def previous_page
63
+ @current_page - 1
64
+ end
65
+
50
66
  def has_links?
51
67
  @total_pages > 1
52
68
  end
@@ -1,3 +1,3 @@
1
1
  module PaginationRenderLogic
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: pagination_render_logic
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - sirfilip