Thursday, January 24, 2013

Using pry in test environment


Is it possible to use pry when I am running my tests? Sometimes when I am running specs for legacy code, I need a way to experiment with the existing code.


Answering my own question. Yes, it is possible. Just add binding.pry in the production code, run the specs, pry will stop at the line where you have the binding in the terminal window where you have the test running. You can also use pry-nav to use the ruby debug familiar commands step, next and continue while you debug the code.

Installing Ruby 2.0 on Mac OS 10.8.2 using RVM

1. Make sure you have the latest RVM
     rvm get head
     rvm reload


$ brew install autoconf
$ brew install automake
$ brew install libyaml

$ rvm install ruby-head

Stolen from the blog post instructions How to install Ruby 2.0 (ruby-head) with RVM

Creating a New File in a Folder in Textmate 2

Textmate 2 does not have the quick shortcut to create a file from a selected folder. Here is a workaround:

1. Select the folder where you want the new file.
2. Hit Option+Command+N keys to create a new file.
3. Add the content and save the file. It will get saved in the selected folder.

Tuesday, January 22, 2013

To open a bundled gem, set $EDITOR or $BUNDLER_EDITOR


1. bundle open rails
      gives that error.
2. Add :
      export BUNDLER_EDITOR=mate
    to ~/.bash_profile

Thursday, January 17, 2013

How to push branch to remote

To check-in your new branch you created locally : 1. git checkout -b your_branch 2. git push -u origin your_branch

Friday, January 11, 2013

Wednesday, January 09, 2013

Configuring Moonshine to use Apache XSendFile for Rails 3.2


1. rails plugin install git://github.com/railsmachine/moonshine_xsendfile.git
2. In the Moonshine manifest file:
    configure :xsendfile => {:x_send_file_path => '/absolute/path/to/download/url'}
    recipe :xsendfile
   
    This will work even if you have subdirectories under the /are directory.
3. In production.rb, uncomment the line:
    config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache

4. In your controller:
      send_file '/absolute/path/to/download/url/file.pdf', type: 'application/pdf'

Thursday, January 03, 2013

Rails 3.2, Factory Girl


1. Include :
 
    group :development, :test do
 gem 'factory_girl_rails'
end

in Gemfile

2. bundle install
3. create factories.rb under spec folder
4. Add the factory definitions in it :

FactoryGirl.define do
 factory :user do
   email 'elinora@zepho.com'
   password 'welcome'
   primary_paypal_email 'elinora.price@zepho.com'
 end
 # product factory with a belongs_to association for the user
 factory :product do
   name 'TDD Workbook'
   price 49.99
   user
   thanks_page 'www.zepho.com/thanks.html'
   cancel_page 'www.zepho.com/cancel.html'
   sales_page 'www.zepho.com/sales_page.html'
   download_page 'www.zepho.com/download.html'  
 end
end

In this definition I have factory for user has_many :products relationship.
5. Create a support directory and include controller_macros.rb in it:
module ControllerMacros
 def login_user
   @request.env["devise.mapping"] = Devise.mappings[:user]
   user = FactoryGirl.create(:user)
   # user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the confirmable module
   sign_in user
 end
end
6. In spec_helper.rb add the devise helpers:
RSpec.configure do |config|
 ...
 config.include Devise::TestHelpers, :type => :controller
 config.include ControllerMacros, :type => :controller
 ...
end
7. In your controller spec, now you can do this:
 
   context 'User is logged in' do
    before do
      login_user
    end

    it 'should render index page' do
      get :index

      response.should be_success
    end
  end
 


Monday, December 31, 2012

How to Create DNS Records using Linode API


dns = Fog::DNS.new(provider: 'linode',
                                    linode_api_key: 'your-linode-api-key-goes-here')
zone = dns.zones.create(domain: 'your-domain.com',
                 email: 'admin@your-domain.com')
zone.nameservers

Create a www version of your site and point to the right IP.
record = zone.records.create(value:  '1.2.3.4', name: 'your-domain.com', type: 'A')

To make www.your-domain.com go to the same place, use a cname record:
record = zone.records.create(value: 'your-domain.com', name: 'www', type: 'CNAME')

Thursday, December 13, 2012

to_yaml method y broken in Ruby 1.9


vi ~/.irbrc
YAML::ENGINE.yamler = 'syck'
Save. Now you will be able to use the method y to dump objects in yaml format.

error: The following untracked working tree files would be overwritten by merge: Library/Formula/pbrt.rb



cd /usr/local
git reset --hard origin/master
brew update


Rails 3.2 Tagged Logging

MyLogger = ActiveSupport::BufferedLogger.new(Rails.root.join('log/my_tagged.log'))
MyTaggedLogger.tagged("PAY") { MyTaggedLogger.info "Hello I am working!" }

Wednesday, December 05, 2012

Tuesday, December 04, 2012

Monday, December 03, 2012

How to install local gem


1. Download the .gem file.
2. gem unpack file.gem .
. is the current directory
3. Specify in Gemfile:
   gem 'name-of-gem', '0.1.0', :path => "/path/to/the/unpacked/gem/directory"
4. bundle install

Friday, November 30, 2012

Turn off generating css.sass and js.coffee files


config.generators.stylesheets = false
config.generators.javascripts = false

in application.rb


Wednesday, November 28, 2012

Install Ruby 2.0 Preview 1 on Mac OS

$ brew install libyaml
$ rvm install ruby-2.0.0-preview1 --with-openssl-dir=$HOME/.rvm/usr --verify-downloads 1

Thursday, November 08, 2012