2012-03-14 71 views
0

我想爲任何是MongoMapper模型的模型定義默認的before_save回調。例如,ActiveRecord的我可能只是這樣做在初始化:MongoMapper默認回調

module ActiveRecord 
    class Base 
    before_save :strip_attrs 

    private 

    def strip_attrs 
     # do stuff 
    end 
    end 
end 

是否有可能做這樣的事情與MongoMapper?

回答

1

嘗試構建一個ActiveSupport::Concern插件,然後將其插入到MongoMapper :: Document中,就像MongoMapper does的內部插件一樣。

module StripOnSave 
    extend ActiveSupport::Concern 

    included do 
    before_save :strip_attrs 
    end 

    def strip_attrs 
    # ... 
    end 
end 

module MongoMapper 
    module Document 
    include StripOnSave 
    end 

    module EmbeddedDocument 
    include StripOnSave 
    end 
end