active resource pagination
Ruby
Switch branches/tags
Pull request Compare This branch is 1 commit ahead of dorren:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.document
.gitignore
LICENSE
README.rdoc
Rakefile
VERSION
active_resource_pagination.gemspec

README.rdoc

active_resource_pagination

This gem adds pagination support to Active Resource.

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})

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.

Other Details

You can use a normal index action on the server side, but you will also need a custom show action to return the count if no results are found. No sure why yet, but this is a quick fix. Really it shouldn't be checking for a second count if the initial one returns nil.

def show

@search = Sunspot.search(Some_search) do

end.hits

@count = {"count"=>@searchfiles.size} #returns 0 in most cases, I choose to research in case I am missing something.

respond_to do |format|
  format.xml { render :xml => @count }
end

end