Faking a Will Paginate Collection on an Active Resource model

October 28th, 2008 by Scott Davis

To follow up on my other blog post about Paginating an ActiveResource model and to_xml I figured I should include the client-side code so you can see how to actually use will paginate helpers out of the box to paginate an ActiveResource request.

This is example code from my model:

  def self.paginate(*args, &block)
    options = args.pop
    page, per_page, total_entries = wp_parse_options(options)
 
    WillPaginate::Collection.create(page, per_page, total_entries) do |pager|
      count_options = options.except :page, :per_page, :total_entries, :finder
      find_options = count_options.except(:count).update(:offset => pager.offset, :limit => pager.per_page) 
 
      args << find_options
      # @options_from_last_find = nil
      find_results = self.find(*args, &block)
      pager.replace find_results
      # magic counting for user convenience:
      pager.total_entries = find_results.total_entries unless find_results.blank?
    end
  end

Example View code:

<%= render :partial => 'list' %>
<%= will_paginate(@model)%>

Facebox With Prototype

October 14th, 2008 by Scott Davis

Making WillPaginate and Rails to_xml play nice with ActiveResource

October 10th, 2008 by Scott Davis

Advanced Model Based searches in rails

July 21st, 2008 by Scott Davis

map.resources and custom nested routes

June 5th, 2008 by Scott Davis