Rails 3 HashWithIndifferentAccess fixes

This commit is contained in:
Kyle Banker 2010-03-02 11:11:07 -05:00
parent 2251d8f61b
commit 0709b98082
2 changed files with 20 additions and 15 deletions

View File

@ -495,7 +495,7 @@ static void write_doc(buffer_t buffer, VALUE hash, VALUE check_keys, VALUE move_
}
else {
allow_id = 1;
if (strcmp(rb_class2name(RBASIC(hash)->klass), "HashWithIndifferentAccess") != 0) {
if (strcmp(rb_class2name(RBASIC(hash)->klass), "Hash") == 0) {
if ((rb_funcall(hash, rb_intern("has_key?"), 1, id_str) == Qtrue) &&
(rb_funcall(hash, rb_intern("has_key?"), 1, id_sym) == Qtrue)) {
VALUE oid_sym = rb_hash_delete(hash, id_sym);

View File

@ -4,11 +4,18 @@ require 'complex'
require 'bigdecimal'
require 'rational'
# Need to simulating this class
# without actually loading it.
module ActiveSupport
class TimeWithZone
begin
require 'active_support/core_ext'
require 'active_support/hash_with_indifferent_access'
Time.zone = "Pacific Time (US & Canada)"
Zone = Time.zone.now
rescue LoadError
warn 'Could not test BSON with HashWithIndifferentAccess.'
module ActiveSupport
class TimeWithZone
end
end
Zone = ActiveSupport::TimeWithZone.new
end
class BSONTest < Test::Unit::TestCase
@ -191,7 +198,7 @@ class BSONTest < Test::Unit::TestCase
end
def test_exeption_on_using_unsupported_date_class
[DateTime.now, Date.today, ActiveSupport::TimeWithZone.new].each do |invalid_date|
[DateTime.now, Date.today, Zone].each do |invalid_date|
doc = {:date => invalid_date}
begin
bson = BSON.serialize(doc)
@ -433,21 +440,19 @@ class BSONTest < Test::Unit::TestCase
BSON.serialize(c, false, false).to_s
end
begin
require 'active_support'
rescue LoadError
warn 'Could not test BSON with HashWithIndifferentAccess.'
end
if defined?(HashWithIndifferentAccess)
def test_keep_id_with_hash_with_indifferent_access
doc = HashWithIndifferentAccess.new
doc[:_id] = ObjectID.new
BSON.serialize(doc, false, false).to_a
embedded = HashWithIndifferentAccess.new
embedded['_id'] = ObjectID.new
doc['_id'] = ObjectID.new
doc['embedded'] = [embedded]
BSON.serialize(doc, false, true).to_a
assert doc.has_key?("_id")
assert doc['embedded'][0].has_key?("_id")
doc['_id'] = ObjectID.new
BSON.serialize(doc, false, false).to_a
BSON.serialize(doc, false, true).to_a
assert doc.has_key?("_id")
end
end