Drupal on Rails

My job at Oregon Sate University Central Web Services has me mostly focused on Drupal. We use Drupal for most of the university websites, and it does a great job. However while I've been slogging trough old PHP code, the rest of our developers have been working on state-of-the-art Ruby-on-Rails apps. So, not to fall too far behind I've been working on building my own Rails apps.

Whenever I want to learn a new development environment I re-write my Ride Log. This app started life as a Perl CGI. After a number of versions as a Perl app I re-wrote it as a PHP service with clean HTML and JavaScript on the front end. The current production version is a Drupal module. I even did a version using the Google App Engine. So when it came to learning Ruby-on-Rails it was a natural choice. Rails is tightly tied to a database and my rides database has over 900 records.

Building the Rails app was pretty straight forward really, once I got used to all those rake this, rails that commands. When it came time to build the controllers I noticed that Rails adds a JSON interface out-of-the-box. Sure enough when it was all put together I could get all of the data as JSON by adding .json to the URL. Very cool. I used to do a lot with XML, but lately I've been sold on JSON as a superior way to ship data back and forth between apps.

So this got me thinking. Rails apps are great for building complex data handling apps, but it has always seemed wasteful to have to build the UI for each app. With Drupal you get the UI plus user authentication out of the box. Wouldn't it be nice it we could use Drupal for the front end and Rails for the back end of our apps. With JSON maybe we can.

The first test was to see if I could setup Drupal as a consumer of data from a Rails app. The broad plan was to create a content type that matched the ride data, and use the Feeds module to read in the data and create the nodes. To make this work I needed to install a couple of modules:

These have some dependencies but Drush took care of downloading and getting them all installed. Once I had the modules I proceeded as follows:

  1. Create the new Content Type adding fields to match the data at hand
  2. Create a new Feeds Importer as follows:
    1. Use the HTTP fetcher
    2. Use the JSON Path Parser
    3. Choose the Node processor
    4. Configure the mappings between the JSON data and the Drupal fields
  3. Run the import
  4. Build a nice View to display all the new data

That's all it took. In less than an hour I had built the whole thing, imported over 900 records, and built a View to display them as a nicely formatted table.

So Drupal as a Rails data consumer is pretty easy, Once the data is in Drupal you can use Views to sort, filter, and display the data in any way imaginable, and with a lot less work than doing the same in a Rails app. Also the Feeds importer can be configured to run on a regular basis and keep the data updated from the source.

But a data consumer is only half of the picture. Can Drupal provide data to a Rails app? Examining the Rails controller further reveals that is is setup to accept a POST of JSON data at the same URL as the GET. On the Drupal end there is a module called Views Datasource that is capable of outputting a JSON feed from a View. One further component will be needed to take this JSON and POST it to Rails, or it may be easier to configure Rails to fetch the data. Say tuned to the next installment to find out.