Skip to content
Tak Tran edited this page Jun 20, 2013 · 17 revisions

Rails

To start with debugger

script/server --debugger # rails 2
rake server --debugger   # rails 3

Rails application name

Rails.application.class.name

RSpec

Run tests

bundle exec rake spec
bundle exec spec --color spec/models/something.rb 

Pending tests

pending "Not done yet"

Focus specs

# spec_helper.rb
RSpec.configure do |conf|
    conf.filter_run_including :focus => true
end

# spec
describe "something", :focus => true do
end

Rake task to run a particular spec file

namespace "spec" do
  desc "Run individual spec. Can also pass in a line number."
  task :run, :spec_file, :line_number do |_, args|
    run_spec_cmd =  if args.line_number.nil?
                      "bundle exec ruby -S rspec --color #{args.spec_file}"
                    else
                      "bundle exec ruby -S rspec --color -l #{args.line_number} #{args.spec_file}"
                    end
    sh run_spec_cmd
  end
end

Mongomapper

To search for _id in MongoHub

{"_id": ObjectId("222222")}

Delete all with a condition

Model.delete_all(:attr => attr)

Comparators

From mongomapper querying)

patients = Patient.where( :last_name.gte => 'A', :last_name.lt => 'B' ).count
#=> 1803

From mongodb querying

db.myCollection.find( { a : { $gt : 3 } } );

Capybara

See https://gist.github.com/428105

Command line options

Modified from optionparser.

require 'optparse'

SCRIPT_NAME = "rename.rb"
OPTIONS = {}

optparse = OptionParser.new do|opts|
  # Set a banner, displayed at the top
  # of the help screen.
  opts.banner = "Usage: #{SCRIPT_NAME} [options] file1 file2..."

  # Define the options, and what they do
  OPTIONS[:verbose] = false
  opts.on( '-v', '--verbose', 'Output more information' ) do
    OPTIONS[:verbose] = true
  end

  OPTIONS[:quick] = false
  opts.on( '-q', '--quick', 'Perform the task quickly' ) do
    OPTIONS[:quick] = true
  end

  OPTIONS[:logfile] = nil
  opts.on( '-l', '--logfile FILE', 'Write log to FILE' ) do|file|
    OPTIONS[:logfile] = file
  end

  # This displays the help screen, all programs are
  # assumed to have this option.
  opts.on( '-h', '--help', 'Display this screen' ) do
    puts opts
    exit
  end
end

# Parse the command-line. Remember there are two forms
# of the parse method. The 'parse' method simply parses
# ARGV, while the 'parse!' method parses ARGV and removes
# any options found there, as well as any parameters for
# the options. What's left is the list of files to process.
optparse.parse!

puts "Being verbose" if OPTIONS[:verbose]
puts "Being quick" if OPTIONS[:quick]
puts "Logging to file #{OPTIONS[:logfile]}" if OPTIONS[:logfile]

ARGV.each do|f|
  puts "Argv: #{f}..."
end

Capistrano

cap [name] deploy:setup
cap [name] deploy

# Invoke commands
cap invoke

http://cheat.errtheblog.com/s/capistrano/

Common problems

  • Referencing incorrect repository url when deploying - if repository has changed, need to delete /shared/cache-copy
  • fatal: The remote end hung up unexpectedly - need to add repo to ~/.ssh/authorized_keys file. Can do a git clone from the server to do this.
Clone this wiki locally