Faking a Will Paginate Collection on an Active Resource model

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)%>
Author image

About Scott Davis

You've successfully subscribed to SmartLogic Blog
Great! Next, complete checkout for full access to SmartLogic Blog
Welcome back! You've successfully signed in.
Unable to sign you in. Please try again.
Success! Your account is fully activated, you now have access to all content.
Error! Stripe checkout failed.
Success! Your billing info is updated.
Error! Billing info update failed.