bookmark_borderassert_tag for a hyperlink in a functional test’s response

I spent some time yesterday trying this out.
I am writing it here to save your time.

Here is the functional test’s code to check for a hyperlink with text “Back to Index” which links to the index action of the controller being tested.


link = find_tag :tag => "a", :content =>"Back to Inbox"
assert_equal @controller.url_for(:action => 'index', :only_path => true), link.attributes["href"]

It could have been done in a single assert_tag … but the statement becomes too long.

bookmark_borderRuby on Rails Presentation

I will be making a Ruby on Rails presentation at the August php meetup.

This would be my second presentation on RoR, after the one at BarCampDelhi.

The image “http://barcamp.org/f/FINAL-BAR-CAMP-LOGO2.png” cannot be displayed, because it contains errors.


And this time there will be no ppts 😉

Thanks to ValueOne for hosting the meetup.

bookmark_borderrails captcha and testing using mock objects

To prevent bots from Signing up on our application, we added a captcha to our User model. We used the validates_captcha plugin available at http://svn.2750flesk.com/validates_captcha/trunk

The plugin, selects text from a String array for the captcha challenges, so, Sur, my co-programmer, hacked the plugin a little to add random text and improved images.

After adding the plugin we realized that all our tests which validate the User model had started failing. Programmatcially a test case or a bot are not much different, they are both scripts.

Mocks to the rescue.

Here is what I wrote in a file named add_captcha_to_active_record.rb under test/mocks/test directory.


require File.expand_path(File.dirname(__FILE__) + 
"/../../../vendor/plugins/validate_captcha/lib/add_captcha_to_active_record")

module AngryMidgetPluginsInc #:nodoc:

	module Validations #:nodoc:

		# module Captcha
		module Captcha #:nodoc:

			# module InstanceMethods
			module InstanceMethods #:nodoc:

			private

				def validate_captcha(options = {}) #:nodoc:
				  
				end

			end

		end#module Captcha

	end#module Validations

end#module AngryMidgetPluginsInc

What I have done by adding this file is, I have asked the test scripts to use the validates_captcha method in /test/mocks/test/add_captcha_to_active_record.rb instead of the validates_captcha method in plugins/validate_captcha/lib/add_captcha_to_active_record.rb

Well actually Rails does all the magic, I just need to give the same file name as the one I want to mock and then define the method that I want to override.

The original validates_captcha method checks that the text entered on the signup screen is the same as that stored on the server. If these do not match it adds a validation error to the base class, the User class in our case.
In the mock validates_captcha method we just do nothing. So a validation error will never be added to the base class.

And now our tests are running and passing again.

I also noted the presence of a directory /test/mocks/development/ … well that means I can even mock classes in the development environment.
One of our application, connects to Paypal to make a payment and then returns back. During development we had commented out this code so that we are not sent to paypal everytime. Now I know how I can avoid commenting or changing the code by using mock objects.
I will be working on that soon and blog about my experiences here.

bookmark_borderWhat the Flock

I downloaded Flock today, just to test create blog posts directly from Flock into socialroots repository, as a part of the project.

But I ended up using it the whole day. Just didn’t feel like going back to FF. Both of them are built on top of the mozilla engine and have the same shortcuts, this really reduce the (psychological and habitual) barriers to the change.

I added a couple of feeds that I used to read via bloglines to Flock; and now i am writing this blog post also in Flock.

Have yet to try out flickr and delicious functions inbuilt in flock.

Even Flock UI seems better than FF ( or is it just that i am liking the change)

Give Flock a try if you still haven’t.

bookmark_borderemacs for windows: meadow

Meadow is a tool that I came across on my last visit to Japan.
It is a GNU Emacs imlpementation for Windows and is fairly popular in Japan.
Though i had tried the windows version of xemacs before, I like the meadow interface much more.

You can get it here.
There is an english version of the wiki here.

I sometime do a little bit of rails hacking on my windows pc too … so i installed the ruby mode.
If you would want to add ruby-mode, you can find ruby-mode.elc under your ruby installation.
Copy it to lisp/progmodes/ under meadows home.

To customize Meadow further… here is a good link ( tis in japanese, but you should understand the lisp)

bookmark_bordervalidating Rails association

Should ActiveRecord add validations based on associations defined in the models?


class User < ActiveRecord::Base 
  has_many :posts 
end 

class Post < ActiveRecord::Base 
  belongs_to :user 
end 

If i create an instance of a post with a user_id that does not exist in the Users table, I wish ActiveRecord threw up a validation error on saving this instance of post.
It does not do so by default, so here are the validations that I add to my models to implement this.


class User < ActiveRecord::Base 
  has_many :posts 
end 


class Post < ActiveRecord::Base 
  belongs_to :user 
  validates_associated :user 
  validates_presence_of :user 
end 


Now if I try and save a post without a user_id or with a user_id that is not found in the Users table, an exception is thrown.

Till now I always use to create foreign keys in my database, as a safety net. So that the db throws an error if my data is not consistent.
After adding these validations, I hope not to have foreign keys in the database anymore.

bookmark_borderPolymorphic Association in Rails

Here is an account of my first use of Rails 1.1’s polymorphism in Model associations.

I had looked at all the examples at the wiki and a couple of other blogs but still wasn’t clear.

You appreciate most, the problems that you solve yourselves 🙂

Here is the problem I solved using Polymorphic associations.

Consider the scenario where there are Users of an application; who can act as both buyers and providers. Each user can infact have multiple buyer and provider profiles.
The application also has a messaging system for users to contact each other.
As a part of the application requirement, we need to keep track of the profiles of the message sender and receiver.

A user logs in using the login name and password present in the User model. I am omitting details so please assume for this example that each logged in user, has either a buyer profile or a provider profile associated to it, when they use the messaging system.

Here are the model files

A user can have many buyer_profiles and many provider_profiles


class User < ActiveRecord::Base
  has_many :buyer_profiles
  has_many :provider_profiles
end

A buyer profile belongs to a user and can have many messages associated to it as a sender and as a receiver.


class BuyerProfile < ActiveRecord::Base
  belongs_to :user
  has_many :messages, :as => :sender
  has_many :messages, :as => :receiver
end

A provider profile also belongs to a user, and can have many messages associated to it as a sender and as a receiver.


class ProviderProfile < ActiveRecord::Base
  belongs_to :user
  has_many :messages, :as => :sender
  has_many :messages, :as => :receiver 
end

The message model holds the polymorphic magic, via the two interfaces sender and receiver. A message belongs to sender which can be polymorphic,either a buyer or a provider. And also a message belongs to a receiver, which again is polymorphic, either a provider or a buyer.


class Message < ActiveRecord::Base
  belongs_to :sender, :polymorphic => true
  belongs_to :receiver, :polymorphic => true
end

The code above requires that I have these four special fields in my messages table to handle polymorphism. Here is the migration for messages


class CreateMessagesTable < ActiveRecord::Migration
  def self.up
    create_table :messages do |t|
      t.column :sender_id, :integer
      t.column :sender_type, :string
      t.column :receiver_id, :integer
      t.column :receiver_type, :string
      t.column :subject, :string
      t.column :message, :string
    end
  end

  def self.down
    drop_table :messages
  end
end

I ran the following code on the console to check if everything is working as expected.


r = BuyerProfile.find(1)
s = ProviderProfile.find(1)
m = Message.new
m.sender = s
m.receiver = r
m.subject = "test subject"
m.message = "test message"
m.save!

And it worked 🙂

I could access profles and users from the message object like this.


m = Message.find(1)
m.receiver                 # refer to the receivers profile object
m.receiver.class          # get receiver type, where BuyerProfile or SellerProfile
m.sender                   # refers to the sender profile object
m.sender.class            # get sender type, where BuyerProfile or SellerProfile
m.receiver.user           # refer the User object of the message receiver
m.sender.user             # refer the User object of the message sender

Polymorphism saved me a lot of time and also a whole new table which I was thinking of adding to handle this functionality prior to polymorphism.

Added : And yes, now I am thinking about what I would need to write to get all the messages sent or received by a User.

bookmark_borderDelhi ruby enthusiasts meetup.

Love Ruby. Love Rails. Love Beer. Love Food. Do You?

Jon and I are organizing a ruby/rails enthusiasts meetup on Friday, June 2, 8pm at Piccaddily, PVR Plaza, C.P.

We’ll be exchanging views and experiences on Ruby and Rails over beer and food.

All Ruby enthusiasts who are willing to share the bill are welcome to join us.

A rsvp mail to me or jon would be appreciated.
Coz Picaddily gets crowded at weekends at times … and we might need to make a reservation.