Module: WillPaginate::Deserializer::ClassMethods

Defined in:
lib/will_paginate/deserializer.rb

Overview

Paginating finders for ActiveResource models

WillPaginate adds paginate, per_page and other methods to ActiveResource::Base class methods and associations.

In short, paginating finders are equivalent to ActiveResource finders; the only difference is that we start with paginate instead of find, exclude the first :all parameter optionally, and the :params and its containing :page parameter are both required:

@posts = Post.paginate :all, :params => {:page => params[:page], :order => 'created_at DESC'}

In paginating finders, “all” is implicit. There is no sense in paginating a single record, right? So, you can drop the :all argument:

Post.paginate(:params => {:page => params[:page]}) =>  Post.find(:all, :params => {:page => params[:page]})

The importance of the :params parameter

In ActiveResource, all parameters in :params just get appeneded to the request url as the query string. It is up to the server to correctly snatch the options out of the params and do something meaningful with them.

This is especially important for the :page and :per_page parameters, as they are indicators for the server to paginate. If the server does not use WillPaginate’s ActiveRecord.paginate and returns a standard Array from to_xml, then that index action is likely to return all records. ActiveResource’s paginate method is smart enough to paginate the resultset for Arrays returned by the server, but realize that grabbing all records constantly will be performance intensive. However, if the server uses ActiveResource.paginate then to_xml will return in a special format that will properly be parsed into a plain vanilla WillPaginate::Collection object that works with all the standard view helpers.

Instance Method Summary collapse

Instance Method Details

#instantiate_collection_with_collection(collection, prefix_options = {}) ⇒ Object

Takes the format that Hash.from_xml produces out of an unknown type (produced by WillPaginate::Collection#to_xml_with_collection_type), parses it into a WillPaginate::Collection, and forwards the result to the former instantiate_collection method. It only does this for hashes that have a :type => “collection”.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/will_paginate/deserializer.rb', line 69

def instantiate_collection_with_collection(collection, prefix_options = {})
  if collection.is_a?(Hash) && collection["type"] == "collection"
    collectables = collection.values.find{|c| c.is_a?(Hash) || c.is_a?(Array) }
    collectables = [collectables].compact unless collectables.kind_of?(Array)
    instantiated_collection = WillPaginate::Collection.create(collection["current_page"], collection["per_page"], collection["total_entries"]) do |pager|
      pager.replace instantiate_collection_without_collection(collectables, prefix_options)
    end          
  else
    instantiate_collection_without_collection(collection, prefix_options)
  end        
end

#paginate(*args) ⇒ Object

This is the main paginating finder.

Special parameters for paginating finders

  • :params => :page – REQUIRED, but defaults to 1 if false or nil

  • :params => :per_page – defaults to CurrentModel.per_page (which is 30 if not overridden)

All other options (ie: from) work as they normally would in ActiveResource.find(:all).



58
59
60
61
62
# File 'lib/will_paginate/deserializer.rb', line 58

def paginate(*args)
  options = wp_parse_options(args.pop)
  results = find(:all, options)
  results.is_a?(WillPaginate::Collection) ? results : results.paginate(:page => options[:params][:page], :per_page => options[:params][:per_page])
end