public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Search Repo:
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
Switch to will_paginate plugin [Mislav]

git-svn-id: http://svn.techno-weenie.net/projects/mephisto/trunk@2902 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Thu Jun 14 08:11:24 -0700 2007
commit  ac57a508d1ccbd05cdd1737eeab79148f05136a3
tree    8ef5bf9fe6ed3c62437d3ab2a7045b44c683ead4
parent  687ced52aa5d8484fe6f2ecf0cc253a619e9d704
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 * SVN *
0
 
0
+* Switch to will_paginate plugin [Mislav]
0
+
0
 * unit test fixes for those not using mysql or psql [Mislav]
0
 
0
 * add comment moderation links to the overview [court3nay]
...
14
15
16
17
18
19
20
 
 
21
22
23
...
14
15
16
 
 
 
 
17
18
19
20
21
0
@@ -14,10 +14,8 @@
0
   before_filter :load_sections, :only => [:new, :edit]
0
 
0
   def index
0
- @article_pages = Paginator.new self, site.articles.count(:all, article_options), 30, params[:page]
0
- @articles = site.articles.find(:all, article_options(:order => 'contents.published_at DESC', :select => 'contents.*',
0
- :limit => @article_pages.items_per_page,
0
- :offset => @article_pages.current.offset))
0
+ @articles = site.articles.paginate(article_options(:order => 'contents.published_at DESC', :select => 'contents.*',
0
+ :page => params[:page], :per_page => params[:per_page]))
0
     
0
     @comments = @site.unapproved_comments.count :all, :group => :article, :order => '1 desc'
0
     @sections = site.sections.find(:all)
...
85
86
87
88
 
 
89
90
91
 
 
92
93
94
95
 
96
97
98
...
85
86
87
 
88
89
90
 
 
91
92
93
94
95
 
96
97
98
99
0
@@ -85,14 +85,15 @@
0
 
0
     def search_assets(limit)
0
       @types = params[:filter].blank? ? [] : params[:filter].keys
0
- @asset_pages = Paginator.new self, count_by_conditions, limit, params[:page]
0
+ options = search_options.merge(:per_page => limit, :page => params[:page], :total_entries => count_by_conditions)
0
+
0
       @assets = @types.any? ?
0
- site.assets.find_all_by_content_types(@types, :all, search_options) :
0
- site.assets.find(:all, search_options)
0
+ site.assets.paginate_by_content_types(@types, :all, options) :
0
+ site.assets.paginate(options)
0
     end
0
 
0
     def search_options
0
- search_conditions.merge(:order => 'created_at desc', :limit => @asset_pages.items_per_page, :offset => @asset_pages.current.offset)
0
+ search_conditions.merge(:order => 'created_at desc')
0
     end
0
 
0
     def search_conditions
...
3
4
5
6
7
8
9
 
 
 
10
11
12
...
3
4
5
 
 
 
 
6
7
8
9
10
11
0
@@ -3,10 +3,9 @@
0
 
0
   def index
0
     CachedPage.with_current_scope do
0
- @cached_page_pages = Paginator.new self, site.cached_pages.count, 30, params[:page]
0
- offset = (((params[:page] || 1).to_i - 1) * @cached_page_pages.items_per_page)
0
- @cached_pages = site.cached_pages.find(:all, :order => 'updated_at', :limit => @cached_page_pages.items_per_page, :offset => offset,
0
- :conditions => (params[:query] && ['url LIKE ?', ["#{params[:query]}%"]]))
0
+ @cached_pages = site.cached_pages.paginate(:order => 'updated_at',
0
+ :conditions => (params[:query] && ['url LIKE ?', ["#{params[:query]}%"]]),
0
+ :page => params[:page])
0
     end
0
   end
0
   alias_method :query, :index
...
83
84
85
86
87
88
89
90
91
92
93
 
 
 
 
94
95
96
97
 
 
98
99
 
100
101
102
...
83
84
85
 
86
 
 
 
 
 
 
87
88
89
90
91
92
 
 
93
94
95
 
96
97
98
99
0
@@ -83,20 +83,17 @@
0
       if @section
0
         conditions.first << ' AND (assigned_sections.section_id = :section)'
0
         conditions.last[:section] = @section.id
0
- joins = "INNER JOIN assigned_sections ON assigned_sections.article_id = contents.id"
0
       end
0
- search_count = site.articles.count(:all, :conditions => conditions, :joins => joins)
0
- @article_pages = Paginator.new self, search_count, site.articles_per_page, params[:page]
0
- @articles = site.articles.find(:all, :conditions => conditions, :order => 'published_at DESC',
0
- :include => [:user, :sections],
0
- :limit => @article_pages.items_per_page,
0
- :offset => @article_pages.current.offset)
0
+
0
+ @articles = site.articles.paginate(:conditions => conditions, :order => 'published_at DESC',
0
+ :include => [:user, :sections],
0
+ :per_page => site.articles_per_page, :page => params[:page])
0
       
0
       render_liquid_template_for(:search, 'articles' => @articles,
0
- 'previous_page' => paged_search_url_for(@article_pages.current.previous),
0
- 'next_page' => paged_search_url_for(@article_pages.current.next),
0
+ 'previous_page' => paged_search_url_for(@articles.previous_page),
0
+ 'next_page' => paged_search_url_for(@articles.next_page),
0
                                           'search_string' => CGI::escapeHTML(params[:q]),
0
- 'search_count' => search_count,
0
+ 'search_count' => @articles.total_entries,
0
                                           'section' => @section)
0
       @skip_caching = true
0
     end
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
1
 
 
 
 
 
 
 
 
 
 
2
3
4
0
@@ -1,14 +1,4 @@
0
 module Admin::CachedPagesHelper
0
- def pagination_remote_links(paginator, options={}, html_options={})
0
- name = options[:name] || ActionController::Pagination::DEFAULT_OPTIONS[:name]
0
- params = (options[:params] || ActionController::Pagination::DEFAULT_OPTIONS[:params]).clone
0
-
0
- pagination_links_each(paginator, options) do |n|
0
- params[name] = n
0
- link_to n.to_s, :page => n
0
- end
0
- end
0
-
0
   def display_cached_page_date(page)
0
     if Date.today == page.updated_at.to_date
0
       if page.updated_at > Time.now - 4.hours
...
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
...
52
53
54
 
 
 
 
 
 
 
 
 
 
55
56
57
0
@@ -52,16 +52,6 @@
0
     end
0
   end
0
 
0
- def pagination_remote_links(paginator, options={}, html_options={})
0
- name = options[:name] || ActionController::Pagination::DEFAULT_OPTIONS[:name]
0
- params = (options[:params] || ActionController::Pagination::DEFAULT_OPTIONS[:params]).clone
0
-
0
- pagination_links_each(paginator, options) do |n|
0
- params[name] = n
0
- link_to_function n.to_s, "window.spotlight.search('#{n}')"
0
- end
0
- end
0
-
0
   def comment_expiration_options
0
     [['Are not allowed', -1],
0
      ['Never expire', 0],
...
37
38
39
 
 
 
 
 
40
41
42
43
44
...
54
55
56
57
58
59
60
61
 
 
62
63
64
65
66
...
37
38
39
40
41
42
43
44
45
46
47
48
49
...
59
60
61
 
62
 
 
 
63
64
65
 
66
67
68
0
@@ -37,6 +37,11 @@
0
 <% end %>
0
 
0
 <% if @articles.any? %>
0
+
0
+<% content_tag :p, :class => 'total' do %>
0
+ Total: <%= content_tag :strong, @articles.total_entries %> articles.
0
+<% end %>
0
+
0
 <!-- begin article list -->
0
 <table id="article-list" cellspacing="0" cellpadding="0">
0
   <thead>
0
0
0
@@ -54,13 +59,10 @@
0
 </table>
0
 <!-- /end article list -->
0
 
0
-
0
 <!-- begin pagination -->
0
-<div id="pagination">
0
-<% if @article_pages.page_count > 1 -%>
0
-Pages: <strong><%= pagination_links @article_pages, :params => { :action => 'index' } %></strong>
0
+<% if @articles.page_count > 1 -%>
0
+ <%= will_paginate @articles, :id => 'pagination' %>
0
 <% end -%>
0
-</div>
0
 <!-- /end pagination -->
0
 
0
 <% else %>
...
27
28
29
30
31
32
33
 
 
34
...
27
28
29
 
 
 
 
30
31
32
0
@@ -27,9 +27,7 @@
0
     <div class="empty" style="margin: 20px;">You have no files of this type or have yet to upload any files. <%= link_to 'Upload one now &raquo;', :action => 'new' %></div>
0
   <% end %>
0
 
0
-<% if @asset_pages.page_count > 1 %>
0
- <div id="pagination">
0
- Pages: <strong><%= pagination_remote_links @asset_pages, :params => { :action => 'index' } %></strong>
0
- </div>
0
+<% if @assets.page_count > 1 %>
0
+ <%= will_paginate @assets, :id => 'pagination' %>
0
 <% end %>
...
2
3
4
5
 
6
7
8
...
111
112
113
 
 
 
 
 
 
 
 
 
114
115
116
...
2
3
4
 
5
6
7
8
...
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
0
@@ -2,7 +2,7 @@
0
 # migrations feature of ActiveRecord to incrementally modify your database, and
0
 # then regenerate this schema definition.
0
 
0
-ActiveRecord::Schema.define(:version => 71) do
0
+ActiveRecord::Schema.define(:version => 68) do
0
 
0
   create_table "assets", :force => true do |t|
0
     t.column "content_type", :string
0
@@ -111,6 +111,15 @@
0
     t.column "author", :string, :limit => 100
0
     t.column "comment_id", :integer
0
     t.column "site_id", :integer
0
+ end
0
+
0
+ create_table "feedbacks", :force => true do |t|
0
+ t.column "site_id", :integer
0
+ t.column "name", :string
0
+ t.column "email", :string
0
+ t.column "body", :text
0
+ t.column "key", :string
0
+ t.column "created_at", :datetime
0
   end
0
 
0
   create_table "memberships", :force => true do |t|
...
1199
1200
1201
 
 
 
 
 
 
 
 
1202
1203
1204
...
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
0
@@ -1199,6 +1199,14 @@
0
   text-align: center;
0
   border-top: 1px solid #ccc;
0
   padding: 10px 0;
0
+ font-weight:bold;
0
+}
0
+
0
+#main p.total {
0
+ margin:-0.3em 0 1.2em 0;
0
+ padding: 0.3em 3em;
0
+ background: #eee;
0
+ text-align:center;
0
 }
0
 
0
 
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
0
@@ -1 +1,137 @@
0
+WillPaginate
0
+===========
0
+
0
+Quick quiz: Where does pagination logic belong?
0
+
0
+ a) in the model;
0
+ b) in the controller;
0
+ c) in views;
0
+ d) all of the above.
0
+
0
+We think you know the answer (if you think hard enough).
0
+
0
+This plugin makes magic happen. You *will* paginate!
0
+
0
+
0
+## Example usage:
0
+
0
+Use a paginate finder in the controller:
0
+
0
+ @posts = Post.paginate_by_board_id @board.id, :page => params[:page]
0
+
0
+Yeah, `paginate` works just like `find` -- it just doesn't fetch all the records.
0
+Just don't forget to tell it which page you want!
0
+
0
+Render the posts in your view like you would normally do. When you need to render
0
+pagination, just stick this in:
0
+
0
+ <%= will_paginate @posts %>
0
+
0
+You're done. (Copy and paste the example fancy CSS styles from the bottom.)
0
+
0
+How does it know how much items to fetch per page? It asks your model by calling
0
+`Post.per_page`. You can define it like this:
0
+
0
+ class Post < ActiveRecord::Base
0
+ cattr_reader :per_page
0
+ @@per_page = 50
0
+ end
0
+
0
+... or like this:
0
+
0
+ class Post < ActiveRecord::Base
0
+ def self.per_page
0
+ 50
0
+ end
0
+ end
0
+
0
+... or don't worry about it at all. (WillPaginate defines it to be 30 if missing.)
0
+You can also specify the count explicitly when calling `paginate`:
0
+
0
+ @posts = Post.paginate :page => params[:page], :per_page => 50
0
+
0
+Find more options in sections below.
0
+
0
+
0
+## Details
0
+
0
+The `paginate` finder wraps the original finder and returns a PaginatedCollection
0
+instance that's in fact a proxy to the original collection. You can use the collection
0
+as you would any ActiveRecord resultset, but WillPaginate view helpers also need the
0
+object to know how to make pagination:
0
+
0
+ <ol>
0
+ <% for post in @posts -%>
0
+ <li>Render `post` in some nice way.</li>
0
+ <% end -%>
0
+ </ol>
0
+
0
+ <p>Now let's render us some pagination!</p>
0
+ <%= will_paginate @posts %>
0
+
0
+
0
+In model finders, "all" is implicit. No sense in paginating a single record, right?
0
+
0
+ * Post.paginate => Post.find :all
0
+ * Post.paginate_all_by_something => Post.find_all_by_something
0
+ * Post.paginate_by_something => Post.find_all_by_something
0
+
0
+Options for `paginate` finders are:
0
+
0
+ * page (default 1)
0
+ * per_page (default is read from the model, which is 30 if not overriden)
0
+ * total entries: ActiveRecord knows how to count, but you can still override it
0
+ * count: takes place of "select" for count() statement
0
+ * distinct: also just for count() statement
0
+
0
+Options for `will_paginate` view helper:
0
+
0
+ * class: CSS class name for the generated DIV (default "pagination")
0
+ * prev_label: default '&laquo; Previous',
0
+ * next_label: default 'Next &raquo;',
0
+ * inner_window: how many links are shown around the current page, defaults to 4
0
+ * outer_window: how many links are around the first and the last page, defaults to 1
0
+
0
+
0
+## Authors, credits
0
+
0
+Ruby port by: PJ Hyett, Mislav Marohnić (Sulien)
0
+Contributors: K. Adam Christensen, Chris Wanstrath, Dr. Nic Williams
0
+Original announcement: http://errtheblog.com/post/929
0
+Original PHP source: http://www.strangerstudios.com/sandbox/pagination/diggstyle.php
0
+
0
+
0
+## Want Digg style?
0
+
0
+Copy the following css into your stylesheet for a good start:
0
+
0
+.pagination {
0
+ padding: 3px;
0
+ margin: 3px;
0
+}
0
+.pagination a {
0
+ padding: 2px 5px 2px 5px;
0
+ margin: 2px;
0
+ border: 1px solid #aaaadd;
0
+ text-decoration: none;
0
+ color: #000099;
0
+}
0
+.pagination a:hover, .pagination a:active {
0
+ border: 1px solid #000099;
0
+ color: #000;
0
+}
0
+.pagination span.current {
0
+ padding: 2px 5px 2px 5px;
0
+ margin: 2px;
0
+ border: 1px solid #000099;
0
+ font-weight: bold;
0
+ background-color: #000099;
0
+ color: #FFF;
0
+}
0
+.pagination span.disabled {
0
+ padding: 2px 5px 2px 5px;
0
+ margin: 2px;
0
+ border: 1px solid #eee;
0
+ color: #ddd;
0
+}
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
0
@@ -1 +1,23 @@
0
+require 'rake'
0
+require 'rake/testtask'
0
+require 'rake/rdoctask'
0
+
0
+desc 'Default: run unit tests.'
0
+task :default => :test
0
+
0
+desc 'Test the dummy plugin.'
0
+Rake::TestTask.new(:test) do |t|
0
+ t.libs << 'lib'
0
+ t.pattern = 'test/**/*_test.rb'
0
+ t.verbose = true
0
+end
0
+
0
+desc 'Generate documentation for the dummy plugin.'
0
+Rake::RDocTask.new(:rdoc) do |rdoc|
0
+ rdoc.rdoc_dir = 'rdoc'
0
+ rdoc.title = 'WillPaginate'
0
+ rdoc.options << '--line-numbers' << '--inline-source'
0
+ rdoc.rdoc_files.include('README')
0
+ rdoc.rdoc_files.include('lib/**/*.rb')
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
0
@@ -1 +1,18 @@
0
+ActiveRecord::Base.send :include, WillPaginate::Finder
0
+ActionView::Base.send :include, WillPaginate::ViewHelpers
0
+
0
+unless Hash.instance_methods.include? 'slice'
0
+ Hash.class_eval do
0
+ # Returns a new hash with only the given keys.
0
+ def slice(*keys)
0
+ allowed = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys)
0
+ reject { |key,| !allowed.include?(key) }
0
+ end
0
+
0
+ # Replaces the hash with only the given keys.
0
+ def slice!(*keys)
0
+ replace(slice(*keys))
0
+ end
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
0
@@ -1 +1,32 @@
0
+module WillPaginate
0
+ # Arrays returned from paginating finds are, in fact, instances of this.
0
+ #
0
+ class Collection < Array
0
+ attr_reader :current_page, :per_page
0
+ attr_accessor :total_entries
0
+
0
+ def initialize(page, per_page, total)
0
+ @current_page = page.to_i
0
+ @per_page = per_page.to_i
0
+ @total_entries = total.to_i
0
+ @total_pages = (@total_entries / @per_page.to_f).ceil
0
+ end
0
+
0
+ def page_count
0
+ @total_pages
0
+ end
0
+
0
+ def offset
0
+ (current_page - 1) * per_page
0
+ end
0
+
0
+ def previous_page
0
+ current_page > 1 ? (current_page - 1) : nil
0
+ end
0
+
0
+ def next_page
0
+ current_page < page_count ? (current_page + 1) : nil
0
+ end
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
0
@@ -1 +1,72 @@
0
+module WillPaginate
0
+ # A mixin for ActiveRecord::Base. Provides `per_page` class method
0
+ # and makes `paginate` finders possible with some method_missing magic.
0
+ #
0
+ module Finder
0
+ def self.included(base)
0
+ base.extend ClassMethods
0
+ class << base
0
+ alias_method_chain :method_missing, :paginate
0
+ define_method(:per_page) { 30 } unless respond_to? :per_page
0
+ end
0
+ end
0
+
0
+ module ClassMethods
0
+ def method_missing_with_paginate(method, *args, &block)
0
+ # did somebody tried to paginate? if not, let them be
0
+ unless method.to_s.index('paginate') == 0
0
+ return method_missing_without_paginate(method, *args, &block)
0
+ end
0
+ options = args.last.is_a?(Hash) ? args.pop.symbolize_keys : {}
0
+
0
+ # :total_entries and :count are mutually exclusive
0
+ total_entries = unless options[:total_entries]
0
+ count_options = options.slice :conditions, :joins, :include, :group, :distinct
0
+ # merge the hash found in :count
0
+ # this allows you to specify :select, :order, or anything else just for the count query
0
+ count_options.update(options.delete(:count)) if options[:count]
0
+ count = count(count_options)
0
+
0
+ count.respond_to?(:length) ? count.length : count
0
+ else
0
+ options.delete(:total_entries)
0
+ end
0
+
0
+ finder = method.to_s.sub /^paginate/, 'find'
0
+ # :all is implicit
0
+ if finder == 'find'
0
+ args.unshift(:all) if args.length < 2
0
+ elsif finder.index('find_all') != 0
0
+ finder.sub! /^find/, 'find_all'
0
+ end
0
+
0
+ returning WillPaginate::Collection.new(
0
+ (options.delete(:page) || 1),
0
+ (options.delete(:per_page) || per_page),
0
+ total_entries
0
+ ) do |pager|
0
+ args << options.merge(:offset => pager.offset, :limit => pager.per_page)
0
+ pager.replace(send(finder, *args))
0
+ end
0
+ end
0
+
0
+ def respond_to? method
0
+ case method.to_sym
0
+ when :paginate, :paginate_by_sql
0
+ true
0
+ else
0
+ super method.to_s.sub(/^paginate/, 'find')
0
+ end
0
+ end
0
+ end
0
+ end
0
+
0
+ # TODO: Controllers need love, too!
0
+ #
0
+ # module ControllerHelpers
0
+ # def totally_awesome_helper
0
+ # patience
0
+ # end
0
+ # end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
0
@@ -1 +1,65 @@
0
+module WillPaginate
0
+ module ViewHelpers
0
+ # single space is friendly to spiders and non-graphic browsers
0
+ PAGE_SEPARATOR = ' '
0
+
0
+ # Renders Digg-style pagination. (We know you wanna!)
0
+ # Returns nil if there is only one page in total (can't paginate that).
0
+ #
0
+ def will_paginate entries = @entries, options = {}
0
+ total_pages = entries.page_count
0
+
0
+ if total_pages > 1
0
+ page = entries.current_page
0
+ options = options.symbolize_keys.reverse_merge :class => 'pagination',
0
+ :prev_label => '&laquo; Previous',
0
+ :next_label => 'Next &raquo;',
0
+ :inner_window => 4, # links around the current page
0
+ :outer_window => 1 # links around beginning and end
0
+
0
+ inner_window, outer_window = options.delete(:inner_window).to_i, options.delete(:outer_window).to_i
0
+ min = page - inner_window
0
+ max = page + inner_window
0
+
0
+ # adjust lower or upper limit if other is out of bounds
0
+ if max > total_pages then min -= max - total_pages
0
+ elsif min < 1 then max += 1 - min
0
+ end
0
+
0
+ current = min..max
0
+ beginning = 1..(1 + outer_window)
0
+ tail = (total_pages - outer_window)..total_pages
0
+ visible = [current, beginning, tail].map(&:to_a).sum
0
+
0
+ # build the list of the links
0
+ links = (1..total_pages).inject([]) do |list, n|
0
+ if visible.include? n
0
+ list << page_link_or_span((n != page ? n : nil), 'current', n)
0
+ elsif n == beginning.last + 1 || n == tail.first - 1
0
+ # ellipsis represents the gap between windows
0
+ list << '...'
0
+ end
0
+ list
0
+ end
0
+
0
+ # next and previous buttons
0
+ links.unshift page_link_or_span(entries.previous_page, 'disabled', options.delete(:prev_label))
0
+ links.push page_link_or_span(entries.next_page, 'disabled', options.delete(:next_label))
0
+
0
+ content_tag :div, links.join(options.delete(:separator) || PAGE_SEPARATOR), options
0
+ end
0
+ end
0
+
0
+ protected
0
+
0
+ def page_link_or_span(page, span_class = nil, text = page.to_s)
0
+ unless page
0
+ content_tag :span, text, :class => span_class
0
+ else
0
+ # page links should preserve GET parameters, so we merge params
0
+ link_to text, params.merge(:page => (page !=1 ? page : nil))
0
+ end
0
+ end
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
0
@@ -1 +1,108 @@
0
+require File.dirname(__FILE__) + '/helper'
0
+require File.dirname(__FILE__) + '/../init'
0
+
0
+class PaginationTest < ActiveRecordTestCase
0
+ fixtures :topics, :replies, :developers, :projects, :developers_projects
0
+
0
+ def test_new_methods_presence
0
+ assert_respond_to_all Topic, %w(per_page paginate paginate_by_sql)
0
+ end
0
+
0
+ def test_paginated_collection
0
+ entries = %w(a b c)
0
+ collection = WillPaginate::Collection.new 2, 3, 10
0
+ collection.replace entries
0
+
0
+ assert_equal entries, collection
0
+ assert_respond_to_all collection, %w(page_count each offset size current_page per_page total_entries)
0
+ assert_equal Array, collection.entries.class
0
+ assert_equal 3, collection.offset
0
+ end
0
+
0
+ def test_simple_paginate
0
+ entries = Topic.paginate
0
+ assert_equal 1, entries.current_page
0
+ assert_nil entries.previous_page
0
+ assert_nil entries.next_page
0
+ assert_equal 1, entries.page_count
0
+ assert_equal 3, entries.size
0
+
0
+ entries = Topic.paginate :page => 2
0
+ assert_equal 2, entries.current_page
0
+ assert_equal 1, entries.previo