Skip to content

Commit

Permalink
Added to_xml and ActiveResource deserialization support for empty col…
Browse files Browse the repository at this point in the history
…lections.
  • Loading branch information
Larry Diehl committed Apr 22, 2008
1 parent 35cb850 commit 1302527
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
7 changes: 6 additions & 1 deletion lib/will_paginate/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,17 @@ def replace(array)
end

def to_xml_with_collection_type(options = {})
to_xml_without_collection_type(options) do |xml|
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
# 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
end
2 changes: 1 addition & 1 deletion lib/will_paginate/deserializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module ClassMethods
def instantiate_collection_with_collection(collection, prefix_options = {})
if collection["type"] == "collection"
collectables = collection.values.find{|c| c.is_a?(Hash) || c.is_a?(Array) }
collectables = [collectables] unless collectables.kind_of?(Array)
collectables = [collectables].compact unless collectables.kind_of?(Array)
instantiated_collection = instantiate_collection_without_collection(collectables, prefix_options = {}).paginate(
:page => collection["current_page"],
:per_page => collection["per_page"],
Expand Down
16 changes: 10 additions & 6 deletions test/collection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,22 @@ def test_page_count_was_removed
end

def test_to_xml_with_blank_collection_type
collection = [].paginate(:page => 1, :per_page => 1)
assert_select_xml collection.to_xml(:dasherize => false, :root => "rappers"), "rappers[type=?]", "collection"
collection = [].paginate(:page => 1, :per_page => 5)
assert_select_xml collection.to_xml(:dasherize => false), "nil_classes[type=?]", "collection" do
assert_select_xml "current_page[type=?]", "integer", "1"
assert_select_xml "per_page[type=?]", "integer", "5"
assert_select_xml "total_entries[type=?]", "integer", "0"
end
end

def test_to_xml_with_filled_collection_type
collection = [{:word => "up"}].paginate(:page => 1, :per_page => 1)
collection = [{:word => "up"}].paginate(:page => 1, :per_page => 5)
assert_select_xml collection.to_xml(:dasherize => false, :root => "rappers"), "rappers[type=?]", "collection" do |rappers|
rappers.each do |rapper|
assert_select_xml "word", "up"
assert_select_xml "current_page[type=?]", "integer", 1
assert_select_xml "per_page[type=?]", "integer", 1
assert_select_xml "total_entries[type=?]", "integer", 1
assert_select_xml "current_page[type=?]", "integer", "1"
assert_select_xml "per_page[type=?]", "integer", "5"
assert_select_xml "total_entries[type=?]", "integer", "1"
end
end
end
Expand Down
24 changes: 23 additions & 1 deletion test/deserializer_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
require 'helper'
require 'lib/activeresource_test_case'

class DeserializerTest < Test::Unit::TestCase
class DeserializerTest < Test::Unit::TestCase
def test_collection_without_entries
projects_xml = [].paginate(:page => 1, :per_page => 5).to_xml

ActiveResource::HttpMock.respond_to do |mock|
mock.get "/projects.xml", {}, projects_xml
end

projects = Client::Project.find(:all)

assert projects.kind_of?(WillPaginate::Collection)

assert_equal 0, projects.size

assert_equal 1, projects.current_page

assert_equal 5, projects.per_page

assert_equal 0, projects.total_entries

assert_equal nil, projects.first
end

def test_collection_with_one_entry
projects_xml = [{:name => "will_paginate" }].paginate(:page => 1, :per_page => 5).to_xml(:root => "projects")

Expand Down

0 comments on commit 1302527

Please sign in to comment.