active_resource_pagination 0.0.5 → 0.0.6

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.
@@ -1,17 +1,35 @@
1
1
  = active_resource_pagination
2
2
 
3
- Description goes here.
3
+ This gem adds pagination support to Active Resource.
4
4
 
5
- == Note on Patches/Pull Requests
6
-
7
- * Fork the project.
8
- * Make your feature addition or bug fix.
9
- * Add tests for it. This is important so I don't break it in a
10
- future version unintentionally.
11
- * Commit, do not mess with rakefile, version, or history.
12
- (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
- * Send me a pull request. Bonus points for topic branches.
5
+ == Sample Usage
6
+ # Article is a resource model
7
+
8
+ Article.paginate
9
+ Article.paginate(:page => 2, :per_page => 20)
10
+ Article.paginate(:page => 2, :per_page => 20, :total_entries => 123)
11
+ Article.paginate(:page => 2, :per_page => 20, :params => {:year => 2010})
14
12
 
15
- == Copyright
16
13
 
17
- Copyright (c) 2010 Dorren Chen. See LICENSE for details.
14
+
15
+ == Configuration
16
+
17
+ To set default per_page value for all resources. you can do
18
+ ActiveResource::Base.per_page = 20 # in config/environment or initializers
19
+
20
+ or to implement per_page() in your resource class.
21
+
22
+
23
+ == Detail
24
+
25
+ When doing the pagination query, it converts :page and :per_page parameters to :offset and :limit to the actual find method, assure your backend honors :offset and :limit parameters.
26
+
27
+ Article.paginate(:page => 2, :per_page => 20, :params => {:year => 2010}) # is translated into 2 request calls
28
+
29
+ Article.find(:all, :params => {:year => 2010, :offset => 40, :limit => 20})
30
+ Article.find(:one, :from => :count, :params => {:year => 2010})
31
+
32
+ * If you pass in the :total_entries parameter, Model.count() will not be called.
33
+ * You can always override count() if default doesn't suit your need.
34
+ * If you don't pass in the :total_entries parameter, Model.count() will be called,
35
+ unless result count > per_page, then this method automatically sets the count from the result.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.0.6
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{active_resource_pagination}
8
- s.version = "0.0.5"
8
+ s.version = "0.0.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Dorren Chen"]
12
- s.date = %q{2010-06-02}
12
+ s.date = %q{2010-06-04}
13
13
  s.description = %q{Adds pagination to Active Resource}
14
14
  s.email = %q{dorrenchen@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -25,7 +25,9 @@ Gem::Specification.new do |s|
25
25
  "VERSION",
26
26
  "active_resource_pagination.gemspec",
27
27
  "lib/active_resource_pagination.rb",
28
+ "lib/hash_ext.rb",
28
29
  "spec/active_resource_pagination_spec.rb",
30
+ "spec/hash_ext_spec.rb",
29
31
  "spec/spec.opts",
30
32
  "spec/spec_helper.rb"
31
33
  ]
@@ -36,6 +38,7 @@ Gem::Specification.new do |s|
36
38
  s.summary = %q{Adds pagination to Active Resource}
37
39
  s.test_files = [
38
40
  "spec/active_resource_pagination_spec.rb",
41
+ "spec/hash_ext_spec.rb",
39
42
  "spec/spec_helper.rb"
40
43
  ]
41
44
 
@@ -1,5 +1,6 @@
1
1
  require 'active_resource'
2
2
  require 'will_paginate'
3
+ require 'hash_ext'
3
4
 
4
5
  module ActiveResource
5
6
  # This adds pagination support to Active Resource. For example
@@ -7,8 +8,8 @@ module ActiveResource
7
8
  # Article.paginate
8
9
  # Article.paginate(:page => 2, :per_page => 20)
9
10
  # Article.paginate(:page => 2, :per_page => 20, :total_entries => 123)
10
- # Article.paginate(:from => :most_popular,
11
- # :params => {:year => 2010, :page => 1, :per_page => 20})
11
+ # Article.paginate(:page => 2, :per_page => 20,
12
+ # :from => :most_popular, :params => {:year => 2010})
12
13
  #
13
14
  # To set default per_page value for all resources. you can do
14
15
  # ActiveResource::Base.per_page = 20 # do this in config/environment or initializers
@@ -22,7 +23,18 @@ module ActiveResource
22
23
  base.extend ClassMethods
23
24
  end
24
25
 
25
- module ClassMethods
26
+ module ClassMethods
27
+ # returns the total_entries count for the paginated result.
28
+ #
29
+ # method expects the returned xml to be in the format of:
30
+ # <?xml version="1.0" encoding="UTF-8"?>
31
+ # <hash>
32
+ # <count type="integer">5</count>
33
+ # </hash>
34
+ def count(options)
35
+ find(:one, :from => :count, :params => options).count.to_i
36
+ end
37
+
26
38
  # use same method signatures as find(), optional additional parameters:
27
39
  # page - current page
28
40
  # per_pape - entries per page
@@ -32,30 +44,28 @@ module ActiveResource
32
44
  # sets the total_entry count from the result. Otherwise, you have to pass in the
33
45
  # :total_entries count value manually.
34
46
  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]
47
+ pg_options, find_options = options.partition{|k,v| [:page, :per_page, :total_entries].include?(k)}
43
48
 
44
- arr = find(:all, options) || []
45
- total_entries = options[:total_entries] || arr.size
46
- WillPaginate::Collection.create(page, per_page, total_entries) do |pager|
49
+ pg_options[:page] ||= 1
50
+ pg_options[:per_page] ||= per_page
51
+
52
+ WillPaginate::Collection.create(pg_options[:page], pg_options[:per_page], pg_options[:total_entries]) do |pager|
53
+ find_options[:params] = (find_options[:params] || {}).merge(:offset => pager.offset, :limit => pager.per_page)
54
+
55
+ arr = find(:all, find_options) || []
56
+ if pg_options[:total_entries]
57
+ pager.total_entries = pg_options[:total_entries]
58
+ else
59
+ pager.total_entries = arr.size > pager.per_page ? arr.size : count(find_options[:params])
60
+ end
61
+
47
62
  if arr.size > per_page
48
63
  pager.replace arr[pager.offset, pager.per_page]
49
64
  else
50
65
  pager.replace arr
51
66
  end
52
67
  end
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
68
+ end
59
69
  end
60
70
  end
61
71
  end
@@ -0,0 +1,11 @@
1
+ class Hash
2
+ # similar to Enumerable partition() method, separate one hash into 2 based on the passed in test condition.
3
+ def partition(&block)
4
+ h = dup
5
+ h2 = {}
6
+ each{|k, v|
7
+ h2[k] = h.delete(k) if block.call(k,v)
8
+ }
9
+ [h2, h]
10
+ end
11
+ end
@@ -53,11 +53,14 @@ describe ActiveResource::Pagination do
53
53
  end
54
54
 
55
55
  describe "when backend does not paginate" do
56
- before(:each) do
57
- Article.should_receive(:find).and_return(@articles)
56
+ before(:each) do
58
57
  end
59
58
 
60
59
  it "should paginate with no params" do
60
+ pg_params = {:offset => 0, :limit => Article.per_page}
61
+ Article.should_receive(:find).with(:all, :params => pg_params).and_return(@articles)
62
+ Article.should_not_receive(:count).with(pg_params) # since articles count > per_page
63
+
61
64
  col = Article.paginate
62
65
  col.current_page.should == 1
63
66
  col.per_page.should == Article.per_page
@@ -66,6 +69,10 @@ describe ActiveResource::Pagination do
66
69
  end
67
70
 
68
71
  it "should paginate with params" do
72
+ pg_params = {:offset => Article.per_page, :limit => Article.per_page}
73
+ Article.should_receive(:find).with(:all, :params => pg_params).and_return(@articles)
74
+ Article.should_not_receive(:count).with(pg_params)
75
+
69
76
  col = Article.paginate(:page => 2)
70
77
  col.current_page.should == 2
71
78
  col.first.should == @articles[3]
@@ -73,9 +80,25 @@ describe ActiveResource::Pagination do
73
80
  end
74
81
 
75
82
  describe "when backend do paginate" do
76
- it "should paginate when backend returns paginated entries" do
83
+ it "should paginate without :total_entries param" do
84
+ @articles = @articles[2,2] # returns 2nd page result
85
+ pg_params = {:offset => Article.per_page, :limit => Article.per_page}
86
+ Article.should_receive(:find).with(:all, :params => pg_params).and_return(@articles)
87
+ Article.should_receive(:count).with(pg_params).and_return(5)
88
+
89
+ col = Article.paginate(:page => 2)
90
+ col.current_page.should == 2
91
+ col.per_page.should == Article.per_page
92
+ col.total_entries.should == 5
93
+ col.total_pages.should == 3 # (col.total_entries / col.per_page.to_f).ceil
94
+ col.first.should == @articles.first
95
+ end
96
+
97
+ it "should paginate with :total_entries param" do
77
98
  @articles = @articles[2,2] # returns 2nd page result
78
- Article.should_receive(:find).and_return(@articles)
99
+ pg_params = {:offset => Article.per_page, :limit => Article.per_page}
100
+ Article.should_receive(:find).with(:all, :params => pg_params).and_return(@articles)
101
+ Article.should_not_receive(:count)
79
102
 
80
103
  col = Article.paginate(:page => 2, :total_entries => 5)
81
104
  col.current_page.should == 2
@@ -0,0 +1,11 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Hash do
4
+ describe "#partition" do
5
+ it "should partition" do
6
+ h1, h2 = {'a' => 1, 'b' => 2, 'c' => 10, 'd' => 20}.partition{|k, v| v >= 10}
7
+ h1.should == {'c' => 10, 'd' => 20}
8
+ h2.should == {'a' => 1, 'b' => 2}
9
+ end
10
+ end
11
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 5
9
- version: 0.0.5
8
+ - 6
9
+ version: 0.0.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - Dorren Chen
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-06-02 00:00:00 -04:00
17
+ date: 2010-06-04 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -49,7 +49,9 @@ files:
49
49
  - VERSION
50
50
  - active_resource_pagination.gemspec
51
51
  - lib/active_resource_pagination.rb
52
+ - lib/hash_ext.rb
52
53
  - spec/active_resource_pagination_spec.rb
54
+ - spec/hash_ext_spec.rb
53
55
  - spec/spec.opts
54
56
  - spec/spec_helper.rb
55
57
  has_rdoc: true
@@ -84,4 +86,5 @@ specification_version: 3
84
86
  summary: Adds pagination to Active Resource
85
87
  test_files:
86
88
  - spec/active_resource_pagination_spec.rb
89
+ - spec/hash_ext_spec.rb
87
90
  - spec/spec_helper.rb