pageable 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +45 -0
- data/Rakefile +34 -0
- data/app/views/pageable/_pager.html.erb +29 -0
- data/config/locales/en.yml +6 -0
- data/config/locales/es.yml +6 -0
- data/lib/pageable.rb +9 -0
- data/lib/pageable/action_view/base.rb +40 -0
- data/lib/pageable/active_record/base.rb +32 -0
- data/lib/pageable/active_record/relation.rb +78 -0
- data/lib/pageable/engine.rb +7 -0
- data/lib/pageable/railtie.rb +15 -0
- data/lib/pageable/version.rb +5 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +13 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/controllers/pages_controller.rb +7 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/models/model.rb +2 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/app/views/pages/index.html.erb +1 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +23 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +29 -0
- data/test/dummy/config/environments/production.rb +80 -0
- data/test/dummy/config/environments/test.rb +36 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +12 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +5 -0
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/db/migrate/20130820024335_create_models.rb +9 -0
- data/test/dummy/db/schema.rb +22 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/development.log +284 -0
- data/test/dummy/log/test.log +4658 -0
- data/test/dummy/public/404.html +58 -0
- data/test/dummy/public/422.html +58 -0
- data/test/dummy/public/500.html +57 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
- data/test/dummy/tmp/cache/assets/development/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
- data/test/empty_test.rb +37 -0
- data/test/first_test.rb +38 -0
- data/test/last_test.rb +38 -0
- data/test/middle_test.rb +38 -0
- data/test/padding_test.rb +83 -0
- data/test/test_helper.rb +21 -0
- metadata +189 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 372426615ff7f93835dbc15b7e1da109a69a2455
|
4
|
+
data.tar.gz: c02e4885a4f063e805437986c586d9ca6a8254e1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a8b5cf8f144ac2c530103345e81e557de578a361c23e84fdc11275953f8734d7b9a95fde5da8a5d6cf97f59eb8706f8af73e696e50ee818269c72f9f73a98d50
|
7
|
+
data.tar.gz: dbe69a97b9ccf8bf1a3340ca6cb6c77aa05138de2ce52ad6c0b0928b00ce166bf432792ce28ca526b2d39f23e5b885f6a18d0995b840ecdff9252a34d497ea0d
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2013 Mattways
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
{<img src="https://codeclimate.com/github/mattways/pageable.png" />}[https://codeclimate.com/github/mattways/pageable] {<img src="https://secure.travis-ci.org/mattways/pageable.png?branch=master" alt="Build Status" />}[https://travis-ci.org/mattways/pageable] {<img src="https://gemnasium.com/mattways/pageable.png" alt="Dependency Status" />}[https://gemnasium.com/mattways/pageable]
|
2
|
+
|
3
|
+
= Pageable
|
4
|
+
|
5
|
+
Inspired in kaminari and will_paginate but more minimalistic.
|
6
|
+
|
7
|
+
= Install
|
8
|
+
|
9
|
+
Put this line in your Gemfile:
|
10
|
+
gem 'pageable'
|
11
|
+
|
12
|
+
Then bundle:
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
= Usage
|
16
|
+
|
17
|
+
The same approach as kaminari:
|
18
|
+
@records = Model.page(1).per(20)
|
19
|
+
|
20
|
+
And then just:
|
21
|
+
pager @records
|
22
|
+
|
23
|
+
= Configuration
|
24
|
+
|
25
|
+
== Default parameter and navigation
|
26
|
+
|
27
|
+
If you want to change the default parameter name or the default navigation length call pager:
|
28
|
+
pager @records, navigation: 3, parameter: :p
|
29
|
+
|
30
|
+
Or to set it globally put this in your application.rb:
|
31
|
+
config.pagination.default_parameter = :p
|
32
|
+
config.pagination.default_navigation = 3
|
33
|
+
|
34
|
+
== Default per page
|
35
|
+
|
36
|
+
If you want to change the default per_page put this in your model:
|
37
|
+
default_per_page 5
|
38
|
+
|
39
|
+
Or to set it globally put this in your application.rb:
|
40
|
+
config.pagination.default_per_page = 5
|
41
|
+
|
42
|
+
== Padding
|
43
|
+
|
44
|
+
You can use positive and negative padding:
|
45
|
+
Model.page(1).per(10).padding(-4)
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'Pageable'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
Bundler::GemHelper.install_tasks
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
|
26
|
+
Rake::TestTask.new(:test) do |t|
|
27
|
+
t.libs << 'lib'
|
28
|
+
t.libs << 'test'
|
29
|
+
t.pattern = 'test/**/*_test.rb'
|
30
|
+
t.verbose = false
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
task default: :test
|
@@ -0,0 +1,29 @@
|
|
1
|
+
<% if relation.total_pages > 1 %>
|
2
|
+
<div class="pager">
|
3
|
+
<% if first_page = relation.first_page %>
|
4
|
+
<%= link_to t('pager.first'), page_path(first_page, options), class: 'first' %>
|
5
|
+
<% end %>
|
6
|
+
<% if previous_page = relation.previous_page %>
|
7
|
+
<%= link_to t('pager.previous'), page_path(previous_page, options), class: 'previous' %>
|
8
|
+
<% end %>
|
9
|
+
<% if less_pages %>
|
10
|
+
<span class="ellipsis">...</span>
|
11
|
+
<% end %>
|
12
|
+
<% pages.each do |page| %>
|
13
|
+
<% if relation.current_page == page %>
|
14
|
+
<span class="page current"><%= page %></span>
|
15
|
+
<% else %>
|
16
|
+
<%= link_to page, page_path(page, options), class: 'page' %>
|
17
|
+
<% end %>
|
18
|
+
<% end %>
|
19
|
+
<% if more_pages %>
|
20
|
+
<span class="ellipsis">...</span>
|
21
|
+
<% end %>
|
22
|
+
<% if next_page = relation.next_page %>
|
23
|
+
<%= link_to t('pager.next'), page_path(next_page, options), class: 'next' %>
|
24
|
+
<% end %>
|
25
|
+
<% if last_page = relation.last_page %>
|
26
|
+
<%= link_to t('pager.last'), page_path(last_page, options), class: 'last' %>
|
27
|
+
<% end %>
|
28
|
+
</div>
|
29
|
+
<% end %>
|
data/lib/pageable.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
module Pageable
|
2
|
+
module ActionView
|
3
|
+
module Base
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
def page_path(page, options = {})
|
7
|
+
parameter = options[:parameter] || Rails.application.config.pagination.default_parameter
|
8
|
+
request.path + '?' + request.query_parameters.merge(parameter => page).to_query
|
9
|
+
end
|
10
|
+
|
11
|
+
def pager(relation, options = {})
|
12
|
+
navigation = options[:navigation] || Rails.application.config.pagination.default_navigation
|
13
|
+
if relation.total_pages >= navigation
|
14
|
+
middle = (navigation / 2).ceil
|
15
|
+
if relation.current_page > relation.total_pages - middle
|
16
|
+
starts_at = relation.total_pages - navigation + 1
|
17
|
+
ends_at = relation.total_pages
|
18
|
+
elsif relation.current_page <= middle
|
19
|
+
starts_at = 1
|
20
|
+
ends_at = navigation
|
21
|
+
else
|
22
|
+
starts_at = relation.current_page - middle
|
23
|
+
ends_at = relation.current_page + middle
|
24
|
+
end
|
25
|
+
else
|
26
|
+
starts_at = 1
|
27
|
+
ends_at = relation.total_pages
|
28
|
+
end
|
29
|
+
render partial: 'pageable/pager', locals: {
|
30
|
+
relation: relation,
|
31
|
+
options: options,
|
32
|
+
pages: Range.new(starts_at, ends_at),
|
33
|
+
less_pages: starts_at > 1,
|
34
|
+
more_pages: ends_at < relation.total_pages
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Pageable
|
2
|
+
module ActiveRecord
|
3
|
+
module Base
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
|
8
|
+
def inherited(subclass)
|
9
|
+
subclass.class_eval do
|
10
|
+
if superclass == ::ActiveRecord::Base
|
11
|
+
scope :page, ->(number) {
|
12
|
+
limit(default_per_page).offset(default_per_page * ([number.to_i, 1].max - 1))
|
13
|
+
} do
|
14
|
+
include Pageable::ActiveRecord::Relation
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
super
|
19
|
+
end
|
20
|
+
|
21
|
+
def default_per_page(value=nil)
|
22
|
+
unless value.nil?
|
23
|
+
value = [value.to_i, 1].max
|
24
|
+
@default_per_page = value
|
25
|
+
end
|
26
|
+
defined?(@default_per_page).nil? ? Rails.application.config.pagination.default_per_page : @default_per_page
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module Pageable
|
2
|
+
module ActiveRecord
|
3
|
+
module Relation
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
def per(value)
|
7
|
+
value = [value.to_i, 1].max
|
8
|
+
limit(value).offset(value * (offset_value / limit_value))
|
9
|
+
end
|
10
|
+
|
11
|
+
def padding(value)
|
12
|
+
@padding = value
|
13
|
+
r = (offset_value + value) < 0 ? self : offset(offset_value + value)
|
14
|
+
decrease_limit? ? r.limit(limit_value + value) : r
|
15
|
+
end
|
16
|
+
|
17
|
+
def total_count
|
18
|
+
@total_count ||= begin
|
19
|
+
r = except(:offset, :limit, :order, :reorder)
|
20
|
+
r = r.except(:includes) unless eager_loading?
|
21
|
+
r = r.count
|
22
|
+
r.respond_to?(:count) ? r.count : r
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def total_pages
|
27
|
+
@total_pages ||= [(fixed_total_count.to_f / fixed_limit_value).ceil, 1].max
|
28
|
+
end
|
29
|
+
|
30
|
+
def current_page
|
31
|
+
@current_page ||= (offset_value.to_f / fixed_limit_value).ceil + 1
|
32
|
+
end
|
33
|
+
|
34
|
+
def previous_page
|
35
|
+
@previous_page ||= current_page > 1 ? (current_page - 1) : nil
|
36
|
+
end
|
37
|
+
|
38
|
+
def next_page
|
39
|
+
@next_page ||= current_page < total_pages ? (current_page + 1) : nil
|
40
|
+
end
|
41
|
+
|
42
|
+
def first_page
|
43
|
+
1
|
44
|
+
end
|
45
|
+
|
46
|
+
def last_page
|
47
|
+
total_pages
|
48
|
+
end
|
49
|
+
|
50
|
+
def out_of_bounds?
|
51
|
+
@out_of_bounds ||= current_page > total_pages or current_page < first_page
|
52
|
+
end
|
53
|
+
|
54
|
+
protected
|
55
|
+
|
56
|
+
def has_padding?
|
57
|
+
!defined?(@padding).nil?
|
58
|
+
end
|
59
|
+
|
60
|
+
def padding_negative?
|
61
|
+
has_padding? and @padding < 0
|
62
|
+
end
|
63
|
+
|
64
|
+
def decrease_limit?
|
65
|
+
padding_negative? and offset_value == 0
|
66
|
+
end
|
67
|
+
|
68
|
+
def fixed_total_count
|
69
|
+
total_count - (padding_negative? ? @padding : 0)
|
70
|
+
end
|
71
|
+
|
72
|
+
def fixed_limit_value
|
73
|
+
limit_value - (decrease_limit? ? @padding : 0)
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Pageable
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
|
4
|
+
config.pagination = ActiveSupport::OrderedOptions.new
|
5
|
+
config.pagination.default_per_page = 30
|
6
|
+
config.pagination.default_parameter = :p
|
7
|
+
config.pagination.default_navigation = 5
|
8
|
+
|
9
|
+
initializer 'pageable' do
|
10
|
+
::ActiveRecord::Base.send :include, Pageable::ActiveRecord::Base
|
11
|
+
::ActionView::Base.send :include, Pageable::ActionView::Base
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
== README
|
2
|
+
|
3
|
+
This README would normally document whatever steps are necessary to get the
|
4
|
+
application up and running.
|
5
|
+
|
6
|
+
Things you may want to cover:
|
7
|
+
|
8
|
+
* Ruby version
|
9
|
+
|
10
|
+
* System dependencies
|
11
|
+
|
12
|
+
* Configuration
|
13
|
+
|
14
|
+
* Database creation
|
15
|
+
|
16
|
+
* Database initialization
|
17
|
+
|
18
|
+
* How to run the test suite
|
19
|
+
|
20
|
+
* Services (job queues, cache servers, search engines, etc.)
|
21
|
+
|
22
|
+
* Deployment instructions
|
23
|
+
|
24
|
+
* ...
|
25
|
+
|
26
|
+
|
27
|
+
Please feel free to use a different markup language if you do not plan to run
|
28
|
+
<tt>rake doc:app</tt>.
|
data/test/dummy/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,13 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the top of the
|
9
|
+
* compiled file, but it's generally better to create a new file per style scope.
|
10
|
+
*
|
11
|
+
*= require_self
|
12
|
+
*= require_tree .
|
13
|
+
*/
|