simple_page 3.0.4 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,19 +1,90 @@
1
1
  = simple_page
2
2
 
3
- Description goes here.
4
-
5
- == Contributing to simple_page
6
-
7
- * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
- * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
- * Fork the project
10
- * Start a feature/bugfix branch
11
- * Commit and push until you are happy with your contribution
12
- * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
- * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
-
15
- == Copyright
16
-
17
- Copyright (c) 2011 David Monagle. See LICENSE.txt for
18
- further details.
3
+ Adds simple but flexible pagination to ActiveRecord as well as the appropriate helpers to render page links.
4
+
5
+ Part of the core of this works was adapted from the github project paged_scopes.
6
+
7
+ Install the gem and you are ready to go. In your Gemfile:
8
+
9
+ gem 'simple_page'
10
+
11
+ == In the Controllers
12
+
13
+ Use the simple_page helpers - this is only needed if you want to use the standard helpers and are not writing your own.
14
+
15
+ helper :simple_page
16
+
17
+ Modify your action to get the current page and set the page size:
18
+
19
+ def index
20
+ @products = Product.where({})
21
+ @products.page_size = 10
22
+ @page = params[:page] || 1
23
+ respond_with(@products)
24
+ end
25
+
26
+ Note the following: Instead of the standard Product.all - which returns an array - I have used Product.where({}) which returns a scope. The following line sets the pagination size, then we set a variable to select the current page (or page 1) depending on the parameters passed to the action.
27
+
28
+ Lastly we do a normal respond with the @products scope. Very little change to a normal controller method really.
29
+
30
+ === Finding the page number for a known object
31
+
32
+ Something that I need to do from time to time. Especially when somebody direct links to an object and the pages are displayed within the "show" action
33
+
34
+ @product = Product.find(params[:id])
35
+ @products = Product.where({})
36
+ @products.page_size = 10
37
+ @page = @products.find_page_number(@product)
38
+
39
+ This is probably not the most wonderfully efficient thing to do, but it works. Feel free to contact me if you have a better method for implementing this than the one I've used.
40
+
41
+ == In the Views
42
+
43
+ In a view where all of the products would normally be displayed, we now only want to display the products from the selected page. This can be achieved as follows:
44
+
45
+ <% @products.page(@page).each do |product| %>
46
+ ...
47
+ <% end %>
48
+
49
+ Then at the bottom we put in page links:
50
+
51
+ <%= page_links(@products, @page) %>
52
+
53
+ === Extras
54
+
55
+ As well as having a simple list of page numbers, there are four special or extra tags that can be used in the links: :first, :preview, :next, :last. The function of these should be reasonably self explanitory or you probably wouldn't be here.
56
+
57
+ So the standard page_links function could be used like this:
58
+
59
+ <%= page_links(@products, @page, [:first, :previous, :next, :last]) %>
60
+
61
+ == In the Helpers
62
+
63
+ === Built in Helpers
64
+
65
+ These helpers should be fairly self explanitory. Better yet you can easily implement your own if you have formatting requirements. This way if you need pagination to appear differently in different parts of your applicaiton, it's very straight forward to implement your own helpers.
66
+
67
+ def simple_page_marker(page)
68
+ case page
69
+ when :previous
70
+ return "Previous"
71
+ when :next
72
+ return "Next"
73
+ when :first
74
+ return "First"
75
+ when :last
76
+ return "Last"
77
+ else
78
+ return page
79
+ end
80
+ end
81
+
82
+ def page_links(collection, selected_page, extras = [ :previous, :next ], page_tag_helper = lambda {|page| simple_page_marker(page)})
83
+ content_tag :ul, :class => "page_select" do
84
+ collection.page_links_generator(selected_page, :inner => 2, :outer => 1, :extras => extras) do |tag, page_number, classes|
85
+ content_tag :li, link_to(page_tag_helper.call(tag), params.merge(:page => page_number)), :class => (classes << :page).join(" ")
86
+ end
87
+ end
88
+ end
89
+
19
90
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.0.4
1
+ 3.1.0
@@ -54,7 +54,7 @@ module SimplePageExtension
54
54
  numbers.uniq!
55
55
  numbers.sort!
56
56
  numbers.reject! { |number| !number.between?(1, self.page_count) }
57
- returning [] do |results|
57
+ [].tap do |results|
58
58
  results << yield(:first, 1, []) if extras.include?(:first)
59
59
  results << yield(:previous, prev_page, []) if extras.include?(:previous)
60
60
  numbers.zip([nil]+numbers, numbers[1..-1]) do |number, prev_number, next_number|
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{simple_page}
8
- s.version = "3.0.4"
8
+ s.version = "3.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["David Monagle"]
12
- s.date = %q{2011-05-09}
11
+ s.authors = [%q{David Monagle}]
12
+ s.date = %q{2012-01-19}
13
13
  s.description = %q{Extends Active Record to give pagination functions. Also supplies view helpers and generators to create customised pagination links.}
14
14
  s.email = %q{david.monagle@intrica.com.au}
15
15
  s.extra_rdoc_files = [
@@ -32,9 +32,9 @@ Gem::Specification.new do |s|
32
32
  "test/test_simple_page.rb"
33
33
  ]
34
34
  s.homepage = %q{http://github.com/intrica/simple_page}
35
- s.licenses = ["MIT"]
36
- s.require_paths = ["lib"]
37
- s.rubygems_version = %q{1.5.3}
35
+ s.licenses = [%q{MIT}]
36
+ s.require_paths = [%q{lib}]
37
+ s.rubygems_version = %q{1.8.6}
38
38
  s.summary = %q{Active Record extension and view helpers to give pagination abilities}
39
39
  s.test_files = [
40
40
  "test/helper.rb",
metadata CHANGED
@@ -1,65 +1,47 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: simple_page
3
- version: !ruby/object:Gem::Version
4
- hash: 15
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.1.0
5
5
  prerelease:
6
- segments:
7
- - 3
8
- - 0
9
- - 4
10
- version: 3.0.4
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - David Monagle
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-05-09 00:00:00 +10:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2012-01-19 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: bundler
23
- version_requirements: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70258976506900 !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
18
+ requirements:
26
19
  - - ~>
27
- - !ruby/object:Gem::Version
28
- hash: 23
29
- segments:
30
- - 1
31
- - 0
32
- - 0
20
+ - !ruby/object:Gem::Version
33
21
  version: 1.0.0
34
- prerelease: false
35
22
  type: :development
36
- requirement: *id001
37
- - !ruby/object:Gem::Dependency
23
+ prerelease: false
24
+ version_requirements: *70258976506900
25
+ - !ruby/object:Gem::Dependency
38
26
  name: jeweler
39
- version_requirements: &id002 !ruby/object:Gem::Requirement
27
+ requirement: &70258976506420 !ruby/object:Gem::Requirement
40
28
  none: false
41
- requirements:
29
+ requirements:
42
30
  - - ~>
43
- - !ruby/object:Gem::Version
44
- hash: 7
45
- segments:
46
- - 1
47
- - 5
48
- - 2
31
+ - !ruby/object:Gem::Version
49
32
  version: 1.5.2
50
- prerelease: false
51
33
  type: :development
52
- requirement: *id002
53
- description: Extends Active Record to give pagination functions. Also supplies view helpers and generators to create customised pagination links.
34
+ prerelease: false
35
+ version_requirements: *70258976506420
36
+ description: Extends Active Record to give pagination functions. Also supplies view
37
+ helpers and generators to create customised pagination links.
54
38
  email: david.monagle@intrica.com.au
55
39
  executables: []
56
-
57
40
  extensions: []
58
-
59
- extra_rdoc_files:
41
+ extra_rdoc_files:
60
42
  - LICENSE.txt
61
43
  - README.rdoc
62
- files:
44
+ files:
63
45
  - .document
64
46
  - Gemfile
65
47
  - LICENSE.txt
@@ -73,40 +55,34 @@ files:
73
55
  - simple_page.gemspec
74
56
  - test/helper.rb
75
57
  - test/test_simple_page.rb
76
- has_rdoc: true
77
58
  homepage: http://github.com/intrica/simple_page
78
- licenses:
59
+ licenses:
79
60
  - MIT
80
61
  post_install_message:
81
62
  rdoc_options: []
82
-
83
- require_paths:
63
+ require_paths:
84
64
  - lib
85
- required_ruby_version: !ruby/object:Gem::Requirement
65
+ required_ruby_version: !ruby/object:Gem::Requirement
86
66
  none: false
87
- requirements:
88
- - - ">="
89
- - !ruby/object:Gem::Version
90
- hash: 3
91
- segments:
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ segments:
92
72
  - 0
93
- version: "0"
94
- required_rubygems_version: !ruby/object:Gem::Requirement
73
+ hash: -2462150418988594274
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
75
  none: false
96
- requirements:
97
- - - ">="
98
- - !ruby/object:Gem::Version
99
- hash: 3
100
- segments:
101
- - 0
102
- version: "0"
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
103
80
  requirements: []
104
-
105
81
  rubyforge_project:
106
- rubygems_version: 1.5.3
82
+ rubygems_version: 1.8.6
107
83
  signing_key:
108
84
  specification_version: 3
109
85
  summary: Active Record extension and view helpers to give pagination abilities
110
- test_files:
86
+ test_files:
111
87
  - test/helper.rb
112
88
  - test/test_simple_page.rb