开发者

Rails: When a model? When a lib?

I'm making a little message sending module. It'll h开发者_开发技巧andle queuing messages from a request to be picked up by a background worker to send email/SMS (or log appropriately for testing).

Question: is this a Model (under /app/models) or a lib (under /lib).

I'd like some religion on this.

Theory A: (My current theory) Unless you're subclassing ActionMailer::Base or ActiveRecord::Base, etc, your code should go into lib.

Theory B: (Theory I'm leaning towards) Things that are application-specific should be in model. Anything that could be of general use should be in lib.

Theory C: only "data models" should be in 'models'. ActionMailer subclasses break this rule, though.

As far as I know, either way it'll work fine, but I'm looking for any subtle functional or philosophical reasons for one vs. the other.

Thoughts?


Whether or not messages inherit from ActiveRecord or ActionMailer, you likely want a model for any objects that your views and controllers interact with. In your case, they will handle instances of the Message class -- you want a model for that.

As for the message sending module -- extracting out to a library is great if you plan to reuse the code elsewhere where you can just include the module in any class.

Since this is just a "little" message sending module, you might want to start in the model and eventually extract out to a separate module if it could be useful elsewhere or your model gets too messy.


You should probably think more in terms of the underlying business here. Models, as their name suggests, are here to represent (model) some real-world system or process. So, the rule of a thumb should be this: Does this entity play any role in the system I'm trying to express in my application? If the answer is positive then the entity is a good candidate for being a model. However, it'd be perfectly OK to implement the core functionality of your messaging module in some separate library for later reuse and just include it in the model.


I like the 'Data Model' Theory and I try to stick to it when I can, but I think Bensie and Milan have the right idea. If you have views and controllers associated with it, it should be with models. If you are only referencing the functionality from within another class, put it in the lib.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜