Extending Database_form to send Emails via Mailer extension

I recently used Radiant to complete a project that was to build a site that was sitting on top of a Ruby on Rails application. The easiest way for them to be able to manage it without my supervision was to have it built on top of a CMS. Of course, the future plan is always to sell something online or maybe build a community for their site, but right now, a static corporate website was well enough.

Having a static site built in Rails has its pros and cons. IMHO, as a static site, you've got the super easy, straightforward utilization of caching. So its going to be superfast! The templates can easily be updated, as well as the graphics. No fancy hardcoding necessary. But still, though its fairly easy for good RoR developers such as myself, its a lot harder for the common programmer who lives and thrives in easy markups.

On the onset of an exciting idea of building a new Rails powered application, the following options automatically crossed my mind:

  • Use a social networking template application such as LovdbyLess or Community Engine.
  • Use a blogging engine such as Typo or Mephisto
  • Use a CMS such as Radiant, and something else (see, I'm a bit biased here.. I don't remember any other CMS as good as Radiant).
  • Use a blank Rails application template that has a lot of plugins built in already, like Bort.
  • Alright, be a hero and start everything from scratch (maybe you have 3months dev time, full time? err)

A few more inputs from them and I understand that they only needed a static site wherein they can change the contents at any given time, and maybe sell some stuffs online soon. And, now the new baby is born out of Radiant!

Radiant is just a gem. Its installation is very easy, and super fast. I really didn't have much trouble. But the thing is, I wanted to have more control over the code itself, since I will be tweaking the Radiant app into something that fits their needs, and my deliverables too. And so, if you end up with this decision, just unpack your radiant gem.

gem unpack radiant

Once you have the tree generated, you'll see that its a normal Rails application hiding in the gem. Now you can inspect the code, and have it tweaked according to your will. I made changes in some of the templates, stylesheets, etc, and added some extensions. One of the uses of the site would be to serve as a mail form where users can contact them via a feedback section. I checked out their Third Party extensions which was quite extensive enough. I found a couple of them that I really needed:

It was pretty sweet and less complicated. The only caveat I had to take note was that, you can't do the usual:

rake radiant:extensions::migrate

if you don't make sure that in your

vendor/extensions

you have the extension name separated by underscores not by dashes. I had to google this up just to know what went wrong after pulling a copy of the extension repository into my vendor/extensions folder.

The next problem was really tricky. After learning about extensions, I kept looking for ways to be able to use the mailer on my database_form page. I found the clue here;. Though it wasn't really what I needed. So, after randomly looking for an available solution, I quit and started to work on my own workaround. If I'm reinventing the wheel or if someone knows any other resource, please be sure to post it here. ;)

If you want to try this out, do this at your own risk. It works for me, that's all I can say.. and whoops, no test for this one. :) First, make sure that you have your mailer extension set up already and working. Next, get database_form and have it set up too and already working. Once you have these two, then let's move forward.

Your database_form page should have a mailer section. Your mailer section should at least have the following example information:

 subject: Confirmation notification for 
 from: noreply@sweetperceptions.com
 redirect_to: /contact-us/thank-you
 recipients:
 - email@email.com
 

In your vendor/extensions/database_form/app/models/database_form_page.rb, add this line to the method save_form in its else clause just before true:

send_static_email(form_data) unless self.parts.find_by_name('mailer').blank?

Then add the send_static_email method contents as such:

 def send_static_email(form_data)
 mailer_config = YAML::load(self.parts.find_by_name('mailer').content).symbolize_keys
 mail = Mail.new(self, mailer_config, form_data)
 mail.send
 end
 

After that, every entry to the database form will send the notification to your desired recipients. Hope this works for you guys wanting to have that feature set for Radiant.

Maricris Nonato

I turn tech ideas into websites using Ruby. I'm a budding Buddhist practitioner, passionate about health & fitness, and a cat fanatic!