active-resource-pagination 0.0.1
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/lib/active_resource/pagination.rb +46 -0
- data/test/pagination_test.rb +64 -0
- metadata +79 -0
@@ -0,0 +1,46 @@
|
|
1
|
+
module ActiveResource
|
2
|
+
module Pagination
|
3
|
+
def self.included(base)
|
4
|
+
base.format = PagingFormatter.new(base.collection_name)
|
5
|
+
end
|
6
|
+
|
7
|
+
class PagedArray < Array
|
8
|
+
attr_accessor :total_pages
|
9
|
+
attr_accessor :current_page
|
10
|
+
end
|
11
|
+
|
12
|
+
# PagingFormatter expects a JSON response that looks like this (for a resource named "posts"):
|
13
|
+
# {
|
14
|
+
# "total_pages": 5,
|
15
|
+
# "current_page": 1,
|
16
|
+
# "posts": [{
|
17
|
+
# "id": 1
|
18
|
+
# }]
|
19
|
+
# }
|
20
|
+
class PagingFormatter
|
21
|
+
include ActiveResource::Formats::JsonFormat
|
22
|
+
|
23
|
+
attr_accessor :collection_name
|
24
|
+
|
25
|
+
def initialize(name)
|
26
|
+
self.collection_name = name
|
27
|
+
end
|
28
|
+
|
29
|
+
def decode(json)
|
30
|
+
meta = super(json)
|
31
|
+
if meta.is_a?(Hash)
|
32
|
+
list = PagedArray.new(meta[collection_name])
|
33
|
+
|
34
|
+
list.total_pages = meta['total_pages']
|
35
|
+
list.current_page = meta['current_page']
|
36
|
+
|
37
|
+
list
|
38
|
+
else
|
39
|
+
# unexpected result, fall back to default behavior
|
40
|
+
meta
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
require 'active_resource'
|
4
|
+
require 'active_resource/pagination'
|
5
|
+
|
6
|
+
class Post < ActiveResource::Base
|
7
|
+
self.element_name = "post"
|
8
|
+
|
9
|
+
include ActiveResource::Pagination
|
10
|
+
end
|
11
|
+
|
12
|
+
class PaginationTest < Test::Unit::TestCase
|
13
|
+
def test_works
|
14
|
+
arr = ActiveResource::Pagination::PagedArray.new([1, 2, 3])
|
15
|
+
arr.total_pages = 5
|
16
|
+
arr.current_page = 1
|
17
|
+
|
18
|
+
assert_equal arr.total_pages, 5
|
19
|
+
assert_equal arr.current_page, 1
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_paging_attributes
|
23
|
+
ActiveResource::HttpMock.respond_to do |mock|
|
24
|
+
fixture = <<-JSON
|
25
|
+
{
|
26
|
+
"total_pages" : 5,
|
27
|
+
"current_page" : 1,
|
28
|
+
"posts" : [{
|
29
|
+
"id" : 1,
|
30
|
+
"title" : "Hello World"
|
31
|
+
}]
|
32
|
+
}
|
33
|
+
JSON
|
34
|
+
|
35
|
+
mock.get "/posts.json", {}, fixture, 200
|
36
|
+
end
|
37
|
+
|
38
|
+
# late bind the site
|
39
|
+
Post.site = "http://localhost"
|
40
|
+
posts = Post.find(:all)
|
41
|
+
|
42
|
+
assert_equal posts.total_pages, 5
|
43
|
+
assert_equal posts.current_page, 1
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_fallback
|
47
|
+
ActiveResource::HttpMock.respond_to do |mock|
|
48
|
+
fixture = <<-JSON
|
49
|
+
[{
|
50
|
+
"id" : 1,
|
51
|
+
"title" : "Hello World"
|
52
|
+
}]
|
53
|
+
JSON
|
54
|
+
|
55
|
+
mock.get "/posts.json", {}, fixture, 200
|
56
|
+
end
|
57
|
+
|
58
|
+
# late bind the site
|
59
|
+
Post.site = "http://localhost"
|
60
|
+
posts = Post.find(:all)
|
61
|
+
|
62
|
+
assert_equal posts.length, 1
|
63
|
+
end
|
64
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active-resource-pagination
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jeff Moss
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activeresource
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Basic pagination support for ActiveResource
|
47
|
+
email: jmoss@heavyobjects.com
|
48
|
+
executables: []
|
49
|
+
extensions: []
|
50
|
+
extra_rdoc_files: []
|
51
|
+
files:
|
52
|
+
- lib/active_resource/pagination.rb
|
53
|
+
- test/pagination_test.rb
|
54
|
+
homepage: https://github.com/jeffmoss/active-resource-pagination
|
55
|
+
licenses: []
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.8.24
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: ActiveResource pagination
|
78
|
+
test_files:
|
79
|
+
- test/pagination_test.rb
|