Vonage Cheap phentermine Pharmacies Instant payday loan Paint Airline tickets 3 in 1 credit report Foreclosure Get viagra Fioricet online Tramadol ultram Cardiology Flomax Zovirax Ephedrine Montelukast Online payday loan Cialis levitra Phentermine 37.5 Orbitz Full tilt poker Smash repair High stakes poker Car insurance quote Tamoxifen House Buy paxil Virus and spyware protection Lorazepam Football handicapping Cheap hotels Super bowl props Allergy Porn Clarithromycin Savings account payday loan Accutane What is adipex Propecia online Consolidate credit card Cholesterol Sell real estate note Super bowl pool Cheap carisoprodol Auto owner insurance Buy clomid Cheapest tramadol Get out of debt Buy generic viagra Furniture Debt consolidation company Tadalafil Arizona auto insurance Superbowl predictions Build Occupational therapy Sildenafil citrate Credit card debt relief Casino portal Medical assistant 

Making WillPaginate and Rails to_xml play nice with ActiveResource

October 10th, 2008 by Scott Davis

We are currently working on a project that involves Flex and active resource + will_paginate and we needed to be able to paginate the xml transactions easily. Unfortunately, will_paginate and to_xml don’t play nicely when it comes to adding the current_page, total_pages, and page attributes to the xml. After many failed attempts I went looking around github and found in a few forks of will paginate that some people had solved this problem but, I didn’t want to install another version of the gem to risk breaking other apps on the server so I did it the rails way!

I started by creating a module that opens up the will_paginate collection class and includes ActiveResource and alias method chain the to_xml method to include these values. Example code below.

#enviroment.rb
...
require 'to_xml_extensions'
#lib/to_xml_extensions.rb
module WillPaginateHelpers
  include ActiveSupport::CoreExtensions::Array::Conversions
  def to_xml_with_collection_type(options = {})
        serializeable_collection.to_xml_without_collection_type(options) do |xml|
          xml.tag!(:current_page, {:type => ActiveSupport::CoreExtensions::Hash::Conversions::XML_TYPE_NAMES[current_page.class.name]}, current_page)
          xml.tag!(:per_page, {:type => ActiveSupport::CoreExtensions::Hash::Conversions::XML_TYPE_NAMES[per_page.class.name]}, per_page)
          xml.tag!(:total_entries, {:type => ActiveSupport::CoreExtensions::Hash::Conversions::XML_TYPE_NAMES[total_entries.class.name]}, total_entries)
        end.sub(%{type="array"}, %{type="collection"})
      end
      alias_method_chain :to_xml, :collection_type
 
      def serializeable_collection #:nodoc:
        # Ugly hack because to_xml will not yield the XML Builder object when empty?
        empty? ? returning(self.clone) { |c| c.instance_eval {|i| def empty?; false; end } } : self
      end
end
 
WillPaginate::Collection.send(:include, WillPaginateHelpers)

This now gives me the proper xml when I call to_xml

<?xml version="1.0" encoding="UTF-8"?>
<time-cards type="collection">
  <current_page type="integer">1</current_page>
  <per_page type="integer">25</per_page>
  <total_entries type="integer">108</total_entries>
  <time_card>
    <approved type="boolean">false</approved>
    <billable type="boolean">false</billable>
    <created_at type="datetime">2008-10-10T14:04:13-04:00</created_at>
    <date type="datetime">2008-10-10T14:04:13-04:00</date>
    <has_been_billed type="boolean">false</has_been_billed>
    <has_been_paid type="boolean">true</has_been_paid>
    <hours type="float">2.0</hours>
    <id type="integer">98</id>
    <is_overtime type="boolean">false</is_overtime>
    <task_id type="integer">6</task_id>
    <updated_at type="datetime">2008-10-10T14:04:13-04:00</updated_at>
    <user_id type="integer">1</user_id>
  </time_card>
  ...
</timecards>
This entry was posted on Friday, October 10th, 2008 at 2:48 pm by Scott Davis. Scott is a 23-year-old Web Developer working at SmartLogic Solutions in Baltimore, Maryland. Languages: Ruby, C, PHP, ColdFusion, ActionScript, Flex

Tags:

4 Responses to “Making WillPaginate and Rails to_xml play nice with ActiveResource”

  1. julien says:

    Thanks for sharing :) very cool

  2. Geoff says:

    This is cool. but I find that when I am trying to consume the resource on a client, ActiveResource dies with the error: undefined method `collect!’ for #

    Did you have to change the consumer in anyway to read/use the new xml params?

  3. Scott Davis says:

    Geoff i had to fake a will_paginate collection on the client side … ill post the code todo this later today

  4. [...] Follow up to my other blog post about Paginating an ActiveResource model and to_xml I figured I should include the client side code [...]

Leave a Reply