Tuesday, October 23, 2007

Autocomplete in Rails 2.0

1. Install the auto_complete plugin.
script/plugin install http://svn.rubyonrails.org/rails/plugins/auto_complete
2. Turn off CSRF protection. Read Ryan's post.

You will get ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3.7707/lib/action_controller/request_forgery_protection.rb:73

error if you don't turn it off.

Thursday, October 18, 2007

Testing Rails 2.0 Http Authentication

Peepcode Rails 2 - New features for your applications by Ryan Daigle shows one way of testing your controllers that use Rails 2.0 http authentication. Another way would be to use mocks. I will post an example when I get back from TDD with Rails training here in Columbus, Ohio.

Testing ActiveMerchant Billing

There are some good resources for ActiveMerchant billing plugin such as:

1. Processing Credit Cards
2. Active Merchant and AuthorizeNet
3. Active Merchant Thread
4. Good blog post on ActiveMerchant

Gotchas:

1. The transaction key that is provided by AuthorizeNet expires automatically in 24 hours. So you must recreate it. Login to your AuthorizeNet test account, go to settings -> Create a new Transaction key.

Use this your login id and the newly generated transaction key in the following code.

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

class ActiveMerchantTest < Test::Unit::TestCase
include ActiveMerchant::Billing

def test_gateway
ActiveMerchant::Billing::Base.mode = :test

creditcard = ActiveMerchant::Billing::CreditCard.new(
:first_name => "dummy",
:last_name => "dummmy 2",
:number => "4779139500118580",
:verification_value => "410",
:type => "Visa",
:month => "10",
:year => "2008"
)

options = {
:name => "Bugs Bunny",
:email => "bbunny@gmail.com",
:phone => "4048615540",
:card_code => "410",
:order_id => "12345",
:description => "Conference reservation",
:billing_address => {
:address1 => "1234 Facke street",
:city => "Wichita",
:state => "Kansas",
:zip => "27606",
:country => "US" }
}
# If you do not immediately disable the old value, it will automatically expire in 24 hours.
gateway = AuthorizeNetGateway.new({:login => "Your API Login ID",
:password => "Your Transaction Key"})

response = gateway.purchase(10000, creditcard, options)

puts response.inspect
assert response.success?
end
end

The output response:

Response

Sunday, October 14, 2007

Extremely Simple Calendar Integration for Rails

Finally found a calendar that is very easy to install and use. Nice calendar. I had to change the width for the select in css to 60px, since I am displaying time also. The syntax:

<%= calendar_date_select_tag "event[departure]",
@event.departure.to_s,
:time => true %>

Upgrading Rails 1.2.5 version

ERROR: While executing gem ... (OpenURI ::HTTPError)
404 Not Found
To resolve this issue during upgrade:

1. sudo gem update --system

2. sudo gem install rails --include-dependecies

#2 worked when the server came back up.

Saturday, October 13, 2007

Testing Emails in Rails 2.0

There is a new assert_emails that can be used like:

assert_emails 1 do
UserMailer.deliver_signup_notification(@user)
end

There is also negated version : assert_no_emails that takes a block.

Friday, October 12, 2007

How to use alias for Textmate Rails projects

My ~/.bash_profile looks like this:

PATH="/usr/local/subversion/bin:$PATH"
PATH="/opt/local/bin:$PATH"
PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"
export PATH
PS1='\t:\w > '
export EDITOR='mate -w'
export JAVA_HOME=/usr
export MANPATH=$MANPATH:/opt/local/share/man
export INFOPATH=$INFOPATH:/opt/local/share/info

export atom="$HOME/work/atomizer"
export ard="$HOME/work/zepho/remote/trunk"
export astro="$HOME/clients/ge/astroceleb"
export star="$HOME/clients/ge/starstories"

alias matom="mate $atom"
alias mard="mate $ard"
alias mastro="mate $astro"
alias mstar="mate $star"
alias off="sudo shutdown -h now"

Example : matom opens the atomizer project.

How to configure Western Digital External Hard Disk for sharing on a network

1. Connect the WD MyBook to the Linksys router using USB cable.
2. Open the admin interface using the browser. In the Storage tab create a new share.
3. Make sure that All Partitions share is selected.
4. Open the finder and browse to the Network -> MsHome folder. Click on Linksyswrt350N and select the share that you created. Click ok. Now you can view the external drive from all the computers connected to the router.

Still have not figured out how to pick up the changes on the external drive from the computer that did change it. I have to reconnect to see the modifications. There has to be a better way.

How to setup mac ox for Facebook call back URL

1. Type ifconfig en0 in the terminal. The output will be like:

en0: flags=8863 mtu 1500
inet6 fe80::211:24ff:fed6:3436%en0 prefixlen 64 scopeid 0x4
inet AAA.XXX.Z.YYY netmask 0xffffff00 broadcast 192.168.1.255
ether 00:11:24:d6:34:36
media: autoselect (1000baseT ) status: active


2. Go to Linksys, Applications and Gaming tab, Port Range Forwarding and give a name for the application, specify start and end port (ex 3000 &B 3010), Protocol is both, To IP Address will have AAA.XXX.Z.YYY value, check the enabled box. Click on Save Settings.

3. In setup, DDNS tab, select DynDNS.org as the DDNS service and provide the username, password and hostname that you specified when you created an account with DynDNS.com site. System value is Capture(ddn.dynamic)Dynamic. All other values are default. Click Update and Save Settings.

4. Login to Facebook and specify the hostname and values for other parameters consistent with the above steps.

That's it. Now you can run the Facebook app and it will call your Rails app running on your machine.

Thursday, October 11, 2007

Tuesday, October 09, 2007

RSpec Not Implemented Feature

In the regular test unit, I do something like:

def test_load_upcoming_events_for_the_week
puts "Not Implemented : test_load_upcoming_events_for_the_week"
assert true
end

def test_load_top_4_categories
puts "Not Implemented : test_load_top_4_categories"
assert true
end

All my to do items goes into the autotest output and I have easy access to my to do list.

Integrating Flash With Rails

1. Install flashobject helper rails plugin
script/plugin install http://lipsiasoft.googlecode.com/svn/trunk/flashobject_helper

2. Include <%= javascript_include_tag :defaults %>

3. In the view: <%= flashobject_tag "/flash/demo.swf %>

Monday, October 08, 2007

Using Capistrano 1.4.1 when you already have 2.0

1. sudo gem install daemons gem_plugin mongrel mongrel_cluster --include-dependencies
2. sudo gem install capistrano -v 1.4.1
3. cap _1.4.1_ long_deploy

Sunday, October 07, 2007

Sake Headaches on Rails 2.0 Preview release

1. sudo gem install ruby2ruby -y
2. sudo gem install sake
3. sake -i bootstrap.rake

throws the error:

/usr/local/lib/ruby/gems/1.8/gems/sake-1.0.11/lib/sake.rb:315:in `parse': /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:2028:in `const_missing': uninitialized constant Sake::TasksFile::RAILS_ROOT (NameError)

RESTful authentication with Rails 2.0

1. script/generate authenticated user --include-activation creates Rails 1.2.3 style migration.

So, I changed it to:

class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users, :force => true do |t|
t.string :login, :email, :remember_token
t.string :crypted_password, :limit => 40
t.string :salt, :limit => 40
t.string :activation_code, :limit => 40

t.datetime :remember_token_expires_at, :activated_at
t.timestamps
end
end

def self.down
drop_table "users"
end
end

2. The routes.rb looks like:

ActionController::Routing::Routes.draw do |map|
map.resources :users
map.resource :session

map.connect '', :controller => 'home', :action => 'index'

map.with_options :controller => "users" do |page|
page.signup "/signup", :action => "new"
page.activate "/activate/:activation_code", :action => "activate"
end

# Install the default routes as the lowest priority.
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end

I had to define named route for signup because if I use new_user method in the application.rhtml, I kept getting error.

I don't know how to define the route in a RESTful way for activation. So I have named route for it.

3. In the form:

<% form_for @user do |f| -%>

<% end -%>

That's it. I am able to signup and activate using Rails 2.0. Of course I had to rename the .rhtml files to .html.erb. Now the Textmate shortcuts are broken, since it recogizes only rhtml files.

Thursday, October 04, 2007

Avoid Heavy Response Processing in Rails

I am half-way through the Topfunky's Peepcode Code Review pdf book. It is very well written.

In my previous job, I had to find a way of processing records that was created by users and send them to an external vendor. My pointy haired boss told me to spawn a thread for this purpose. I suggested using some kind of messaging system for obvious reasons. In the end we compromised and settled for Rake task and cron job combination.

Peepcode's book talks about this issue in detail.

Tuesday, October 02, 2007

uninitialized constant Rails::Initializer::MemCache

1. sudo port install memcached
2. sudo gem install memcache-client

That got rid of the error message.