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/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/active_resource_pagination.rb +52 -0
- data/spec/active_resource_pagination_spec.rb +65 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +86 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Dorren Chen
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= active_resource_pagination
|
2
|
+
|
3
|
+
Description goes here.
|
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.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 Dorren Chen. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "active_resource_pagination"
|
8
|
+
gem.summary = %Q{Adds pagination to Active Resource}
|
9
|
+
gem.description = %Q{Adds pagination to Active Resource}
|
10
|
+
gem.email = "dorrenchen@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/dorren/active_resource_pagination"
|
12
|
+
gem.authors = ["Dorren Chen"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "active_resource_pagination #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'active_resource'
|
2
|
+
require 'will_paginate'
|
3
|
+
|
4
|
+
module ActiveResource
|
5
|
+
# This adds pagination support to Active Resource. For example
|
6
|
+
#
|
7
|
+
# Article.paginate
|
8
|
+
# Article.paginate(:all, :page => 2, :per_page => 20)
|
9
|
+
# Article.paginate(:all, :page => 2, :per_page => 20, :total_entries => 123)
|
10
|
+
# Article.paginate(:all, :from => :most_popular,
|
11
|
+
# :params => {:year => 2010, :page => 1, :per_page => 20})
|
12
|
+
module Pagination
|
13
|
+
def self.included(base)
|
14
|
+
base.extend ClassMethods
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
# can be overrided in subclass
|
19
|
+
def per_page
|
20
|
+
10
|
21
|
+
end
|
22
|
+
|
23
|
+
# use same method signatures as find(), optional additional parameters:
|
24
|
+
# page - current page
|
25
|
+
# per_pape - entries per page
|
26
|
+
# total_entries - total entries count
|
27
|
+
#
|
28
|
+
# if resource backend doesn't paginate and retursn all result, then this method automatically
|
29
|
+
# sets the total_entry count from the result. Otherwise, you have to pass in the
|
30
|
+
# :total_entries count value manually.
|
31
|
+
def paginate(*args)
|
32
|
+
options = args.last || {}
|
33
|
+
options = options[:params] || options
|
34
|
+
options = {:page => 1, :per_page => per_page}.merge(options)
|
35
|
+
page = options[:page]
|
36
|
+
per_page = options[:per_page]
|
37
|
+
|
38
|
+
arr = find(*args)
|
39
|
+
total_entries = options[:total_entries] || arr.size
|
40
|
+
WillPaginate::Collection.create(page, per_page, total_entries) do |pager|
|
41
|
+
if arr.size > per_page
|
42
|
+
pager.replace arr[pager.offset, pager.per_page]
|
43
|
+
else
|
44
|
+
pager.replace arr
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
ActiveResource::Base.send :include, ActiveResource::Pagination
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe ActiveResource::Pagination do
|
4
|
+
describe "##paginate" do
|
5
|
+
before(:all) do
|
6
|
+
class Article < ActiveResource::Base
|
7
|
+
self.site = ''
|
8
|
+
|
9
|
+
def self.per_page
|
10
|
+
2
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.from_xml(xml)
|
14
|
+
instantiate_collection(format.decode(xml))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# generate 5 articles, per_page is 2
|
19
|
+
xml = (1..5).collect{|x| {:name => "article #{x}", :content => "blah blah #{x}"}}.to_xml(:root => "articles")
|
20
|
+
@articles = Article.from_xml(xml)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should respond to paginate" do
|
24
|
+
ActiveResource::Base.should respond_to(:paginate)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should override per_page" do
|
28
|
+
Article.per_page.should == 2
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "when backend does not paginate" do
|
32
|
+
before(:each) do
|
33
|
+
Article.should_receive(:find).and_return(@articles)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should paginate with no params" do
|
37
|
+
col = Article.paginate
|
38
|
+
col.current_page.should == 1
|
39
|
+
col.per_page.should == Article.per_page
|
40
|
+
col.total_entries.should == @articles.size
|
41
|
+
col.total_pages.should == 3 # (col.total_entries / col.per_page.to_f).ceil
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should paginate with params" do
|
45
|
+
col = Article.paginate(:page => 2)
|
46
|
+
col.current_page.should == 2
|
47
|
+
col.first.should == @articles[3]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "when backend do paginate" do
|
52
|
+
it "should paginate when backend returns paginated entries" do
|
53
|
+
@articles = @articles[2,2] # returns 2nd page result
|
54
|
+
Article.should_receive(:find).and_return(@articles)
|
55
|
+
|
56
|
+
col = Article.paginate(:page => 2, :total_entries => 5)
|
57
|
+
col.current_page.should == 2
|
58
|
+
col.per_page.should == Article.per_page
|
59
|
+
col.total_entries.should == 5
|
60
|
+
col.total_pages.should == 3 # (col.total_entries / col.per_page.to_f).ceil
|
61
|
+
col.first.should == @articles.first
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_resource_pagination
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Dorren Chen
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-06-02 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 2
|
30
|
+
- 9
|
31
|
+
version: 1.2.9
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
description: Adds pagination to Active Resource
|
35
|
+
email: dorrenchen@gmail.com
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files:
|
41
|
+
- LICENSE
|
42
|
+
- README.rdoc
|
43
|
+
files:
|
44
|
+
- .document
|
45
|
+
- .gitignore
|
46
|
+
- LICENSE
|
47
|
+
- README.rdoc
|
48
|
+
- Rakefile
|
49
|
+
- VERSION
|
50
|
+
- lib/active_resource_pagination.rb
|
51
|
+
- spec/active_resource_pagination_spec.rb
|
52
|
+
- spec/spec.opts
|
53
|
+
- spec/spec_helper.rb
|
54
|
+
has_rdoc: true
|
55
|
+
homepage: http://github.com/dorren/active_resource_pagination
|
56
|
+
licenses: []
|
57
|
+
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options:
|
60
|
+
- --charset=UTF-8
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
requirements: []
|
78
|
+
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 1.3.6
|
81
|
+
signing_key:
|
82
|
+
specification_version: 3
|
83
|
+
summary: Adds pagination to Active Resource
|
84
|
+
test_files:
|
85
|
+
- spec/active_resource_pagination_spec.rb
|
86
|
+
- spec/spec_helper.rb
|