开发者

Allow for configurable model validations using separate files?

I'm working on a Rails 3 app that uses MongoDB. As such, new keys (i.e. fields or columns from SQL-based DBs) can be added to models on the fly and without the need for any migrations or similar overhead. I am using the MongoMapper ORM.

I have a model, EmailTemplate that takes several of its keys from a YAML file in the config directory of the app. The thought behind this is that the app is fully generic and new use cases only require the modification of configuration files rather than application code.

This is working fine. Now, I want to add validations for several of the keys. Ideally, since the keys are created from a configuration file, the validations should also be created from a configuration file, maintaining the reusability of the app.

I have tried adding a file email_template_validations.rb to config, which defines a class like this:

class EmailTemplate::Validations
    include ActiveModel::Validations
    validates :countries, :presence => true
    validates :languages, :presence => true
    ...
end

Then in my model, I am doing require config/email_template_validations. Unfortunately, this is where it breaks down,开发者_如何学JAVA and I receive a LoadError saying:

Expected proj_dir/app/models/email_template.rb to define EmailTemplate

So, the question: Am I fighting best practices here? Is there a better way to do this? How can I use validations from a separate file?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜