did_paginate 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 25236dfb785f5fabf62039759b4e04e0159e292b
4
+ data.tar.gz: 3154ed9109f04fb6840dafc6aecf2390ebbfb74b
5
+ SHA512:
6
+ metadata.gz: c2bc26a0159e71a51d69ba83d9b19ffb6e34fb7a0ba287c503afa71cf66902b250f875e1756dd3bd5e37754f0e6cd627d9d4dec01e72936932eff3412983b9c3
7
+ data.tar.gz: 704c004b6ffe7f671da0ecfe776c2b06ed13c540372985bfca6a18e0f72cdb2e1524f3e637da4faa09adfe61cd5c5a9778618d0822a487fec64192b09fd89e62
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in did_paginate.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alex Petropavlovsky
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.
@@ -0,0 +1,95 @@
1
+ # Overview
2
+
3
+ This gem provides Rails view helper for rendering pagination component. It doesn't make any data queries to split data collection on pages, instead it assumes that collection has been already paginated and total pages count is known.
4
+
5
+ Basically it gerenates html list of pagination items, where each item might link to certain page. There are several types of pagination item:
6
+ * previous page
7
+ * general page
8
+ * current page
9
+ * next page
10
+ * gap
11
+
12
+ Previous page item and next page item are presented only if it's possible to go to those pages. General page item contains link to certain page. The items for the first and the last pages are always visible. Current page item doesn't link to current page (no sense to go from current page to current). Gap item contains `…` symbol and also doesn't link anywhere.
13
+
14
+ This is how the rendered component looks like
15
+
16
+ ![Pagination component render](https://www.dropbox.com/s/du116n5vrzunc9f/Screenshot%202014-12-11%2018.38.56.png?dl=1)
17
+
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ gem 'did_paginate'
24
+
25
+ And then execute:
26
+
27
+ $ bundle install
28
+
29
+ ## Usage
30
+
31
+ To render pagination component you should use `did_paginate` helper
32
+
33
+ <%= did_paginate(self,
34
+ @current_page,
35
+ @total_pages,
36
+ params,
37
+ ->(url_params) { refinery.brands_admin_brands_path(url_params) },
38
+ page_param_name: :page,
39
+ klass: 'did-paginate-pager',
40
+ siblings_count: 7)
41
+ %>
42
+
43
+ This is the signature of this helper method
44
+
45
+ did_paginate(template, current_page, total_pages, params, url_builder, options = {})
46
+
47
+ The `params` parameter is a hash of request parameters used to keep other url query parameters in each generated pagination item, e.g. search query or category parameter.
48
+
49
+ When it needs to create a url for certain pagination item, it calls the `url_builder` lambda parameter passing into a hash of all needed parameters for creating a url.
50
+
51
+ The `options` parameter contains following settings:
52
+ * page_param_name
53
+ * klass
54
+ * siblings_count
55
+
56
+ The `page_param_name` parameter is a name of the key of your `params` hash representing page query parameter in the url. Default values is `:page`.
57
+
58
+ The `klass` parameter used for setting a CSS class on the root `ul` element of pagination component. Default value is `did-paginate-pager`. This is the HTML structure of entire component
59
+
60
+ <ul class="did-paginate-pager">
61
+ <li>
62
+ <a href="/products?page=5">previous</a>
63
+ </li>
64
+ <li>
65
+ <a href="/products?page=1">1</a>
66
+ </li>
67
+ <li class="disabled">
68
+ <a href="#">…</a>
69
+ </li>
70
+ ...
71
+ <li class="active">
72
+ <a href="#">6</a>
73
+ </li>
74
+ ...
75
+ <li>
76
+ <a href="/products?page=10">10</a>
77
+ </li>
78
+ <li>
79
+ <a href="/products?page=7">next</a>
80
+ </li>
81
+ </ul>
82
+
83
+ **NOTE**: if you want to use default style you need to require it in you application.css manifest file
84
+
85
+ *= require did_paginate
86
+
87
+ The `siblings_count` parameter determines how many items to show at each side of the current page item. Default value is `5`. Note that for each side this number includes two first items (first page and gap) and two last items (gap and last page), but doesn't include previous page and next page items.
88
+
89
+ ## Contributing
90
+
91
+ 1. Fork it
92
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
93
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
94
+ 4. Push to the branch (`git push origin my-new-feature`)
95
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'did_paginate/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "did_paginate"
8
+ spec.version = DidPaginate::VERSION
9
+ spec.authors = ["Alex Petropavlovsky"]
10
+ spec.email = ["petalvlad@gmail.com"]
11
+ spec.description = 'did_paginate provides Rails view helper to build pagination component. This helper assumes that collection data has been already paginated, i.e. current page and total pages count are known.'
12
+ spec.summary = 'Renders pagination component for given current page and total pages count.'
13
+ spec.homepage = "https://github.com/petalvlad/did_paginate"
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_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,64 @@
1
+ .did-paginate-pager {
2
+ display: inline-block;
3
+ padding-left: 0;
4
+ margin: 20px 0;
5
+ border-radius: 4px;
6
+ }
7
+ .did-paginate-pager > li {
8
+ display: inline;
9
+ padding: 0px !important;
10
+ }
11
+ .did-paginate-pager > li > a,
12
+ .did-paginate-pager > li > span {
13
+ position: relative;
14
+ float: left;
15
+ padding: 6px 12px !important;
16
+ margin-left: -1px;
17
+ line-height: 1.42857143 !important;
18
+ color: #337ab7;
19
+ text-decoration: none;
20
+ background-color: #fff;
21
+ border: 1px solid #ddd !important;
22
+ }
23
+ .did-paginate-pager > li:first-child > a,
24
+ .did-paginate-pager > li:first-child > span {
25
+ margin-left: 0;
26
+ border-top-left-radius: 4px;
27
+ border-bottom-left-radius: 4px;
28
+ }
29
+ .did-paginate-pager > li:last-child > a,
30
+ .did-paginate-pager > li:last-child > span {
31
+ border-top-right-radius: 4px;
32
+ border-bottom-right-radius: 4px;
33
+ }
34
+ .did-paginate-pager > li > a:hover,
35
+ .did-paginate-pager > li > span:hover,
36
+ .did-paginate-pager > li > a:focus,
37
+ .did-paginate-pager > li > span:focus {
38
+ color: #23527c;
39
+ background-color: #eee;
40
+ border-color: #ddd;
41
+ }
42
+ .did-paginate-pager > .active > a,
43
+ .did-paginate-pager > .active > span,
44
+ .did-paginate-pager > .active > a:hover,
45
+ .did-paginate-pager > .active > span:hover,
46
+ .did-paginate-pager > .active > a:focus,
47
+ .did-paginate-pager > .active > span:focus {
48
+ z-index: 2;
49
+ color: #fff;
50
+ cursor: default;
51
+ background-color: #337ab7;
52
+ border-color: #337ab7;
53
+ }
54
+ .did-paginate-pager > .disabled > span,
55
+ .did-paginate-pager > .disabled > span:hover,
56
+ .did-paginate-pager > .disabled > span:focus,
57
+ .did-paginate-pager > .disabled > a,
58
+ .did-paginate-pager > .disabled > a:hover,
59
+ .did-paginate-pager > .disabled > a:focus {
60
+ color: #777;
61
+ cursor: default;
62
+ background-color: #fff;
63
+ border-color: #ddd;
64
+ }
@@ -0,0 +1,9 @@
1
+ require 'did_paginate/version'
2
+ require 'did_paginate/railtie' if defined?(Rails)
3
+ require 'did_paginate/page_item'
4
+ require 'did_paginate/pager'
5
+
6
+ module DidPaginate
7
+ class Engine < ::Rails::Engine
8
+ end
9
+ end
@@ -0,0 +1,73 @@
1
+ module DidPaginate
2
+
3
+ class PageItem
4
+
5
+ ACTIVE_CLASS = 'active'
6
+ DISABLED_CLASS = 'disabled'
7
+ NULL_URL = '#'
8
+
9
+ attr_accessor :url, :content
10
+
11
+ def initialize(url, content, is_current)
12
+ @url = url || NULL_URL
13
+ @content = content
14
+ @is_current = is_current
15
+ end
16
+
17
+ def is_current?
18
+ @is_current
19
+ end
20
+
21
+ def is_linkable?
22
+ @url != NULL_URL
23
+ end
24
+
25
+ def render(template)
26
+ template.content_tag(:li, template.link_to(@content, @url), class: item_class)
27
+ end
28
+
29
+ protected
30
+
31
+ def item_class
32
+ return ACTIVE_CLASS if is_current?
33
+ DISABLED_CLASS unless is_linkable?
34
+ end
35
+
36
+ end
37
+
38
+ class PreviousPageItem < PageItem
39
+
40
+ def initialize(url, content = previous_span, is_current = false)
41
+ super(url, content, is_current)
42
+ end
43
+
44
+ private
45
+
46
+ def previous_span
47
+ '<span aria-hidden="true">&laquo;</span>'.html_safe
48
+ end
49
+
50
+ end
51
+
52
+ class NextPageItem < PageItem
53
+
54
+ def initialize(url, content = next_span, is_current = false)
55
+ super(url, content, is_current)
56
+ end
57
+
58
+ private
59
+
60
+ def next_span
61
+ '<span aria-hidden="true">&raquo;</span>'.html_safe
62
+ end
63
+
64
+ end
65
+
66
+ class GapPageItem < PageItem
67
+
68
+ def initialize(url = nil, content = '…', is_current = false)
69
+ super(url, content, is_current)
70
+ end
71
+
72
+ end
73
+ end
@@ -0,0 +1,127 @@
1
+ module DidPaginate
2
+ class Pager
3
+
4
+ def initialize(current_page, total_pages, params, url_builder, options = {})
5
+ options = default_options.merge(options)
6
+ @current_page = current_page
7
+ @total_pages = total_pages
8
+ @params = params
9
+ @url_builder = url_builder
10
+ @page_param_name = options[:page_param_name]
11
+ @class = options[:klass]
12
+ @siblings_count = options[:siblings_count]
13
+ @all_items_count = @siblings_count * 2 + 1
14
+ end
15
+
16
+ def render(template)
17
+ html = items.map { |item| item.render(template) }.join.html_safe
18
+ template.content_tag(:ul, html, class: @class)
19
+ end
20
+
21
+ def items
22
+ @items ||=
23
+ [
24
+ (previous_page_item if previous_page_item.is_linkable?),
25
+ pages.map { |page| page_item(page) },
26
+ (next_page_item if next_page_item.is_linkable?)]
27
+ .select { |item| item.present? }
28
+ .flatten
29
+ end
30
+
31
+ private
32
+
33
+ def default_options
34
+ {
35
+ page_param_name: :page,
36
+ klass: 'did-paginate-pager',
37
+ siblings_count: 5
38
+ }
39
+ end
40
+
41
+ def previous_page_item
42
+ @previous_page_item ||= PreviousPageItem.new(item_url(@current_page - 1))
43
+ end
44
+
45
+ def next_page_item
46
+ @next_page_item ||= NextPageItem.new(item_url(@current_page + 1))
47
+ end
48
+
49
+ def pages
50
+ return (1..@total_pages).to_a if @total_pages <= @all_items_count
51
+
52
+ # for example [24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34]
53
+ pages = pages_sequence
54
+
55
+ # turns above into [1, nil, 26, 27, 28, 29, 30, 31, 32, nil, 100]
56
+ add_gaps!(pages)
57
+
58
+ pages
59
+ end
60
+
61
+ def pages_sequence
62
+ # for example, if @total_pages equals to 21
63
+ # @current_page equals to 2
64
+ # siblings_count equals to 5
65
+ # then unshifted_sequence will be equal
66
+ # [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7]
67
+ unshifted_sequence = ((@current_page - @siblings_count)..(@current_page + @siblings_count)).to_a
68
+
69
+ # offset will be equal to 4
70
+ offset = offset_of_pages(unshifted_sequence)
71
+
72
+ # shifts the above sequence
73
+ # [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7] to
74
+ # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
75
+ unshifted_sequence.map {|page| page + offset}
76
+ end
77
+
78
+ def offset_of_pages(pages)
79
+ left_offset = 1 - pages.first
80
+ right_offset = @total_pages - pages.last
81
+
82
+ if left_offset > 0
83
+ left_offset
84
+ elsif right_offset < 0
85
+ right_offset
86
+ else
87
+ 0
88
+ end
89
+ end
90
+
91
+ def add_gaps!(pages)
92
+ # adds gap to the head
93
+ # [4, 5, 6, 7, 8, 9, 10] -> [1, nil, 6, 7, 8, 9, 10]
94
+ pages[0], pages[1] = 1, nil if pages[2] != 3
95
+
96
+ # adds gap to the tail
97
+ # [1, 2, 3, 4, 5, 6, 7] -> [1, 2, 3, 4, 5, nil, 10]
98
+ pages[-2], pages[-1] = nil, @total_pages if pages[-3] != @total_pages - 2
99
+ end
100
+
101
+ def page_item(page)
102
+ return GapPageItem.new if page.nil?
103
+ PageItem.new(item_url(page), page, is_page_current?(page))
104
+ end
105
+
106
+ def item_url(page)
107
+ return nil unless is_page_linkable?(page)
108
+ url_params = @params.merge({ @page_param_name => page })
109
+ url_params.delete(@page_param_name) if page <= 1
110
+ url_params = url_params.reject {|k,v| v.blank? }
111
+ @url_builder.call(url_params)
112
+ end
113
+
114
+ def is_page_linkable?(page)
115
+ is_page_exist?(page) && !is_page_current?(page)
116
+ end
117
+
118
+ def is_page_exist?(page)
119
+ page.present? && page > 0 && page <= @total_pages
120
+ end
121
+
122
+ def is_page_current?(page)
123
+ @current_page == page
124
+ end
125
+
126
+ end
127
+ end
@@ -0,0 +1,11 @@
1
+ require 'did_paginate/view_helpers'
2
+
3
+ module DidPaginate
4
+ class Railtie < Rails::Railtie
5
+
6
+ initializer "did_paginate.view_helpers" do
7
+ ActionView::Base.send :include, ViewHelpers
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module DidPaginate
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,9 @@
1
+ module DidPaginate
2
+ module ViewHelpers
3
+
4
+ def did_paginate(template, current_page, total_pages, params, url_builder, options = {})
5
+ DidPaginate::Pager.new(current_page, total_pages, params, url_builder, options).render(template)
6
+ end
7
+
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: did_paginate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alex Petropavlovsky
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: did_paginate provides Rails view helper to build pagination component.
42
+ This helper assumes that collection data has been already paginated, i.e. current
43
+ page and total pages count are known.
44
+ email:
45
+ - petalvlad@gmail.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - .gitignore
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - did_paginate.gemspec
56
+ - lib/assets/stylesheets/did_paginate/index.css
57
+ - lib/did_paginate.rb
58
+ - lib/did_paginate/page_item.rb
59
+ - lib/did_paginate/pager.rb
60
+ - lib/did_paginate/railtie.rb
61
+ - lib/did_paginate/version.rb
62
+ - lib/did_paginate/view_helpers.rb
63
+ homepage: https://github.com/petalvlad/did_paginate
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.2.2
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Renders pagination component for given current page and total pages count.
87
+ test_files: []