Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

CODEBASE-335: replace will_paginate with kaminari

  • Loading branch information...
commit 289dc85977fb8861dbc34d7dd7bb25b2cbde1cb9 2 parents 0a5418b + ce42780
Chris Beer authored
2  app/controllers/bookmarks_controller.rb
@@ -34,7 +34,7 @@ def update
34 34 end
35 35
36 36 def index
37   - @bookmarks = current_user.bookmarks.paginate :page => params[:page]
  37 + @bookmarks = current_user.bookmarks.page(params[:page])
38 38 end
39 39
40 40 # For adding a single bookmark, suggest use PUT/#update to
2  app/controllers/folder_controller.rb
... ... @@ -1,6 +1,6 @@
1 1 class FolderController < ApplicationController
2   -
3 2 include Blacklight::SolrHelper
  3 + helper CatalogHelper
4 4
5 5 # fetch the documents that match the ids in the folder
6 6 def index
38 app/helpers/catalog_helper.rb
... ... @@ -1,33 +1,37 @@
1 1 module CatalogHelper
  2 +
  3 + def paginate_rsolr_response(response, options = {}, &block)
  4 + per_page = response.rows
  5 + per_page = 1 if per_page < 1
  6 + current_page = (response.start / per_page).ceil + 1
  7 + num_pages = (response.total / per_page.to_f).ceil
  8 +
  9 + paginate Struct.new(:current_page, :num_pages, :limit_value).new(current_page, num_pages, per_page), options, &block
  10 + end
  11 +
2 12 #
3 13 # shortcut for built-in Rails helper, "number_with_delimiter"
4 14 #
5 15 def format_num(num); number_with_delimiter(num) end
6 16
7 17 #
8   - # Displays the "showing X through Y of N" message. Not sure
9   - # why that's called "page_entries_info". Not entirely sure
10   - # what collection argument is supposed to duck-type too, but
11   - # an RSolr::Ext::Response works. Perhaps it duck-types to something
12   - # from will_paginate?
13   - def page_entries_info(collection, options = {})
14   - start = (collection.current_page - 1) * collection.per_page + 1
15   -
16   - # actual WillPaginate::Collection's have #total_entries. RSolr::Ext::Response
17   - # has #total instead. We want this to work for both, to do what we want
18   - # for RSolr, but not break WillPaginate's usual use.
19   - total_hits = collection.respond_to?(:total_entries) ? collection.total_entries : collection.total
  18 + # Displays the "showing X through Y of N" message.
  19 + def render_pagination_info(response, options = {})
  20 + start = response.start + 1
  21 + per_page = response.rows
  22 + current_page = (response.start / per_page).ceil + 1
  23 + num_pages = (response.total / per_page.to_f).ceil
  24 + total_hits = response.total
20 25
21 26 start_num = format_num(start)
22   - end_num = format_num(start + collection.size - 1)
  27 + end_num = format_num(start + response.docs.length - 1)
23 28 total_num = format_num(total_hits)
24   - # end_num = total_num if total_hits < (start + collection.per_page - 1)
25 29
26 30 entry_name = options[:entry_name] ||
27   - (collection.empty?? 'entry' : collection.first.class.name.underscore.sub('_', ' '))
  31 + (response.empty?? 'entry' : response.docs.first.class.name.underscore.sub('_', ' '))
28 32
29   - if collection.total_pages < 2
30   - case collection.size
  33 + if num_pages < 2
  34 + case response.docs.length
31 35 when 0; "No #{h(entry_name.pluralize)} found".html_safe
32 36 when 1; "Displaying <b>1</b> #{h(entry_name)}".html_safe
33 37 else; "Displaying <b>all #{total_num}</b> #{entry_name.pluralize}".html_safe
2  app/views/bookmarks/index.html.erb
@@ -10,7 +10,7 @@
10 10
11 11 <%- else -%>
12 12
13   - <%= will_paginate @bookmarks %>
  13 +<%= paginate @bookmarks, :theme => 'blacklight' %>
14 14 <div id="userBookmarks">
15 15 <p>
16 16 <%= link_to "Clear Bookmarks", clear_bookmarks_path, :method => :delete, :confirm => "Clear your bookmarks?" %>
9 app/views/catalog/_results_pagination.html.erb
... ... @@ -1,11 +1,6 @@
1   - <% if @response.docs.total_pages > 1 %>
  1 + <% if @response.total > 1 %>
2 2 <div class="pagination">
3   - <% # will_paginate is called oddly because we want next/prev together
4   - # at left for screen reader accessibility. We use CSS to restore
5   - # a more conventional next-on-right.
6   - %>
7 3 <h2>Results navigation</h2>
8   - <%= will_paginate @response.docs, :page_links => false, :container => false, :renderer => Blacklight::CommaLinkRenderer %>
9   - <%= will_paginate @response.docs, :next_label => '', :previous_label => '', :class => "page_links", :renderer => Blacklight::CommaLinkRenderer %>
  4 + <%= paginate_rsolr_response @response, :theme => 'blacklight' %>
10 5 </div>
11 6 <% end %>
2  app/views/catalog/index.html.erb
@@ -15,7 +15,7 @@
15 15 <%= render 'constraints', :localized_params=>params %>
16 16
17 17 <div class="pageEntriesInfo">
18   - <%= (page_entries_info @response.docs, :entry_name=>'item').html_safe %>
  18 + <%= render_pagination_info @response, :entry_name=>'item' %>
19 19 <%=link_to "start over", catalog_index_path, :id=>"startOverLink" %>
20 20 </div>
21 21
11 app/views/kaminari/blacklight/_first_page.html.erb
... ... @@ -0,0 +1,11 @@
  1 +<%# Link to the "First" page
  2 + - available local variables
  3 + url: url to the first page
  4 + current_page: a page object for the currently displayed page
  5 + num_pages: total number of pages
  6 + per_page: number of items to fetch per page
  7 + remote: data-remote
  8 +-%>
  9 +<span class="first">
  10 + <%= link_to_unless current_page.first?, raw(t 'views.pagination.first'), url, :remote => remote %>
  11 +</span>
8 app/views/kaminari/blacklight/_gap.html.erb
... ... @@ -0,0 +1,8 @@
  1 +<%# Non-link tag that stands for skipped pages...
  2 + - available local variables
  3 + current_page: a page object for the currently displayed page
  4 + num_pages: total number of pages
  5 + per_page: number of items to fetch per page
  6 + remote: data-remote
  7 +-%>
  8 +<span class="page gap"><%= raw(t 'views.pagination.truncate') %></span>
11 app/views/kaminari/blacklight/_last_page.html.erb
... ... @@ -0,0 +1,11 @@
  1 +<%# Link to the "Last" page
  2 + - available local variables
  3 + url: url to the last page
  4 + current_page: a page object for the currently displayed page
  5 + num_pages: total number of pages
  6 + per_page: number of items to fetch per page
  7 + remote: data-remote
  8 +-%>
  9 +<span class="last">
  10 + <%= link_to_unless current_page.last?, raw(t 'views.pagination.last'), url, {:remote => remote} %>
  11 +</span>
11 app/views/kaminari/blacklight/_next_page.html.erb
... ... @@ -0,0 +1,11 @@
  1 +<%# Link to the "Next" page
  2 + - available local variables
  3 + url: url to the next page
  4 + current_page: a page object for the currently displayed page
  5 + num_pages: total number of pages
  6 + per_page: number of items to fetch per page
  7 + remote: data-remote
  8 +-%>
  9 +<span class="next next_page <%= 'disabled' if current_page.last? %>">
  10 + <%= link_to_unless current_page.last?, raw(t 'views.pagination.next'), url, :rel => 'next', :remote => remote %>
  11 +</span>
12 app/views/kaminari/blacklight/_page.html.erb
... ... @@ -0,0 +1,12 @@
  1 +<%# Link showing page number
  2 + - available local variables
  3 + page: a page object for "this" page
  4 + url: url to this page
  5 + current_page: a page object for the currently displayed page
  6 + num_pages: total number of pages
  7 + per_page: number of items to fetch per page
  8 + remote: data-remote
  9 +-%>
  10 +<span class="page<%= ' current' if page.current? %>">
  11 + <%= link_to_unless page.current?, page, url, opts = {:remote => remote, :rel => page.next? ? 'next' : page.prev? ? 'prev' : nil} %>
  12 +</span>
23 app/views/kaminari/blacklight/_paginator.html.erb
... ... @@ -0,0 +1,23 @@
  1 +<%# The container tag
  2 + - available local variables
  3 + current_page: a page object for the currently displayed page
  4 + num_pages: total number of pages
  5 + per_page: number of items to fetch per page
  6 + remote: data-remote
  7 + paginator: the paginator that renders the pagination tags inside
  8 +-%>
  9 +<%= paginator.render do -%>
  10 + <nav class="pagination-container">
  11 + <%= prev_page_tag %>
  12 + <%= next_page_tag %>
  13 + <div class="page_links">
  14 + <% each_page do |page| -%>
  15 + <% if page.left_outer? || page.right_outer? || page.inside_window? -%>
  16 + <%= page_tag page %>
  17 + <% elsif !page.was_truncated? -%>
  18 + <%= gap_tag %>
  19 + <% end -%>
  20 + <% end -%>
  21 + </div>
  22 + </nav>
  23 +<% end -%>
11 app/views/kaminari/blacklight/_prev_page.html.erb
... ... @@ -0,0 +1,11 @@
  1 +<%# Link to the "Previous" page
  2 + - available local variables
  3 + url: url to the previous page
  4 + current_page: a page object for the currently displayed page
  5 + num_pages: total number of pages
  6 + per_page: number of items to fetch per page
  7 + remote: data-remote
  8 +-%>
  9 +<span class="prev prev_page <%= 'disabled' if current_page.first? %>">
  10 + <%= link_to_unless current_page.first?, raw(t 'views.pagination.previous'), url, :rel => 'prev', :remote => remote %>
  11 +</span>
7 blacklight.gemspec
@@ -31,10 +31,9 @@ Gem::Specification.new do |s|
31 31 s.add_dependency "rails", "~> 3.0"
32 32 s.add_dependency "nokogiri", "~>1.5.0.beta.3" # XML Parser
33 33 s.add_dependency "marc", "~> 0.4.3" # Marc record parser
34   - s.add_dependency "rsolr", '~>1.0'
35   - s.add_dependency "rsolr-ext", '~>1.0' # extension to the above for some rails-ish behaviors - currently embedded in our solr document ojbect.
36   - s.add_dependency "will_paginate", "~> 3.0.pre2" # the pagination (page 1,2,3, etc..) of our search results
37   -
  34 + s.add_dependency "rsolr", '~> 1.0' # Library for interacting with rSolr.
  35 + s.add_dependency "rsolr-ext", '~> 1.0' # extension to the above for some rails-ish behaviors - currently embedded in our solr document ojbect.
  36 + s.add_dependency "kaminari" # the pagination (page 1,2,3, etc..) of our search results
38 37
39 38 # TESTING GEM REQUIREMENTS
40 39 # -------------------------------------------
10 config/locales/kaminari.yml
... ... @@ -0,0 +1,10 @@
  1 +# Sample localization file for Kaminari. You can override these values in your app's locales file if you want.
  2 +
  3 +en:
  4 + views:
  5 + pagination:
  6 + first: "&laquo; First"
  7 + last: "Last &raquo;"
  8 + previous: "&laquo; Previous"
  9 + next: "Next &raquo;"
  10 + truncate: "…"
2  lib/blacklight.rb
... ... @@ -1,4 +1,4 @@
1   -require 'will_paginate'
  1 +require 'kaminari'
2 2 require 'marc'
3 3 require 'rsolr'
4 4 require 'rsolr-ext'
2  lib/blacklight/engine.rb
@@ -5,6 +5,8 @@
5 5 module Blackight
6 6 class Engine < Rails::Engine
7 7
  8 + config.paths["config/locales"] = "config/locales"
  9 +
8 10 # BlacklightHelper is needed by all helpers, so we inject it
9 11 # into action view base here.
10 12 initializer 'blacklight.helpers' do |app|
40 test_support/spec/helpers/catalog_helper_spec.rb
@@ -2,65 +2,69 @@
2 2 describe CatalogHelper do
3 3 include CatalogHelper
4 4
5   - def mock_collection args
  5 + def mock_response args
6 6 current_page = args[:current_page] || 1
7 7 per_page = args[:per_page] || 10
8 8 total = args[:total]
9   - arr = (1..total).to_a
  9 + start = (current_page - 1) * per_page
10 10
11   - page_results = WillPaginate::Collection.create(current_page, per_page, total) do |pager|
12   - pager.replace(arr.slice(pager.offset, pager.per_page))
13   - end
  11 + mock_response = mock("RSolr::Ext::Response")
  12 + mock_response.stub!(:total).and_return(total)
  13 + mock_response.stub!(:rows).and_return(per_page)
  14 + mock_response.stub!(:start).and_return(start)
  15 + mock_response.stub!(:docs).and_return((1..total).to_a.slice(start, per_page))
  16 +
  17 + mock_response
14 18 end
15 19
16   - describe "page_entries_info" do
  20 + describe "render_pagination_info" do
17 21 before(:all) do
18 22 end
19 23
20 24 it "with no results" do
21   - @collection = mock_collection :total => 0
  25 + @response = mock_response :total => 0
22 26
23   - html = page_entries_info(@collection, { :entry_name => 'entry_name' })
  27 + html = render_pagination_info(@response, { :entry_name => 'entry_name' })
24 28 html.should == "No entry_names found"
25 29 html.html_safe?.should == true
26 30 end
27 31
28 32 it "with a single result" do
29   - @collection = mock_collection :total => 1
  33 + @response = mock_response :total => 1
30 34
31   - html = page_entries_info(@collection, { :entry_name => 'entry_name' })
  35 + html = render_pagination_info(@response, { :entry_name => 'entry_name' })
32 36 html.should == "Displaying <b>1</b> entry_name"
33 37 html.html_safe?.should == true
34 38 end
35 39
36 40 it "with a single page of results" do
37   - @collection = mock_collection :total => 7
  41 + @response = mock_response :total => 7
38 42
39   - html = page_entries_info(@collection, { :entry_name => 'entry_name' })
  43 + html = render_pagination_info(@response, { :entry_name => 'entry_name' })
40 44 html.should == "Displaying <b>all 7</b> entry_names"
41 45 html.html_safe?.should == true
42 46 end
43 47
44 48 it "on the first page of multiple pages of results" do
45   - @collection = mock_collection :total => 15, :per_page => 10
  49 + @response = mock_response :total => 15, :per_page => 10
46 50
47   - html = page_entries_info(@collection, { :entry_name => 'entry_name' })
  51 + html = render_pagination_info(@response, { :entry_name => 'entry_name' })
48 52 html.should == "Displaying entry_names <b>1 - 10</b> of <b>15</b>"
49 53 html.html_safe?.should == true
50 54 end
51 55
52 56 it "on the second page of multiple pages of results" do
53   - @collection = mock_collection :total => 47, :per_page => 10, :current_page => 2
  57 + @response = mock_response :total => 47, :per_page => 10, :current_page => 2
54 58
55   - html = page_entries_info(@collection, { :entry_name => 'entry_name' })
  59 + html = render_pagination_info(@response, { :entry_name => 'entry_name' })
56 60 html.should == "Displaying entry_names <b>11 - 20</b> of <b>47</b>"
57 61 html.html_safe?.should == true
58 62 end
59 63
60 64 it "on the last page of results" do
61   - @collection = mock_collection :total => 47, :per_page => 10, :current_page => 5
  65 + @response = mock_response :total => 47, :per_page => 10, :current_page => 5
62 66
63   - html = page_entries_info(@collection, { :entry_name => 'entry_name' })
  67 + html = render_pagination_info(@response, { :entry_name => 'entry_name' })
64 68 html.should == "Displaying entry_names <b>41 - 47</b> of <b>47</b>"
65 69 html.html_safe?.should == true
66 70 end

0 comments on commit 289dc85

Please sign in to comment.
Something went wrong with that request. Please try again.