Skip to content

Instantly share code, notes, and snippets.

@michaeldv
Created December 1, 2010 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaeldv/722889 to your computer and use it in GitHub Desktop.
Save michaeldv/722889 to your computer and use it in GitHub Desktop.
activerecord/test/cases/xml_serialization_test.rb
<?xml version="1.0" encoding="UTF-8"?>
<toy>
<created-at type="datetime">2010-11-30T19:00:36-08:00</created-at>
<integer type="integer" nil="true"></integer>
<name>Mickey</name>
<pet-id type="integer" nil="true"></pet-id>
<toy-id type="integer">1</toy-id>
<updated-at type="datetime">2006-08-01T00:00:00Z</updated-at>
</toy>
<?xml version="1.0" encoding="UTF-8"?>
<toy>
<created-at type="datetime">2010-11-30T19:00:36-08:00</created-at>
<integer type="integer" nil="true"></integer>
<name>Minnie</name>
<pet-id type="integer" nil="true"></pet-id>
<toy-id type="integer">2</toy-id>
<updated-at type="datetime">2006-07-31T17:00:00-07:00</updated-at>
</toy>
module ActiveModel
# == Active Model XML Serializer
module Serializers
module Xml
extend ActiveSupport::Concern
include ActiveModel::Serialization
class Serializer #:nodoc:
class Attribute #:nodoc:
attr_reader :name, :value, :type
def initialize(name, serializable, raw_value=nil)
@name, @serializable = name, serializable
### The following line seems to fix the tests.
raw_value = raw_value.in_time_zone if raw_value.respond_to?(:in_time_zone)
@value = raw_value || @serializable.send(name)
@type = compute_type
end
require 'models/toy'
class DefaultXmlSerializationTimezoneTest < ActiveRecord::TestCase
def test_should_serialize_datetime_with_timezone
timezone, Time.zone = Time.zone, "Pacific Time (US & Canada)"
toy = Toy.create(:name => 'Mickey', :updated_at => Time.utc(2006, 8, 1))
### puts toy.to_xml
assert_match %r{<updated-at type=\"datetime\">2006-07-31T17:00:00-07:00</updated-at>}, toy.to_xml
ensure
Time.zone = timezone
end
def test_should_serialize_datetime_with_timezone_reloaded
timezone, Time.zone = Time.zone, "Pacific Time (US & Canada)"
toy = Toy.create(:name => 'Minnie', :updated_at => Time.utc(2006, 8, 1)).reload
### puts toy.to_xml
assert_match %r{<updated-at type=\"datetime\">2006-07-31T17:00:00-07:00</updated-at>}, toy.to_xml
ensure
Time.zone = timezone
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment