Monday, July 16, 2012

Modules in Ruby - an overview

We can define any number of methods in a module. It simply holds all the defined methods and we can include/extend it in any class.
If you want to use all module methods access by an instance then include the module in that class.
If you want to use all module methods access by an class then extend the module in that class.
module Foo
  def foo
    puts 'foo called!'
  end
end

class Bar
  include Foo
end

Bar.new.foo # foo called!
Bar.foo # NoMethodError: undefined method ‘foo’ for Bar:Class

class Baz
  extend Foo
end

Baz.foo # foo called!
Baz.new.foo # NoMethodError: undefined method ‘foo’ for #


Hope this helps. Happy learning & coding :)

Tuesday, February 28, 2012

Rails 3.1.0 - functional/integration test to test AJAX

Integration test with ajax achieved by means of calling xhr like below
xhr :req_method, :path, :parameters, :headers

xhr :get, '/users/new' # new user action

xhr :delete, '/users/20' # delete user

In integration test, you need to specify full path to make it work.

Incase of Functional test, we can simply specify the name of action.

Cheers,
Vadivelan.K

Authlogic login access in rails functional test

We are shifted to use Authlogic, Devise rather than rails authorization plugin/gem. We can do Integration with detailed explanations/tutorials available for Authlogic.

In Rails test (functional/integration test in particular), we are in need to test whether the user is logged-in or not.
To start testing the user session, we need to use authlogic session. By using the following steps we can access Authlogic session in our rails test

- Add the following line in your test_helper.rb
require "authlogic/test_case"

- And add the method in your test_helper.rb
def user_login(user)
UserSession.create(user ? user : nil)
end

- Then inside your functional/integration test file
Add this to top of all test
setup :activate_authlogic

- Logged in with any existing user
user_login(users(:last))

You are logged in.
- User session is accessible.

Web application testing is much easier in Rails :)

Cheers,
Vadivelan.K

Run ruby script as a background process in windows

Hi Ruby folks,

I went across a scenario in need of running a ruby process in background particularly in Windows OS(in Unix based system we can use '&' to run any process/job in background).

To run any ruby program in background, do the following

- Take a copy of the file, which you needs to run.
- Save the file as .rbw extension.
- Use the command
start "Your job description" ruby D:\Ruby\Jobs\particular_job.rbw

- Once it started running, You got a new console window with the given message on top and process status in console.

Cheers,
Vadivelan.K