Search code examples
ruby-on-railspluginsmodularity

most appropriate form to make re-usable Ruby on Rails code more re-usable?


I make a lot of rails applications, and there seems to be lots of way to make code reusable.

Among the things I reuse are the following:

css files, initializers, views/shared/ partials admin images application helpers haml view templates gems etc...

I once tried to put all of this into a gem about a year ago and it backfired terribly mostly because of changing dependencies, and the fact that I was always tweaking the code and adding to it. This is not good as a gem, because to see a simple change take place you have to compile/publish/install the gem. So the gem is not right.

What are recommended ways to keep all of this "personal" code more or less modular so I can "plop" it into a new rails project and edit it as normal rails code (no gem compiling / publishing).

The two words that come to mind are engines and plugins, but I really don't know much about how either of them work. I've also heard of (confusingly) "templates", not the view kind.

What are your suggestions. I'd love to get this figured out!


Solution

  • It depends on the file, if you want I can break an answer down for you. But my general advice is to keep your code small, lightweight and designed for one task. A good way of doing this is determining if the code you write is orthogonal. Wikipedia defines it in a computer science scope as this:

    Orthogonality is a system design property facilitating feasibility and compactness of complex designs. Orthogonality guarantees that modifying the technical effect produced by a component of a system neither creates nor propagates side effects to other components of the system. The emergent behavior of a system consisting of components should be controlled strictly by formal definitions of its logic and not by side effects resulting from poor integration, i.e. non-orthogonal design of modules and interfaces. Orthogonality reduces testing and development time because it is easier to verify designs that neither cause side effects nor depend on them.

    As far as constraining the size of your code you can leave that up to your own personal judgement on when and where you delegate methods and classes. However I like to keep in mind the separation of concerns and the single responsibility principle when working on breaking up the issues I face into smaller parts.

    However you cant just write good code and leave it at that, you need to have a good way of distributing your code. I have a development server on my LAN that hosts many private Git repositories however you can use any VCS or DVCS to keep your code organized. I can then use this to keep my projects sourcing the latest versions of the files from the server when I need to update them and make changes and I can always branch if my requirements change for the project I'm working on. With Git I always have my code in version control so I use submodules to let me keep the latest version of the code handy and up to date.

    Let me know if you want more information or if you dont use Git I can look for similar features on a VCS you are familiar with. Also I'd just like to note that this is a good question that has a lot of points and I'm glad you asked since I remember struggling with this and I'm sure others are tackling the same problem as well.