jnunemaker/mongomapper — Issue #328: SingletonClass and MongoMapper bug
Author: phiny
Created: 2011-10-20 23:46:41+00:00
State: open

Sorry, in last post I try to report one bug of MongoMapper with SingletonClass but I think that I dont explain the bug very well. Try do something like this in console:

#create one class
class Category
include MongoMapper::Document

key :name, String
end

#create one object
c = Category.new

#open object and add another attribute
class << c
key :content, String
end

#try set the new attribute value
c.content = 'test'

#and get the value
c.content

This will not work, because the content attribute was add to @keys hash of the SingletonClass but arealy exists another hash @keys on the Category class. The accessor content will try get the object key on the Category hash, but the object only exists on the SingletonClass hash.

I overwrite two methods of MongoMapper only like a suggestion to resolve this problem, but like I sayd, i dont know if this is the better solution or if will broke anothers features.

MongoMapper::Plugins::Keys::ClassMethods.class_eval do
def keys(singleton_keys = nil)
@keys ||= {}
@keys = singleton_keys.merge(@keys) if singleton_keys.present?
@keys
end
end

MongoMapper::Plugins::Keys::InstanceMethods.class_eval do
def keys
singleton_keys = self.singleton_class.keys if self.singleton_class.respond_to? :keys
self.class.keys(singleton_keys)
end
end