simple_pager 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/simple_pager.rb +44 -0
- metadata +65 -0
data/lib/simple_pager.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
module SimplePager
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
# = Simple Paginating for ActiveRecord models
|
7
|
+
#
|
8
|
+
# Adds pager scope to all ActiveRecord models. Works similar to will_paginate but does not count the collection.
|
9
|
+
# You can't set a universal per_page yet, but you can set it on the model. Defaults to 30.
|
10
|
+
#
|
11
|
+
# @posts = Post.pager(:page => params[:page]).order('created_at DESC')
|
12
|
+
module ClassMethods
|
13
|
+
def pager(pp={})
|
14
|
+
limit(pp[:per_page] || (self.respond_to?(:per_page) ? self.per_page : 30)).
|
15
|
+
offset(pp[:page].blank? ? 0 : ((pp[:page].to_i-1)*(pp[:per_page] || (self.respond_to?(:per_page) ? self.per_page : 30))))
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module SimplePagerHelper
|
21
|
+
def simple_pager(collection,per_page=nil,prev_name='Previous',next_name='More')
|
22
|
+
return if !collection
|
23
|
+
per_page = collection.first.class.per_page if per_page.blank? and collection.first.class.respond_to?(:per_page)
|
24
|
+
per_page ||= 30
|
25
|
+
pprev = ''
|
26
|
+
pnext = ''
|
27
|
+
prefix = ''
|
28
|
+
if params[:page] and params[:page].to_i > 1
|
29
|
+
if collection.empty?
|
30
|
+
prefix = content_tag(:p,"There are no more.")
|
31
|
+
end
|
32
|
+
pprev = content_tag(:li, link_to( "← #{prev_name}".html_safe, request.query_parameters.merge({:page => (params[:page].to_i - 1)}) ),:class => 'previous')
|
33
|
+
end
|
34
|
+
if !@no_more_pages and collection.size >= per_page
|
35
|
+
pnext = content_tag(:li, link_to( "#{next_name} →".html_safe, request.query_parameters.merge({:page => (params[:page] ? params[:page].to_i + 1 : 2)}) ),:class => 'next')
|
36
|
+
end
|
37
|
+
(prefix + content_tag(:ul, (pprev+pnext).html_safe, :class => 'pager')).html_safe
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
# mix everything into rails
|
43
|
+
ActiveRecord::Base.send(:include, SimplePager)
|
44
|
+
ActionView::Base.send( :include, SimplePagerHelper)
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_pager
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Peter Gerard
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-10-05 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Super lightweight and simple Rails 3 pagination for when you don't need to count the number of items or pages. simple_pager shows Previous and More links.
|
22
|
+
email:
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- lib/simple_pager.rb
|
31
|
+
homepage: https://github.com/accidental/simple_pager
|
32
|
+
licenses: []
|
33
|
+
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
hash: 3
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.8.10
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Super simple pagination for Rails 3
|
64
|
+
test_files: []
|
65
|
+
|