Gemifying our way to future projects

Itay January 20, 2013

After working on several new products at the ISC for the past year, one of the things that always bugged us as a team is the amount of copy & paste we had to do from one project to the other to the libs that we wrote. These libs could apply to any project, but we just didn’t take the extra steps to make them self-contained. As the amount of products that we work on increases, it makes a lot of sense for us to extract these components into ruby gems that can be shared across our projects. This gives us quite a few benefits, such as: * No more copying & pasting lib code! Just add the desired gems to the N0-120 project’s Gemfile and voila! * A lot easier to maintain these libs now that it’s separate from our project. No more different versions of the same code in different projects, trying to figure out which one is the most up-to-date. * These libraries are all open-sourced by us, so if anyone else finds them useful and contributes to them, everyone benefits! So without further ado, here they are, in no particular order:

gem 'batlog'

Link to GitHub repository BatLog is our little logging helper. It allows you to log into more than just the default RailsLogger which saves the logs into text files. You can add a DbLog that, as the name suggests, adds your logs into your DB, and also an ExceptionalLogger that notifies of critical errors that occurred in your system with more details. You can write your own custom 200-120 logger that can report to other external services. It’s also possible to provide BatLog with a log context that can provide additional data on a log entry, such as request data, user state, etc. One last thing – if BatLog fails to log in any logger, it notifies the other loggers about it.

gem 'configreader'

Link to Github repository A common pattern that is used in rails apps is to load up static configuration files into simple hash objects, that can then be used across our Rails app from any object. This is achieved by defining for each static configuration an initializer file that loads up the data into a const on Object. Most of the time, these configuration files contain configurations that are environment-specific, and other times these are just flat configurations. In one of our projects, we needed another layer of awareness. We then thought that this can be done a bit nicer. ConfigReader offers to load up your configuration files into a container object that gives you access to your data with methods, instead of hash keys. So when you have a configuration file with different settings for each environment, let’s say Facebook, instead of accessing your data like so:


With EnvConfigReader you can access it like so:


Another great feature of ConfigReader that I love is that you can automatically create these configuration files by following a simple directory convention, and a little initializer with a configuration setting. (No more initializer files for configuration files, yay!)

gem 'creators'

Link to GitHub respository In one of our latest projects, we saw a pattern in our controllers that is worth extracting into its own Concern. The pattern is model creation and updating. ActiveRecord gives you great tools to handle it easily, but when the size of your forms increases, there’s a need to slice up the request params, and prepare them in a certain way for the model. You may also want to add custom validations that are not so relevant to the model in question. You can read more about Creators in Yon’s post, ‘Trimming the fat from your controllers’.

gem 'isc_analytics'

Link to GitHub repository This gem has really helped us clean up a lot of boilerplate Analytics code that is required to setup KISSmetrics and Google Analytics. It’s more of a wrapper for KISSmetrics, since that’s our primary Web Analytics tool, but it can be easily extended to other tools.

gem 'iscjs'

Link to GitHub repository A rails engine that includes all of the jQuery plugins we wrote at the ISC. Again, this is another great way to share libraries across different projects.

gem 'controller_support'

Link to GitHub repository This gem is a helper for a convention we’re using to manage Rails Controllers. Also mentioned here. If you want to start gemifying your libraries, I recommend reading this post from Pivotal Labs about working on gems locally from within your project. IMHO its a great way to migrate your libraries into a gem.


Comments are closed.

< Trimming the fat from your controllers