我正在尝试为 App.net 中的注释构建一个模型:http: //developers.app.net/docs/meta/annotations/。为了轻松地将注释添加到许多模型中,我使用了一个关注点。
到目前为止,我有注释的模型:
class Annotation
include Mongoid::Document
field :key, :type => String
field :value
embedded_in :annotatable, polymorphic: true
# Some validations
attr_accessible :key, :value
attr_accessor :key, :value
end
关注点:
module Annotatable
extend ActiveSupport::Concern
included do
embeds_many :annotations, as: :annotatable
end
# Somewhat helper method to add annotaion only after they are valid.
def add_annotation( annotation )
if !annotation.valid?
return false
else
self.annotations.push annotation
return true
end
end
end
最后还有一个模型,其中包含我的关注点:
class User
include Mongoid::Document
include Mongoid::Timestamps
include Annotatable
# Much other stuff...
end
不幸的是,注释不会被保存:-/有什么想法吗?