Archive for rubyonrails

Nastiness of restful_authentication RSpecs

I use the plugin restful_authentication to handle logged in sessions. It makes modules to include in your controller and also makes specs if you have spec directory. All fine, but I cannot live with one login session for the websites; I need “admin” and “client” area. Each session will be verified against separate models, i.e. Client and User models in this case. The problem was with the spec for these tests that were generated by the plugin.

The generator also creates authenticated test helpers, which in my case lie in their own namespace. so I have:

/lib/client/authenticated_test_helper.rb
/lib/admin/authenticated_test_helper.rb

But the typical spec generated looks like this:

require File.dirname(__FILE__) + '/../spec_helper'

# Be sure to include AuthenticatedTestHelper in spec/spec_helper.rb instead.
# Then, you can remove it from this and the functional test.
include AuthenticatedTestHelper

describe User do
...

In my case I had to modify it to include from Admin::AuthenticatedTestHelper or Client::AuthenticatedTestHelper as it fits the model.

For no obvious reason, the tests fail. I finally found the problem with the generated spec that included the module

include Client::AuthenticatedTestHelper
describe User do
...

It includes the AuthenticatedTestHelper outside the describe block. This gets into trouble with another model spec when it is also called AuthenticatedTestHelper but different namespace.

Anyway, the fix is to include with in the describe block, as in:

describe User do
  include Client::AuthenticatedTestHelper
...

Comments

Sub-domains and Developer Testing

Sometimes you sell accounts to your web application with unique sub-domain per license. This is a neat way of handling accounts and gives you lot control.

For e.g. lets say you have built hotel-reservations-engine in Ruby on Rails and are planning to license it for many different hotels. The best way to do that would be use sub-domains to your advantage. There are many blog posts that tells you how to achieve this on Ruby On Rails and the production server.

My focus is though, how to get this working in your developer machine without any dilly-dally of hosts file.

Use Name Server To Your Advantage:

For e.g. lets say you manage your DNS records with tinydns which has a great feature of wild-card alias, and you add the following entries…

+local.example.com:127.0.0.1
+*.local.example.com:127.0.0.1

after propagation its all yours:

local.example.com => 127.0.0.1
sub1.local.example.com => 127.0.0.1
sub2.local.example.com => 127.0.0.1
sub3.local.example.com => 127.0.0.1

Note the * in the +*.local.example.com:127.0.0.1 which does the trick.

No more editing the hosts file, and it works for all developers pointing to their own machine. Nice!

Comments

Rails plugin restful_authentication a misnomer

It all started with a debate on #rubyonrails with ReinH about how less RESTful a session could be — mainly because it uses cookies. I suggested a resource such as:

  http://example.com/cart/:cart_id/items

or even this…

  http://example.com/user/:user_id/cart/items

…could be RESTful. Which ReinH neither agreed nor denied as far as I remember and asked “how many actually do this?”. Fairly none and agreed that he was right. I must really be honest and admit that I only knew a little about REST while debating.

Read the rest of this entry »

Comments

Rails Patch: validates_format_of doc missing allow_blank

Added a doc patch to the rails trac today to add the missing doc for allow_blank option in the validates_format_of.

Nothing big, but I noticed people asking question about this in the #rubyonrails.

Looks like this option is missing in many other validation methods, someone or I should submit another patch fixing them all.

Comments

Rails Plugin: Google Charts On Rails

Google recently made the API for google charts public. It takes parameters using URL query strings and returns png image for the chart.

What we have here is a small wrapper plugin in rails for the google charts.

It is simple to use:

GoogleChart.pie(10,20,40,30).to_url

would create something like this:

a sample pie chart

For more usage, visit the Google charts on Rails project.

Comments (6)

Rails: customising URL Path for map.resources

A rails patch that would help in customising the URL generated from the mapped resources.

Read the rest of this entry »

Comments

« Previous Page « Previous Page Next entries »