krunaldo-dm-pagination 0.3.0.0 → 0.3.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +7 -0
- data/lib/dm-pagination/pagination.rb +12 -22
- data/spec/dm-pagination_spec.rb +4 -0
- metadata +1 -1
data/README
CHANGED
@@ -21,3 +21,10 @@ USAGE:
|
|
21
21
|
</ul>
|
22
22
|
<%= paginate @posts %>
|
23
23
|
|
24
|
+
Paramaters:
|
25
|
+
:order = same paramaters as the :order in a normal DM call
|
26
|
+
(http://datamapper.org/doku.php?id=docs:finders&s[]=order#order)
|
27
|
+
|
28
|
+
:per_page = Number of items per page
|
29
|
+
|
30
|
+
:page = What page number
|
@@ -1,40 +1,30 @@
|
|
1
1
|
module DmPagination
|
2
2
|
class Pagination
|
3
|
+
attr_reader :page, :num_pages
|
4
|
+
|
3
5
|
def initialize(collection, options)
|
6
|
+
@proxy_collection = collection
|
7
|
+
order = options[:order]
|
8
|
+
|
4
9
|
@page = (options[:page] || 1).to_i
|
5
10
|
@per_page = (options[:per_page] || 10).to_i
|
6
|
-
@reverse = true if options[:reverse]
|
7
|
-
@proxy_collection = collection
|
8
|
-
@offset = offset
|
9
11
|
@num_pages = (@proxy_collection.count + @per_page - 1) / @per_page
|
10
|
-
@
|
11
|
-
:offset => @offset , :limit => @per_page)
|
12
|
-
end
|
12
|
+
@offset = (@page - 1)*@per_page
|
13
13
|
|
14
|
-
|
15
|
-
@
|
14
|
+
@collection = collection.all(:offset => @offset, :limit => @per_page)
|
15
|
+
@collection = @collection.all(:order => order) if order
|
16
16
|
end
|
17
17
|
|
18
|
-
def offset
|
19
|
-
if @reverse
|
20
|
-
(@page - @numpages)*@per_page
|
21
|
-
else
|
22
|
-
(@page - 1)*@per_page
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
|
27
18
|
def pages(window = 5, left = 2, right = 2)
|
28
|
-
return [] if
|
29
|
-
(1
|
30
|
-
i <= left || (
|
19
|
+
return [] if num_pages <= 1
|
20
|
+
(1..num_pages).inject([]) do |result, i|
|
21
|
+
i <= left || (num_pages - i) < right || (i-page).abs < window ?
|
31
22
|
result << i : (result.last.nil? ? result : result << nil)
|
32
23
|
end
|
33
24
|
end
|
34
25
|
|
35
26
|
def count
|
36
|
-
|
37
|
-
[0, [@proxy_collection.count - offset, @per_page].min].max
|
27
|
+
[0, [@proxy_collection.count - @offset, @per_page].min].max
|
38
28
|
end
|
39
29
|
|
40
30
|
def respond_to?(*args, &block)
|
data/spec/dm-pagination_spec.rb
CHANGED
@@ -50,6 +50,10 @@ describe "dm-pagination" do
|
|
50
50
|
Post.paginate(:page => 12).count.should == 0
|
51
51
|
Post.paginate(:page => 5, :per_page => 6).count.should == 6
|
52
52
|
end
|
53
|
+
|
54
|
+
it "should handle :order parametetr" do
|
55
|
+
Post.paginate(:page => 1, :order => [:id.desc]).first.id.should == 101
|
56
|
+
end
|
53
57
|
end
|
54
58
|
|
55
59
|
describe "pagination builder" do
|