active_resource_pagination 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/active_resource_pagination.gemspec +1 -1
- data/lib/active_resource_pagination.rb +18 -11
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.5
|
@@ -5,9 +5,9 @@ module ActiveResource
|
|
5
5
|
# This adds pagination support to Active Resource. For example
|
6
6
|
#
|
7
7
|
# Article.paginate
|
8
|
-
# Article.paginate(:
|
9
|
-
# Article.paginate(:
|
10
|
-
# Article.paginate(:
|
8
|
+
# Article.paginate(:page => 2, :per_page => 20)
|
9
|
+
# Article.paginate(:page => 2, :per_page => 20, :total_entries => 123)
|
10
|
+
# Article.paginate(:from => :most_popular,
|
11
11
|
# :params => {:year => 2010, :page => 1, :per_page => 20})
|
12
12
|
#
|
13
13
|
# To set default per_page value for all resources. you can do
|
@@ -31,15 +31,17 @@ module ActiveResource
|
|
31
31
|
# if resource backend doesn't paginate and retursn all result, then this method automatically
|
32
32
|
# sets the total_entry count from the result. Otherwise, you have to pass in the
|
33
33
|
# :total_entries count value manually.
|
34
|
-
def paginate(
|
35
|
-
options
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
34
|
+
def paginate(options={})
|
35
|
+
if options[:params]
|
36
|
+
pg_params = options[:params] = with_default_params(options[:params])
|
37
|
+
else
|
38
|
+
pg_params = options = with_default_params(options)
|
39
|
+
end
|
40
|
+
|
41
|
+
page = pg_params[:page]
|
42
|
+
per_page = pg_params[:per_page]
|
41
43
|
|
42
|
-
arr = find(
|
44
|
+
arr = find(:all, options) || []
|
43
45
|
total_entries = options[:total_entries] || arr.size
|
44
46
|
WillPaginate::Collection.create(page, per_page, total_entries) do |pager|
|
45
47
|
if arr.size > per_page
|
@@ -49,6 +51,11 @@ module ActiveResource
|
|
49
51
|
end
|
50
52
|
end
|
51
53
|
end
|
54
|
+
|
55
|
+
protected
|
56
|
+
def with_default_params(options)
|
57
|
+
{:page => 1, :per_page => per_page}.merge(options.reject{|k, v| v.blank?})
|
58
|
+
end
|
52
59
|
end
|
53
60
|
end
|
54
61
|
end
|