forked from dorren/active_resource_pagination
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
110 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,35 @@ | ||
= active_resource_pagination | ||
|
||
Description goes here. | ||
This gem adds pagination support to Active Resource. | ||
|
||
== Note on Patches/Pull Requests | ||
|
||
* Fork the project. | ||
* Make your feature addition or bug fix. | ||
* Add tests for it. This is important so I don't break it in a | ||
future version unintentionally. | ||
* Commit, do not mess with rakefile, version, or history. | ||
(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) | ||
* Send me a pull request. Bonus points for topic branches. | ||
== Sample Usage | ||
# Article is a resource model | ||
|
||
Article.paginate | ||
Article.paginate(:page => 2, :per_page => 20) | ||
Article.paginate(:page => 2, :per_page => 20, :total_entries => 123) | ||
Article.paginate(:page => 2, :per_page => 20, :params => {:year => 2010}) | ||
|
||
== Copyright | ||
|
||
Copyright (c) 2010 Dorren Chen. See LICENSE for details. | ||
|
||
== Configuration | ||
|
||
To set default per_page value for all resources. you can do | ||
ActiveResource::Base.per_page = 20 # in config/environment or initializers | ||
|
||
or to implement per_page() in your resource class. | ||
|
||
|
||
== Detail | ||
|
||
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. | ||
|
||
Article.paginate(:page => 2, :per_page => 20, :params => {:year => 2010}) # is translated into 2 request calls | ||
|
||
Article.find(:all, :params => {:year => 2010, :offset => 40, :limit => 20}) | ||
Article.find(:one, :from => :count, :params => {:year => 2010}) | ||
|
||
* If you pass in the :total_entries parameter, Model.count() will not be called. | ||
* You can always override count() if default doesn't suit your need. | ||
* If you don't pass in the :total_entries parameter, Model.count() will be called, | ||
unless result count > per_page, then this method automatically sets the count from the result. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.0.5 | ||
0.0.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Hash | ||
# similar to Enumerable partition() method, separate one hash into 2 based on the passed in test condition. | ||
def partition(&block) | ||
h = dup | ||
h2 = {} | ||
each{|k, v| | ||
h2[k] = h.delete(k) if block.call(k,v) | ||
} | ||
[h2, h] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require File.expand_path(File.dirname(__FILE__) + '/spec_helper') | ||
|
||
describe Hash do | ||
describe "#partition" do | ||
it "should partition" do | ||
h1, h2 = {'a' => 1, 'b' => 2, 'c' => 10, 'd' => 20}.partition{|k, v| v >= 10} | ||
h1.should == {'c' => 10, 'd' => 20} | ||
h2.should == {'a' => 1, 'b' => 2} | ||
end | ||
end | ||
end |