paginate_me 0.0.1
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.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/lib/paginate_me/paginate.rb +94 -0
- data/lib/paginate_me/railtie.rb +9 -0
- data/lib/paginate_me/version.rb +3 -0
- data/lib/paginate_me.rb +2 -0
- data/paginate_me.gemspec +24 -0
- metadata +73 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,94 @@
|
|
1
|
+
module PaginateMe
|
2
|
+
module PMController
|
3
|
+
def paginate_me(item, options = {})
|
4
|
+
model_name = item.to_s
|
5
|
+
model = model_name.singularize.camelize.constantize
|
6
|
+
|
7
|
+
@options = {}
|
8
|
+
@options[:base_url] = options[:url] || method("#{model_name}_path").call
|
9
|
+
@options[:per_page] = options[:per_page] || 10
|
10
|
+
@options[:page_total] = (model.count / @options[:per_page].to_f).ceil
|
11
|
+
@options[:current_page] = self.params['page'].to_i || options[:page].to_i || 1
|
12
|
+
|
13
|
+
current_page = @options[:current_page]
|
14
|
+
page_total = @options[:page_total]
|
15
|
+
|
16
|
+
if current_page <= 0
|
17
|
+
current_page = 1
|
18
|
+
elsif current_page > page_total
|
19
|
+
current_page = page_total - 1
|
20
|
+
end
|
21
|
+
|
22
|
+
instance_variable_set("@#{model_name}",
|
23
|
+
model.limit(@options[:per_page]).offset((current_page-1) * @options[:per_page]))
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module PMView
|
28
|
+
def paginate_for(item,options = {},&block)
|
29
|
+
model_name = item.to_s
|
30
|
+
|
31
|
+
options = options.merge @options
|
32
|
+
|
33
|
+
paginate_builder = PaginateMeBuilder.new(options)
|
34
|
+
|
35
|
+
content = capture(paginate_builder,&block)
|
36
|
+
|
37
|
+
content_tag(:div,content, :class => "paginate_me #{model_name}")
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
class PaginateMeBuilder
|
42
|
+
include ActionView::Helpers
|
43
|
+
|
44
|
+
def initialize(options)
|
45
|
+
|
46
|
+
@first_label = options[:first_label] || "First"
|
47
|
+
|
48
|
+
@next_label = options[:next_label] || "Next"
|
49
|
+
@previous_label = options[:previous_label] || "Previous"
|
50
|
+
|
51
|
+
@last_label = options[:last_label] || "Last"
|
52
|
+
|
53
|
+
@base_url = options[:base_url]
|
54
|
+
@per_page = options[:per_page]
|
55
|
+
@page_total = options[:page_total]
|
56
|
+
@current_page = options[:current_page]
|
57
|
+
|
58
|
+
@next_page = (@current_page >= @page_total ? @current_page : @current_page + 1).to_s
|
59
|
+
@prev_page = (@current_page <= 1 ? @current_page : @current_page - 1).to_s
|
60
|
+
end
|
61
|
+
|
62
|
+
def link_to_next
|
63
|
+
add_to_template link_to(@next_label, @base_url + "/page/#{@next_page}", :class => "next") if
|
64
|
+
@current_page < @page_total
|
65
|
+
end
|
66
|
+
|
67
|
+
def link_to_previous
|
68
|
+
add_to_template link_to(@previous_label, @base_url + "/page/#{@prev_page}" , :class => "previous") if
|
69
|
+
@current_page > 1
|
70
|
+
end
|
71
|
+
|
72
|
+
def link_to_first
|
73
|
+
add_to_template link_to(@first_label, @base_url + "/page/#{'1'}", :class => "first") if
|
74
|
+
@current_page < @page_total
|
75
|
+
end
|
76
|
+
|
77
|
+
def link_to_last
|
78
|
+
add_to_template link_to(@last_label, @base_url + "/page/#{@page_total}" , :class => "last") if
|
79
|
+
@current_page > 1
|
80
|
+
end
|
81
|
+
|
82
|
+
def page_out_of_total
|
83
|
+
add_to_template "#{@current_page} of #{@page_total}"
|
84
|
+
end
|
85
|
+
|
86
|
+
private
|
87
|
+
def add_to_template(string)
|
88
|
+
template = "\n\t" + string
|
89
|
+
template.html_safe
|
90
|
+
string
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
data/lib/paginate_me.rb
ADDED
data/paginate_me.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "paginate_me/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "paginate_me"
|
7
|
+
s.version = PaginateMe::VERSION
|
8
|
+
s.authors = ["Adam Rensel"]
|
9
|
+
s.email = ["adamrensel@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Something}
|
12
|
+
s.description = %q{Its a thing}
|
13
|
+
|
14
|
+
s.rubyforge_project = "paginate_me"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
# s.add_runtime_dependency "rest-client"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: paginate_me
|
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
|
+
- Adam Rensel
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-09-16 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Its a thing
|
22
|
+
email:
|
23
|
+
- adamrensel@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- Gemfile
|
33
|
+
- Rakefile
|
34
|
+
- lib/paginate_me.rb
|
35
|
+
- lib/paginate_me/paginate.rb
|
36
|
+
- lib/paginate_me/railtie.rb
|
37
|
+
- lib/paginate_me/version.rb
|
38
|
+
- paginate_me.gemspec
|
39
|
+
homepage: ""
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
hash: 3
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project: paginate_me
|
68
|
+
rubygems_version: 1.8.10
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Something
|
72
|
+
test_files: []
|
73
|
+
|