Store Rails views on database
2011-06-12
The ideal way is to store views on a database, indeed there are some CMSs that solve this problem with similar approaches, like Radiant and Locomotive.
However, I wanted something that let me to reuse this feature on several projects (because a custom CMS may have different features), and to store only certain views, according to specific content and needs.
So, I’ve developed Panoramic, a simple gem inspired from some ideas found in José Valim’s excellent book Crafting Rails Applications. Simply put, once Panoramic is installed, you can store your views on database. It works through an implemetation of ActionView::Resolver
class: Rails will lookup views as if they were on filesystem. In controller, you’ll tell where and when to use those views, using prepend_view_path
and append_view_path
on a controller or action basis (they are both implemented as class and instance methods). Depending from the method you’ll use, Rails will respectively look for views on database, then it will fallback on filesystem, or viceversa. Quite easy, right?
I’ll not show any code here, because there’s a full explanation on Panoramic’s README. As a final note, at the moment it works only with ActiveRecord, but I’ve planned to extend support to DataMapper and Mongoid as soon as possible. It’s very easy to implement, the main problem is about testing: maybe I’ll write some rspec’s custom matchers to reuse with different ORMs, like carrierwave.