Showing posts with label plugins. Show all posts
Showing posts with label plugins. Show all posts

Tuesday, March 10, 2009

using multiple contexts block in test case with shoulda

Test::Unit which is a default unit testing framework with rails. It has only one context for each test case class. If we can have more than one context per class than it could reduce number of files and classes that we are required to make to test cases in multiple contexts.


Answer to this issue is support for multiple contexts in Shoulda. Shoulda is rspec like testing framework built upon Test::Unit. Shoulda allows you to have multiple context with thier own setup and teardown methods. For example see this context block in a test case




class UserTest < Test::Unit::TestCase
context "with user" do
setup do
@user = User.create(:login => "john", :password => "secret" )
end

should "be able to authenticate" do
assert User.authenticate("john", "secret")
end

should "return authenticated user" do
assert_equal @user, User.authenticate("john", "secret")
end
end
end


For more details on shoulda testing framework take a look at official rails wiki page

Wednesday, July 4, 2007

Using Gravatar plugin to embed avatars in rails views

Gravatar offers service to keep your globally recognizable avatars (gravatar). This tiny plugin offers rails view helper to embed gravatars in your views. It can also be used in blog, forums applications to insert gravatar to be visible instead of email. Here is how to install it:

$ ruby script/plugin install http://tools.assembla.com/svn/hasham/plugins/gravatar_tag


Here is sample usage in an erb template:

<%= gravatar_tag "user@domain.com", :size => "60x60"%>


The first parameter which is a email that user signed up with on gravatar.com is required. The other parameters are same as rails image_tag view helper.

Thursday, June 28, 2007

Ignoring common exceptions in exception_notification plugin

Exception Notification is a great plugin, that helps to you to get trace of exception on your app running in production mode. Without this plugin you probably would have to SSH to production machine and tail the production.log file to get last exception trace which is extremely inconvenient to do. FTP to get to production.log is also an option but production.log files tend to become too large. only problem is that you may get too many emails form this plugin especially when you have just launched your site in production mode.

some of the exception are not too much intresting like typical ActiveRecord::RecordNotFound exceptions, To ignore these kind of trivial exceptions you must overide it in rescue_action_in_public method of your application.rb file.

Here is how you do it:



def rescue_action_in_public(exception)
case exception
when ::ActiveRecord::RecordNotFound,
::ActionController::UnknownController,
::ActionController::UnknownAction,
::ActionController::RoutingError
render_404
else
render_500
deliverer = self.class.exception_data
data = case deliverer
when nil then {}
when Symbol then send(deliverer)
when Proc then deliverer.call(self)
end
ExceptionNotifier.deliver_exception_notification(exception, self, request, data)
end
end