smart_pagination 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 38a44adbebe9175085a9987a30fd959f0ccea1f3
4
+ data.tar.gz: 41037b399a8913f0774b64883fb18625715dd576
5
+ SHA512:
6
+ metadata.gz: e3876b7f670150061bcce253880684f9076fca8532aa3b2e15cef182214b5177ab1f8136d6d14a5118823ad17885e26a125d3cb61d31996b154f2da6702d8cde
7
+ data.tar.gz: ebe79f1369d85c67f38cab3b47135fe43db92b38d44a53e2bb107da5d20979ddb19cd0325acad08d0f6c4577380ad9945836ef895457f911b248f8c38233e4d4
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in smart_pagination.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,61 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ smart_pagination (0.1.0)
5
+ actionview (>= 4.0.0, < 5.2)
6
+ smart_paginate (~> 0.2)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionview (5.1.1)
12
+ activesupport (= 5.1.1)
13
+ builder (~> 3.1)
14
+ erubi (~> 1.4)
15
+ rails-dom-testing (~> 2.0)
16
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
17
+ activemodel (5.1.1)
18
+ activesupport (= 5.1.1)
19
+ activerecord (5.1.1)
20
+ activemodel (= 5.1.1)
21
+ activesupport (= 5.1.1)
22
+ arel (~> 8.0)
23
+ activesupport (5.1.1)
24
+ concurrent-ruby (~> 1.0, >= 1.0.2)
25
+ i18n (~> 0.7)
26
+ minitest (~> 5.1)
27
+ tzinfo (~> 1.1)
28
+ arel (8.0.0)
29
+ builder (3.2.3)
30
+ concurrent-ruby (1.0.5)
31
+ erubi (1.6.0)
32
+ i18n (0.8.4)
33
+ loofah (2.0.3)
34
+ nokogiri (>= 1.5.9)
35
+ mini_portile2 (2.1.0)
36
+ minitest (5.10.2)
37
+ nokogiri (1.7.2)
38
+ mini_portile2 (~> 2.1.0)
39
+ rails-dom-testing (2.0.3)
40
+ activesupport (>= 4.2.0)
41
+ nokogiri (>= 1.6)
42
+ rails-html-sanitizer (1.0.3)
43
+ loofah (~> 2.0)
44
+ rake (10.5.0)
45
+ smart_paginate (0.2.0)
46
+ activerecord (>= 4.0.0, < 5.2)
47
+ thread_safe (0.3.6)
48
+ tzinfo (1.2.3)
49
+ thread_safe (~> 0.1)
50
+
51
+ PLATFORMS
52
+ ruby
53
+
54
+ DEPENDENCIES
55
+ bundler (~> 1.14)
56
+ minitest (~> 5.0)
57
+ rake (~> 10.0)
58
+ smart_pagination!
59
+
60
+ BUNDLED WITH
61
+ 1.14.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Jonian Guveli
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,92 @@
1
+ # SmartPagination
2
+
3
+ SmartPagination adds view helpers for SmartPaginate. A simple, smart and clean pagination extension for Active Record and plain Ruby Arrays:
4
+
5
+ - **Simple:** Easy to use, with a simple interface. It just does pagination, nothing more.
6
+ - **Smart:** Can navigate through the pages without having to do an expensive count query. SmartPaginate will actually fetch one record more than needed and use it to determine if there's a next page.
7
+ - **Clean:** The code is as minimal as possible while still useful. SmartPaginate does not auto include itself or monkey patch any classes.
8
+
9
+ For more details you can check SmartPaginate at https://github.com/ppostma/smart_paginate.
10
+
11
+ [![Gem Version](https://badge.fury.io/rb/smart_pagination.svg)](https://badge.fury.io/rb/smart_pagination)
12
+ [![Build Status](https://travis-ci.org/hardpixel/smart-pagination.svg?branch=master)](https://travis-ci.org/hardpixel/smart-pagination)
13
+ [![Code Climate](https://codeclimate.com/github/hardpixel/smart-pagination/badges/gpa.png)](https://codeclimate.com/github/hardpixel/smart-pagination)
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'smart_pagination'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install smart_pagination
30
+
31
+ ## Usage
32
+
33
+ To enable pagination in an Active Record model, include the `SmartPagination` or `SmartPaginate` concern in your class:
34
+
35
+ ```ruby
36
+ class User < ActiveRecord::Base
37
+ include SmartPagination
38
+ end
39
+ ```
40
+
41
+ Then you can use the paginate scope to paginate results:
42
+
43
+ ```ruby
44
+ @users = User.paginate(page: params[:page], per_page: params[:per_page])
45
+ ```
46
+
47
+ And finally you can use the `smart_pagination_for` or `pagination_for` helper in your views to render the pagination links:
48
+
49
+ ```ruby
50
+ <%= smart_pagination_for(@users) %>
51
+ ```
52
+
53
+ If you want to render only previous and next links you can use the `smart_pager_for` or `pager_for` helper in your views:
54
+
55
+ ```ruby
56
+ <%= smart_pager_for(@users) %>
57
+ ```
58
+
59
+ There are a number of options you can use to customize the pagination links. The default options are:
60
+
61
+ ```ruby
62
+ options = {
63
+ pager_mode: false,
64
+ item_class: '',
65
+ previous_text: '&laquo;',
66
+ previous_class: 'previous',
67
+ next_text: '&raquo;',
68
+ next_class: 'next',
69
+ active_class: 'active',
70
+ disabled_class: 'disabled',
71
+ wrapper: 'ul',
72
+ wrapper_class: 'pagination',
73
+ item_wrapper: 'li',
74
+ inner_window: 2,
75
+ outer_window: 0
76
+ }
77
+
78
+ smart_pagination_for(@users, options)
79
+ ```
80
+
81
+ ## Development
82
+
83
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run `bundle exec rake install`.
84
+
85
+ ## Contributing
86
+
87
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hardpixel/smart-pagination.
88
+
89
+
90
+ ## License
91
+
92
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,16 @@
1
+ require 'smart_paginate'
2
+ require 'smart_pagination/version'
3
+ require 'smart_pagination/helper'
4
+ require 'smart_pagination/renderer'
5
+
6
+ module SmartPagination
7
+ class << self
8
+ def included(model_class)
9
+ model_class.send :include, SmartPaginate
10
+ end
11
+ end
12
+ end
13
+
14
+ if defined? ActionView::Base
15
+ ActionView::Base.send :include, SmartPagination::Helper
16
+ end
@@ -0,0 +1,20 @@
1
+ module SmartPagination
2
+ module Helper
3
+ # Render pagination links
4
+ def smart_pagination_for(collection, options={})
5
+ SmartPagination::Renderer.new(self, collection, options).render
6
+ end
7
+
8
+ # Alias helper method
9
+ alias :pagination_for :smart_pagination_for
10
+
11
+ # Render pager links
12
+ def smart_pager_for(collection, options={})
13
+ options = options.merge(pager_mode: true)
14
+ SmartPagination::Renderer.new(self, collection, options).render
15
+ end
16
+
17
+ # Alias helper method
18
+ alias :pager_for :smart_pager_for
19
+ end
20
+ end
@@ -0,0 +1,183 @@
1
+ module SmartPagination
2
+ class Renderer
3
+ # Initialize pagination renderer
4
+ def initialize(context, collection, options={})
5
+ @context = context
6
+ @collection = collection
7
+ @options = default_options.merge(options)
8
+ end
9
+
10
+ # Render pagination links
11
+ def render
12
+ pager_mode? ? pager : pagination
13
+ end
14
+
15
+ private
16
+
17
+ # Default options
18
+ def default_options
19
+ {
20
+ pager_mode: false,
21
+ item_class: '',
22
+ previous_text: '&laquo;',
23
+ previous_class: 'previous',
24
+ next_text: '&raquo;',
25
+ next_class: 'next',
26
+ active_class: 'active',
27
+ disabled_class: 'disabled',
28
+ wrapper: 'ul',
29
+ wrapper_class: 'pagination',
30
+ item_wrapper: 'li',
31
+ inner_window: 2,
32
+ outer_window: 0
33
+ }
34
+ end
35
+
36
+ # Get current_page
37
+ def current_page
38
+ @collection.current_page.to_i
39
+ end
40
+
41
+ # Get total pages
42
+ def total_pages
43
+ @total_pages ||= @collection.total_pages
44
+ end
45
+
46
+ # Check if current page
47
+ def current_page?(page)
48
+ page.to_i == current_page
49
+ end
50
+
51
+ # Check if pager mode enabled
52
+ def pager_mode?
53
+ @options[:pager_mode].present?
54
+ end
55
+
56
+ # Get page numbers
57
+ def page_numbers
58
+ inner = @options[:inner_window].to_i
59
+ outer = @options[:outer_window].to_i
60
+ from = current_page - inner
61
+ to = current_page + inner
62
+
63
+ if to > total_pages
64
+ from -= to - total_pages
65
+ to = total_pages
66
+ end
67
+
68
+ if from < 1
69
+ to += 1 - from
70
+ from = 1
71
+ to = total_pages if to > total_pages
72
+ end
73
+
74
+ middle = from..to
75
+
76
+ if outer + 3 < middle.first
77
+ left = (1..(outer + 1)).to_a
78
+ left << :sep
79
+ else
80
+ left = 1...middle.first
81
+ end
82
+
83
+ if total_pages - outer - 2 > middle.last
84
+ right = ((total_pages - outer)..total_pages).to_a
85
+ right.unshift :sep
86
+ else
87
+ right = (middle.last + 1)..total_pages
88
+ end
89
+
90
+ left.to_a + middle.to_a + right.to_a
91
+ end
92
+
93
+ # Get link params
94
+ def link_params(page)
95
+ if page.is_a? Integer
96
+ { page: page }
97
+ else
98
+ { page: @collection.send(:"#{page}_page") }
99
+ end
100
+ end
101
+
102
+ # Get link options
103
+ def link_options(page)
104
+ if page.is_a? Integer
105
+ active = @options[:active_class] if current_page? page
106
+ { class: "#{@options[:item_class]} #{active}".strip }
107
+ else
108
+ disabled = @options[:disabled_class] unless @collection.send(:"#{page}_page?")
109
+ item_class = @options[:"#{page}_class"]
110
+
111
+ { class: "#{@options[:item_class]} #{item_class} #{disabled}".strip }
112
+ end
113
+ end
114
+
115
+ # Page link
116
+ def page_link(page)
117
+ item_link page, link_params(page), link_options(page)
118
+ end
119
+
120
+ # Previous link
121
+ def previous_link
122
+ item_link @options[:previous_text], link_params(:previous), link_options(:previous)
123
+ end
124
+
125
+ # Next link
126
+ def next_link
127
+ item_link @options[:next_text], link_params(:next), link_options(:next)
128
+ end
129
+
130
+ # Render item link
131
+ def item_link(text, params={}, html_options={})
132
+ wrapper = @options[:item_wrapper]
133
+ link_opt = html_options if wrapper.blank?
134
+ item_tag = link_to text, params, link_opt
135
+ item_tag = tag wrapper, item_tag, html_options if wrapper.present?
136
+
137
+ item_tag
138
+ end
139
+
140
+ # Render item separator
141
+ def item_sep
142
+ item_link '&hellip;', {}, class: "#{@options[:item_class]} #{@options[:disabled_class]}".strip
143
+ end
144
+
145
+ # Render content tag
146
+ def tag(*args, &block)
147
+ @context.content_tag(*args, &block)
148
+ end
149
+
150
+ # Render link tag
151
+ def link_to(text, params={}, html_options={})
152
+ url = @context.url_for params.merge(per_page: @collection.per_page)
153
+ opt = html_options.to_h
154
+
155
+ opt.merge!(href: url) if params[:page].present?
156
+ tag :a, "#{text}".html_safe, opt
157
+ end
158
+
159
+ # Render wrapper
160
+ def wrapper(links)
161
+ wrapper = @options[:wrapper]
162
+ wrap_opt = { class: @options[:wrapper_class] }
163
+ wrap_tag = links.html_safe
164
+ wrap_tag = tag wrapper, wrap_tag, wrap_opt if wrapper.present?
165
+
166
+ wrap_tag
167
+ end
168
+
169
+ # Render pager
170
+ def pager
171
+ items = [previous_link, next_link]
172
+ wrapper items.map(&:to_s).join
173
+ end
174
+
175
+ # Render pagination
176
+ def pagination
177
+ items = page_numbers.map { |page| page == :sep ? item_sep : page_link(page) }
178
+ items = [previous_link, *items, next_link]
179
+
180
+ wrapper items.map(&:to_s).join
181
+ end
182
+ end
183
+ end
@@ -0,0 +1,3 @@
1
+ module SmartPagination
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smart_pagination
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jonian Guveli
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: smart_paginate
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: actionview
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 4.0.0
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '5.2'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 4.0.0
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '5.2'
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.14'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.14'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: minitest
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '5.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '5.0'
89
+ description: Adds view helpers for SmartPaginate. A simple, smart and clean pagination
90
+ extension for Active Record and plain Ruby Arrays.
91
+ email:
92
+ - jonian@hardpixel.eu
93
+ executables: []
94
+ extensions: []
95
+ extra_rdoc_files: []
96
+ files:
97
+ - Gemfile
98
+ - Gemfile.lock
99
+ - LICENSE.txt
100
+ - README.md
101
+ - Rakefile
102
+ - lib/smart_pagination.rb
103
+ - lib/smart_pagination/helper.rb
104
+ - lib/smart_pagination/renderer.rb
105
+ - lib/smart_pagination/version.rb
106
+ homepage: https://github.com/hardpixel/smart-pagination
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.6.11
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: View helpers for SmartPaginate
130
+ test_files: []