Why do some "Plain Old Ruby Objects" go in app/models directory instead of lib direcory? - ruby-on-rails

I am working on a project where the current developers have put their "Plain Old Ruby Objects" in the models directory of our Rails app.
I have seen most examples online where the PORO files go inside lib instead, but some say models.
Is there even a logical / specific reason why people put them in the models directory over the lib directory?

"Idiomatically" the models directory intended for code used to hold state. Most of the time this would be ActiveRecord subclasses corresponding to database tables. However frequently people put other stuff in the models directory. One thing often seen are code dropped here in order to take advantage of auto-reloading. (the lib dir doesn't normally auto-reload)

Based on the Getting Started Rails guide, the app/models/ directory is pretty much anything, whereas lib/ is for modules that are used across the entire app (e.g. extensions).
As #seand said, yes, one advantage is that the app/models/ directory automatically reloads, but I tend to think of it as any class that "interacts with other classes" should live in app/models/, with the only exception being a service class (which I tend to think of as "a class which manipulates the state of another class"), which I tend to put into app/services/.
I know a lot of developers would disagree with me - many I've talked to even create a separate directory namespaced to their app (e.g. if your app is named "MyBlog", they would create an app/myblog directory for any object not explicitly backed by the database, but not a module or a service class either.
That said, I think ultimately it boils down to a) personal preference and b) where you feel is best to place the PORO with respect to your app.

There is no hard and fast rule on where to put POROs. The rails community has been hashing this out for a while now. At one point the convention was to put stuff in the concerns directory but that seems to have fallen out of favor with some folks.
One rule of thumb for the difference between /lib and app/{blah} is that code in the /lib folder is code that you presumably can reuse across several projects e.g. general purpose class or utilities. for example if you have some custom monkey patches to core ruby or rails classes that you will use in multiple projects, a good place to but them would be in the lib folder. Anything in app/{blah} should pertain specifically to current project.
One way to think of lib folder is as the poor cousin of plugins and gems.
Just my 2 cents on the subject

Related

Is it possible to keep different folders for different applications in the root level of RoR project

This is what I'm trying to achieve. I want to build an application with an admin and client portal. So I want to build a single RoR application for both these portals and I would like to keep the code of each portal separated at the root level. So basically following is the folder structure I want to have in my project.
/admin
/client
/modals
/config
/db
...
...
So I want to keep controllers, views, and other stuff relevant to each project in each folder separately.
So I want to move the modal folder to the root level as well. I read about the namespaces in RoR, but as I understand I'm unable to do it with namespaces alone. I guess I need to do some configuration level changers, don't I?
Please let me know if there is a way to do this.
Thank You.
What you're suggesting sounds like 5 Rails apps running independently. Does each one of those directories have its own Gemfile, database?
It would be possible to do what you want, but it will almost certainly be more trouble than it's worth. You'll have to override some path configurations, make adjustments to connect the models/views/controllers directories for each entity (https://guides.rubyonrails.org/action_view_overview.html#view-paths), and probably much more. Rails is an opinionated framework and it doesn't do things like this gracefully.
The 'Rails' way to do it would be to:
Add some namespace routes for each of your directories
Add app/models/admin app/views/admin app/controllers/admin etc. directories
Namespace your model/view/controller files. e.g. class Admin::SomeController < ApplicationController
If each of those is really its own app, then you would probably want to create a repository for each of them so that they're in version control separately.

Zend Framework 2: Autoload classmap

I generated the classmap of our project to improve its performance in production.
I'm surprised because the generated file is really big (more than 5000 lines). It includes the whole ZF2 library, Doctrine, etc.
Is it normal or should I exclude the vendor directory?
EDIT:
#Sam, yes we are using APC in production.
My plan is to automatically generate the class map as part of the deployment process.
I suppose you're talking about the classmap autoloader provided by composer? Then yes, this is absolutely normal. The classmap generated for each module should only contain the modules classes tho.
As far as performance is concerned, that's a bit of a tricky thing. If you do not have access to APC or Memcache (or any other memory cache for that matter), then a 5000 Lines classmap loader will probably not be the most ideal solution.
The most ideal solution would be a classmap that ONLY contains the autoloading directives for the classes that you actually use throughout your project. Luckily Evan ".pro" Coury has created the very great EdpSuperluminal. That one does precisely what the ideal solution is all about, but it requires quite a bit of manual attention, since basically once your project is done you'll need to call every single URL of your application once with a special URL parameter to create the classmap.

What are the performance implications of using require_dependency in Rails 3 applications?

I feel like I understand the difference between require and require_dependency (from How are require, require_dependency and constants reloading related in Rails?).
However, I'm wondering what should happen if I use some of the various methods out there (see http://hemju.com/2010/09/22/rails-3-quicktip-autoload-lib-directory-including-all-subdirectories/ and Best way to load module/class from lib folder in Rails 3?) to get all files loading so we:
don't need to use require_dependency all over the place in the application and
don't have to restart development servers when files in the lib directory change.
It seems like development performance would be slightly impacted, which is not that big of a deal to me. How would performance be impacted in a production environment? Do all of the files generally get loaded only once if you are in production anyway? Is there a better way that I'm not seeing?
If you could include some resources where I could read more about this, they would be greatly appreciated. Some blog posts said that this behavior changed recently with Rails 3 for autoreloading lib/* files and that it was contentious, but I didn't see any links to these discussions. It would be helpful for considering the pros/cons. Thanks!
The code reloader is disabled by default in production. So if you are calling require_dependency at the top of a file it is going to be executed only once.
The Rails 3 change you mentioned is really small. You can usually call Foo and it will be loaded from app/models/foo.rb automatically. Before it could also be loaded from lib/foo.rb. (These directories app/models and lib are called autoload paths.) Rails team decided to remove lib from autoload paths in the 3rd version. You can still put it back. But it is encouraged to leave in lib less frequenttly-changed and project-specific files. If you have something that does not belong to any of the default app subdirectories like app/models or app/controllers you don't have to put it in lib. You can add your own subdirectory. I have app/presenters, for example. There is a discussion on the old issue tracker if you want more info on that.

Can/should directories for test frameworks be consolidated into a top-level directory (a la vendor/plugins)?

For some reason, I find it really irksome that the files for each testing framework (rspec, test::unit, cucumber, etc.) live in a separate folder in the top level of my app. Is there a strong reason these directories should be scattered about instead of consolidated like gems/plugins in the vendor directory?
If there isn't an actual reason for the way it is, would it be kosher to consolidate these directories into a top-level "test" directory, containing subdirectories for each of the testing frameworks alongside fixture data, etc.? Has anyone else ever been driven bonkers this (and maybe already hacked together a quick'n'easy way of updating the necessary paths to get this to work)?
I for one would welcome this change.
Personally I use RSpec and Cucumber at the same time and I'm torn whether or not to put the fixtures in the features/support/fixtures directory (I create it myself) or spec/fixtures. What if I use this fixture in features and in spec tests too?! [overly emotional] Why won't somebody think of the fixtures!!
Definitely. A top-level test directory with the subdirectories of features, specs, fixtures and whatever else you need.
Great question!

Where to reopen a class in RoR

I'm attempting to reopen the String class in rails and add a bunch more methods for my app to use. Writing the code isn't a problem - my question is rather about where this code should go.
It doesn't make sense to me to reopen a class inside a different model file, because it really has nothing to do with any of the models specifically. I thought perhaps somewhere in config or lib would make sense, but I'm not particularly well versed with RoR yet.
To summarize, where would be the most logical place to define class-modifying code, and are there any implications depending on where/when the code is loaded?
The most logical place is probably in a file in the config/initializers directory. Any *.rb file you put in here will be automatically executed when rails boots. If you want, you could put them in a sub folder, so you could do something like config/initializers/extensions/*.rb.
I try keep these monkey-patches to a minimum, only when they are very clearly in the best interest of my code.
Lately I have preferred to keep the files organized in folders such as lib/monkey/string.rb, lib/monkey/hash.rb, etc. I then require all files in the lib/monkey folder in my environment.rb file.
# Load all monkey-patches.
Dir["lib/monkey/*.rb"].each {|monkeyfile| require monkeyfile}
This keeps all of my class modifying code isolated to one location, should a problem arise. I also enjoy the somewhat goofy naming, because it makes it stand out as something to be mindful of. Someone may have a better system, if so ... I'd love to hear about it!

Resources