Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails : Namespacing vs Engines?

I am just getting started in a Rails project after a long time in the PHP world.

This project is in a very early stage, meaning that is a good time to do major changes, and currently comprises two different rails modules, one for administrators and one for users, both in separate rails instances. What I would like to do is to merge both projects into a single rails instance, which I believe it will improve the manageability of the application in the long run.

Both instances share the same database and each of them has a devise model for authentication. I have been documenting myself about ways of merging both projects and I came up with two alternatives: the first would be to create two namespaces, one for users and another one for admins, and share the model and framework logic. the second one would be to create engines for admins and users, which seems to be cleaner but much more complex.

I have read a lot documentation and I am experimenting with rails, but at this point I am in need of a more experienced opinion.

Thank you in advance, and I appreciate your thoughts on this.

like image 713
Andres Avatar asked Feb 11 '23 10:02

Andres


1 Answers

Both namespaces and engines have pros and cons. Which one to use will come down to your particular use case. For the most part, I recommend using namespaces as it tends to be easier and saves some headaches. Especially when sharing a data model. However, engines are great when you want to share common isolated behavior in multiple applications.

Engines

Pros

  • Isolate related functionality. This is especially good if you have some tables that are only used by one of the engines as they can manage their own database migrations with prefixed table names.
  • Can be reused in multiple Rails applications. (In some cases)
  • Good starting point when extracting code out of a monolithic Rails application.
  • Reduces the chance that developers working on different engines of a large codebase will break functionality in one of the other engines.

Cons

  • Not a common pattern to have an application that is made up entirely of engines. So it will take new engineers some time to get used to how everything is setup.
  • Can be frustrating to share common functionality. E.g. stylesheets
  • You will most likely end up sharing code between the two engines in unexpected ways. Which takes away some value of the Isolate related functionality benefit.
  • Text editors with Rails plugins tend to have odd behavior. For example, the vim plugin shortcuts sometimes don't work when inside of a plugin directory

Namespaces

Pros

  • Simple to use and already plenty of Rails conventions to follow.
  • Sharing assets and other common functionality tends to be very straightforward.
  • Fully supported by text editor rails plugins.

Cons

  • Unclear which models or libraries are important to different namespaces.
like image 118
Jkarayusuf Avatar answered Feb 13 '23 17:02

Jkarayusuf