Permalink
Browse files

CODEBASE-335: replace will_paginate with kaminari

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

0 comments on commit 289dc85

Please sign in to comment.