Ruby on Rails Posts

A Production-Ready Rails 5 Email Workflow with Mailer Previews, Premailer and ActiveJob

A Production-Ready Rails 5 Email Workflow with Mailer Previews, Premailer and ActiveJob

Since the introduction of Mailer Previews and ActiveJob in Rails 4.1, the framework has truly set itself apart from others in the comprehensiveness and comfort of its workflow around emails. The framework’s creator, DHH, has espoused a perspective that mailers are views, and accordingly Rails now gives us an email workflow that is nearly identical to that around ordinary controllers and views.

I will walk through my complete email stack and workflow, which I have used very effectively across several production apps.

Validation Contexts in ActiveRecord

Validation Contexts in ActiveRecord

Validation contexts are a little-appreciated but immensely practical feature of Ruby on Rails’ object-relational mapper, ActiveRecord. I cannot count the number of times I have seen hacks around a problem for which a validation context would have been a perfect fit simply because this feature lives a bit under the radar and isn’t in every Rails developer’s toolbox.

What is a validation context, precisely? It is a way to constrain a model validation to a particular usage context for a record. This is similar to what you might achieve with something like state_machine, but far more lightweight.

Less Code
This desk is not mine

Less Code

These days I increasingly feel that I am in a minority in my deep appreciation – love, even – of the heaviest weight of heavyweight dynamic MVC web frameworks: Ruby on Rails. I have worked, now, with many of the more popular frameworks on both the server and client side. Flask, Django, Web.py, React, Angular, Express JS, Sinatra and others. I still come back to Rails.

I belive that, for the overwhelming majority of domains, it is the tool – if weilded with healthy reverence for its conventions and idioms – which leads to the most manageable, consistent, non-redundant and all-around sane codebases.

Pattern Vision Redux

A couple years ago I read an article DHH had posted to 37Signals’ “Signal vs. Noise” blog called Pattern Vision. I love that article not because it is at all profound. It isn’t. Kind of the opposite. I love it because I could have written it. The ways of thinking described in it are overwhelmingly common in the work I come across and live with day to day. Every new codebase I encounter seems to carry some new dubiously-applied patterns, some new collection of less-than-idiomatic directories, modules and classes for me to discover nested within “/app”.

Russian Doll Caching in Rails 4

Russian Doll Caching in Rails 4

Rails 4 nudges developers toward more pervasive use of fragment caching (and the so-called “russian doll caching” strategy of deeply nesting fragment caches) with the introduction of ActionView::Digestor and on-by-default appending of template digests to fragment cache keys. Though actually pretty simple in its implementation, this feature (paired with correct use of ActiveRecord’s “touch” relation option) solves most of the formerly enormous headaches you’d encounter (and still do today with pretty much every other framework) trying to implement an advanced caching strategy with largely dynamic content. I’ve been working with this technique for more than half a year now (initially using the Rails 3 “cache_digests” gem , which implemented the same functionality now available through ActionView::Digestor in Rails 4).