Permalink
Browse files

adding previous_page and next_page to collection to better simulate w…

…ill_paginate
  • Loading branch information...
1 parent b65910c commit 7c854fefec87428165b2795b06ac5903ddbea3dd @ryanb committed Nov 1, 2011
Showing with 13 additions and 1 deletion.
  1. +9 −1 lib/xapit/client/collection.rb
  2. +4 −0 spec/xapit/client/collection_spec.rb
@@ -90,6 +90,14 @@ def current_page
(clause_value(:page) || 1).to_i
end
+ def previous_page
+ current_page - 1 if current_page > 1
+ end
+
+ def next_page
+ current_page + 1 if current_page < num_pages
+ end
+
def limit_value
(clause_value(:per_page) || DEFAULT_PER_PAGE).to_i
end
@@ -148,7 +156,7 @@ def filter_facet_options(options)
end
def clause_value(key)
- clauses.map { |clause| clause[key] }.compact.first
+ clauses.map { |clause| clause[key] }.compact.last
end
def query
@@ -64,6 +64,10 @@
collection = Xapit::Client::Collection.new.page("2").per_page("10")
collection.stub(:total_entries) { 29 }
collection.current_page.should eq(2)
+ collection.previous_page.should eq(1)
+ collection.next_page.should eq(3)
collection.total_pages.should eq(3)
+ collection.page(3).next_page.should be_nil
+ collection.page(1).previous_page.should be_nil
end
end

0 comments on commit 7c854fe

Please sign in to comment.