Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: When a model? When a lib?

I'm making a little message sending module. It'll handle 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?

like image 616
Kevin Moore Avatar asked Nov 02 '09 00:11

Kevin Moore


1 Answers

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.

like image 72
bensie Avatar answered Nov 17 '22 04:11

bensie