Monthly Archives: July 2012

Quick Tip: Rails for legacy oracle tables

Using Rails can be fun, however sometimes you have a legacy Oracle table that is bringing you down. This table was possibly created long ago, and does not have a sequence setup to handle the primary key. Since Oracle has no auto number type this can be a problem. Rails usually would have a hard time with this since it likes to stick to it’s “sensible defaults”. However, if we set a few things manually we can whip Rails and Oracle into shape.

First thing we need to do is set the table_name and primary_key attributes for our legacy table. Then we need to set the before_save property to increment our column id by selecting the highest legacy id and adding one to it. Pretty simple.

This is for sure not a best practice, but if you cannot create a sequence and need rails to work with legacy oracle, this will do the trick.

Underscore templates with Node.js

A while back I wrote a post about using underscore templates and how they can help to organize your application’s markup. Today we are going to talk about how we can take this one step further and use underscore templates within a node.js application to write a simple backbone generator.

Getting underscore

First thing we need to do is get underscore. NPM (Node package manager) will be our friend here. If npm is installed, we can get underscore simply by typing the following from the command line.

Creating our node.js program

First thing we need to do is create our node program. We will save this as file called bb.js. Inside bb.js add the code below. To get started we are requiring underscore, the node file system, and defining our template file.

Next, we need to make a function in bb.js that will generate our file based on the backbone-view template. (Which we will create later)

The template will be run from the command line using

Create File

The first thing createFile() does is look at the arguments passed to node. Since our command is going to be “node bb <view_name>” the view name will be the 3rd argument from process.argv. Process.argv is just an array containing the command line arguments. Next, if a view name was passed in we will setup a file path based on the current working directory and the view name.
Ex. path/to/current/directory/view_name.js.

Line 9 will use the node file system to read in our template file and convert it to a string. Finally, we will apply the _.template function using our templateFile string and view name as parameters. We will then write out the result to a file using the node file system.

Our backbone-view.js template file

As you can see the backbone view template is pretty simple. It takes the view name that was passed in and applies it in several different spots.

And that’s it. It is pretty easy to write a little node.js and underscore program to help with simple tasks such as writing out template files. Obviously this is a very simple example. In the next post we will combine this general idea with cli ( a command line tool for node ) to make a more powerful interface for generating files and folders with node.

Sharing Rails sessions with PHP, ColdFusion, and more!

Sometimes it does not make sense to store session within a cookie. It might be a size issue, or it could be sensitive information you do not want to go across the wire with each request. Whatever the reason, another option is to database the session information. Luckily rails makes it really easy to do this via active record store.

Active record store

To start databasing our sessions, we need to setup rails to use the active record store. To enable active record store we need to edit the config/initializers/session_store.rb file. We need to comment out the cookie store option and add the active record store option.

Creating the database

Next step is to create the session database table. To do that we will run the following commands…

Session data

When you create the session each created session will be entered as a record in the database. There will be id, session_id, data, created_at, and updated_at columns. The session_id will stored in a cookie and be used to retrieve the data column which contains the databased session information. The data column will store the information as a base64 encoded string. Like so.

Which can be marshaled to a ruby object upon request.

Making rails share

This works pretty well for storing sessions in rails. However, there is a problem. Ruby is using it’s internal marshaler when storing the session information. This means that any other language, such as php, coldfusion, etc cannot decode the data unless you write the marshaler 🙁

There may however, be a way around this. What if we could override rails and tell it to save the data as base64 encoded JSON instead of marshaling it? Every other language could then decode base64 strings and serialize them from JSON to native objects.

If we again edit our session_store.rb file, we can override some internal rails methods when handling sessions. Below we are overriding base the SessionStore class.

If you notice we are only overriding the marshal and unmarshal methods to preform a JSON.encode and JSON.decode instead of the traditional Marshal.load and Marshal.dump.

We can then easily read this information out in another language.