Currently the posts are filtered by: Development
Reset this filter to see all posts.

Category: Ruby on Rails
Date: 2012-05-04 22:45

Using SimpleCov with FactoryGirl

FactoryGirl is a great replacement for your Ruby-on-rails fixtures. They offer more flexibility for generating stubs and mack-ups for your automated testing.

SimpleCov is the test coverage tool for Ruby 1.9.x (Use RCov for Ruby 1.8.x).

Using both in conjunction requires two extra lines in your test/test_helper.rb file not covered by any documentation. This will prevent you from getting messages like "Factory not registered: " or "uninitialized constant Test::Unit::TestCase::FactoryGirl (NameError)".

My test_helper.rb file looks like:

Code:  Select all
require 'simplecov'
SimpleCov.start 'rails'
 
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
 
# The following two lines of code will reinitialize Factorygirl:
require 'factory_girl'
FactoryGirl.reload
 
class ActiveSupport::TestCase
...
end
Category: Javascript
Date: 2011-01-05 23:35

Fix jQuery .replaceWith() method to perform in IE8

This article applies to jQuery 1.4.4

The jQuery .replaceWith() method replaces a DOM-element and all of its children by another DOM-element. This function is ideal for use in Ajax applications.

The .replaceWith() method however internally uses the jQuery .remove() method which in Internet Explorer can be dramatically slow. There is a tric however to improve this performance radically by emptying the DOM-element first before removing it. 

It enhanced my performance in IE at least 10 times!

Here's the line:

jQuery(selector).empty().replaceWith(newContent);
Category: Javascript
Date: 2010-08-27 15:04

Show jQuery tabs promptly

jQuery tabs are just fabulous!

But if you have a lot of content then you might see your tabs being built up when jQuery applies them.

Here's the solution. Just hide the tabs HTML element initially:

<div id="my_tabs" style="display:none;">

Then use the following jQuery Tabs Event to display them when they are ready:

$('#my_tabs').tabs({ 
  show: function(event, ui) 
  {jQ(event.target).not(':visible').show();}
});

By using jQuery's .not(:visible) filter method show() is executed only once.

Category: Ruby on Rails
Date: 2010-08-20 23:06

HTML5 helpers for Rails 2.3.x

Eager on using HTML5 in your Rails projects without upgrading to Rails 3?

For those I've ported the HTML5 FormHelpers and FormTagHelpers from Rails 3 back to Rails 2.3.x. So you can now use:

 

<%= f.email_field(:email) %>

<%= email_field_tag(:user, @user.email) %>

All helpers are Rails 3 compliant so after updating to Rails 3 just remove the plugin.

Category: Ruby on Rails
Date: 2010-07-27 15:29

Rails will_paginate with acts_as_ferret

I wanted to use pagination in one of my Rails projects where I was also using the acts_as_ferret plugin. Now normally one would use

@collection=Model.paginate(:page=>params[:page])

in the controller and in the view render with:

<%= will_paginate @collection %>

But with acts_as_ferret the controller will contain:

@collection=Model.find_with_ferret(params[:search], {:page=> params[:page], :per_page=>Model.per_page})

This is not a problem however. Acts_as_ferret is compatible with will_paginate. Your default will_paginate() helper will still be working.