Thursday, January 26, 2012

RCov Tips

1. How to turn off rcov for specific sections in a file:
Start and end with :

#:nocov:

in your file.

2. How to exclude files from rcov:

In spec directory, edit rcov.opts

 append our path of files to –exclude option. Eg: –exclude “helpers/*,app/sweepers/*”.

Reference

http://psixty.wordpress.com/2010/06/22/how-to-exclude-files-in-rcova-code-coverage-tool-in-ruby/

Monday, January 16, 2012

How to uninstall all gems in global rvm ?

rvm use @global 
rvm gemset empty global

If you want to remove all gems for a particular gemset, replace global with your-gemset-name

gem "rake" is not installed

Problem uninstalling rake? Run the following command:
rvm use @global && gem uninstall rake

Thursday, January 12, 2012

How to disable acts_as_audited 2.0 rails plugin in Rails 3.1 and RSpec

Create disable.rb under spec/support folder and call disable_auditing method on your modes. For example:
Account.disable_auditing
Cart.disable_auditing

Monday, January 09, 2012

Using Reek gem in Rails 3.1 projects

1. gem install reek
2. reek lib/*.rb > myreport.txt

You can run reports for app/models/*.rb, app/controllers/*.rb and app/helpers/*.rb

rcov for Rails 3.1

1. rake spec:rcov
2. open coverage/index.html
3. add coverage/* to your .gitignore file.
4. $ rake -T rcov
rake spec:clobber_rcov  # Remove rcov products for rcov
rake spec:rcov          # Run all specs in spec directory with RCov (excluding plugin specs)

Rails Best Practices

1. gem install rails_best_practices
2. gem install ripper
3. From the root of the project run: 
rails_best_practices -f html .
to generate the report.

Sunday, December 25, 2011

You need to specify gem names as Strings. Use 'gem "test"' instead.

This can happen due to syntax error in your Gemfile. Fix the sytax error by manually checking this file. If that does not work delete the lines that you added to result in this problem and copy / paste from a working copy of a Gemfile.

Wednesday, December 07, 2011

Thursday, December 01, 2011

`test': unknown command 't' (ArgumentError)

To write tests using Test::Unit new syntax :

test "truth" do
  assert true
end

1. gem install test-unit
2. Code:

require 'rubygems'
gem 'test-unit'
require 'test/unit'


class ExampleTest < Test::Unit::TestCase
  def test_truth
    assert true
  end
 
  test "truth" do
    assert true
  end
end


3. Run: ruby example_test This will work on ruby-1.9.3-p0.

Reference:  Test::Unit and MiniTest with different Ruby versions

Wednesday, November 30, 2011

Installing Ruby 1.9.3 using RVM on Mac OS Lion

ERROR: The provided CC(gcc) is LLVM based, it is not yet fully supported by ruby and gems, please read `rvm requirements`.

1) Download Xcode 4.2.x via App Store.
2) Go to Applications folder and click Install Xcode
3) Download and install GCC from https://github.com/kennethreitz/osx-gcc-installer
4) In a new terminal run : export CC=gcc
and then run : rvm install 1.9.3
5) You may also want to upgrade ruby gems by running:
gem update --system

Wednesday, November 02, 2011

ActiveRecord::Relation, undefined method error

If you are using the active record relationships to access columns, for instance:

Article has many comments
article.comments.where("spam = ?", false).order("created_at asc")

This returns ActiveRelation object that does not fire the query. Only when you loop through the results you can access the columns of comments objects. If you want to force the call to the database just append "all" to it like this:

article.comments.where("spam = ?", false).order("created_at asc").all

Monday, October 24, 2011

(local out of date) error in Git when working with branches

1. git pull origin your-branch
2. git push origin your-branch

Step 1 brings your local branch to up-to date. Step 2 pushes your changes to the remote branch.

Sunday, October 23, 2011

invalid date format in specification: gem ruby

1) Upgraded ruby gems to 1.8.11 (gem update --system)
2) gem pristine --all

No luck.

Deleted all the gemspec that were causing problems: whenever-0.6.8.gemspec etc. Then bundle install made it work.

`build_extensions': ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError)

I was getting :
Installing eventmachine (0.12.10) with native extensions .rvm/rubies/ruby-1.8.7-p330/lib/ruby/site_ruby/1.8/rubygems/installer.rb:533:in `build_extensions': ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError)

during the installation of scheduler_daemon on Mac OS Lion. Upgraded XCode via appstore (download from appstore and install) to resolve this issue.

Friday, October 07, 2011

Integrating Tweet Button in Rails App

In Rails 3.0 app:

1) In Gemfile include:

gem 'tweet-button'

2) bundle install

3) In application_helper.rb include:

module ApplicationHelper
include TweetButton

... other methods...
end

4) In your partial:

<%= tweet_button(:via => "CreditCardLogic", :url => polymorphic_url(article), :text => "Check this out!") %>

To see this in action, check out my credit cards site.

Tuesday, September 27, 2011

Making a Branch the Master in GIT

git checkout cat_killer
git merge -s ours master
git checkout master
git merge cat_killer
git push

cat_killer is the branch you want to make the master.

Stolen from Stackoverflow post.

Tuesday, September 20, 2011

no mongrel gem found in sdk rubymine

Even if you are using RVM, Rubymine actually looks at the global installation for the gem. Install thin, mongrel etc on the global Ruby installation that is independent of any gemset.

How to start Thin server on a different port?

thin start -p 80

Friday, August 12, 2011

require that is compatible with ruby 1.8.x and 1.9.x

require File.expand_path(File.join(File.dirname(__FILE__), 'player'))

This makes : require 'player' work on both Ruby 1.8.x and 1.9.x