Skip to content

Commit

Permalink
Recogonize _id when using dynamic attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
leshill committed Mar 10, 2012
1 parent e76bf1a commit 7ce5abd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/mongo_doc/document.rb
Expand Up @@ -143,7 +143,7 @@ def bson_create(bson_hash, options = {})
allocate.tap do |obj|
bson_hash.each do |name, value|
if MongoDoc::Configuration.dynamic_attributes
if _attributes.include?(name.to_sym)
if _attributes.include?(name.to_sym) || name == '_id'
obj.send("#{name}=", MongoDoc::BSON.decode(value, options))
else
obj.send(:dynamic_attributes)[name] = MongoDoc::BSON.decode(value, options)
Expand Down
6 changes: 5 additions & 1 deletion spec/mongo_doc/document_spec.rb
Expand Up @@ -275,7 +275,7 @@ class DynamicExample
attr_accessor :known
end

let(:attribute_data) { {'known' => 'known', 'unknown' => true} }
let(:attribute_data) { {'_id' => BSON::ObjectId.new, 'known' => 'known', 'unknown' => true} }

context "when allowed" do
before { MongoDoc::Configuration.dynamic_attributes = true }
Expand All @@ -285,6 +285,10 @@ class DynamicExample
DynamicExample.bson_create(attribute_data).should be_instance_of(DynamicExample)
end

it "sets the id" do
DynamicExample.bson_create(attribute_data).id.should be_instance_of(BSON::ObjectId)
end

it "adds a read accessor for the attribute" do
doc = DynamicExample.bson_create(attribute_data)
doc.should respond_to(:unknown)
Expand Down

0 comments on commit 7ce5abd

Please sign in to comment.