Skip to content

Instantly share code, notes, and snippets.

View emilpetkov's full-sized avatar

Emil Petkov emilpetkov

View GitHub Profile
@kbaribeau
kbaribeau / gist:1103102
Created July 24, 2011 21:06
Ruby single quote escaping madness
Ok, so I'm trying to replace a ' character with \' in a ruby string. I have working code, but it was
*way* too painful getting there for my taste.
Take a look:
ree-1.8.7-2010.02 :001 > single_quote = "\'"
=> "'"
ree-1.8.7-2010.02 :003 > single_quote.gsub(/'/, "\\\'")
=> ""
ree-1.8.7-2010.02 :004 > single_quote.gsub(/'/) {|c| "\\'"}
nnoremap << :call <SID>KeepCursorAfterShift('<<', -1)<cr>
nnoremap >> :call <SID>KeepCursorAfterShift('>>', 1)<cr>
function! s:KeepCursorAfterShift(cmd, modifier)
let column = col('.')
exe "normal! ".a:cmd
call cursor(line('.'), column + a:modifier * &shiftwidth)
endfunction

Transactions

As your business logic gets complex you may need to implement transactions. The classic example is a bank funds transfer from account A to account B. If the withdrawal from account A fails then the deposit to account B should either never take place or be rolled back.

Basics

All the complexity is handled by ActiveRecord::Transactions. Any model class or instance has a method named .transaction. When called and passed a block, that block will be executed inside a database transaction. If there's an exception raised, the transaction will automatically be rolled back.

Example

@skanev
skanev / wait_for_ajax_to_complete.rb
Created July 18, 2011 13:08
wait_for_ajax_to_complete cuke helper
module WaitForAjaxToComplete
def wait_for_ajax_to_complete
wait_until { page.evaluate_script('$.active') == 0 }
end
end
World(WaitForAjaxToComplete)
@avdi
avdi / throw-catch.rb
Created July 11, 2011 05:55
throw/catch demo code for RubyLearning article
require 'rubygems'
require 'mechanize'
MAX_PAGES = 6
def each_google_result_page(query, max_pages=MAX_PAGES)
i = 0
a = Mechanize.new do |a|
a.get('http://google.com/') do |page|
search_result = page.form_with(:name => 'f') do |search|
@skanev
skanev / close_hidden_buffers.vim
Created July 6, 2011 20:15
Close all hidden buffers in Vim
command! CloseHiddenBuffers call s:CloseHiddenBuffers()
function! s:CloseHiddenBuffers()
let open_buffers = []
for i in range(tabpagenr('$'))
call extend(open_buffers, tabpagebuflist(i + 1))
endfor
for num in range(1, bufnr("$") + 1)
if buflisted(num) && index(open_buffers, num) == -1
@AndrewRadev
AndrewRadev / close_hidden.vim
Created July 6, 2011 12:22
Close all buffers that are not currently opened
command! CloseHiddenBuffers call s:CloseHiddenBuffers()
function! s:CloseHiddenBuffers()
" Get the output of :ls as a string
redir => ls_output
silent ls
redir END
" Go through all buffer entries
for line in split(ls_output, "\n")
if line =~ '\d\+\s*\S\+\s*".*"' " then the listing contains flags
@sjl
sjl / markdown.vim
Created June 21, 2011 19:47 — forked from vim-voom/markdown.vim
Markdown folding for Vim
" folding for Markdown headers, both styles (atx- and setex-)
" http://daringfireball.net/projects/markdown/syntax#header
"
" this code can be placed in file
" $HOME/.vim/after/ftplugin/markdown.vim
func! Foldexpr_markdown(lnum)
let l1 = getline(a:lnum)
if l1 =~ '^\s*$'
@dimitarvp
dimitarvp / gemdetails.rb
Created June 18, 2011 08:32
Get online info about any gem existing at rubygems.org
# Alternative way of getting all Gem details using the official Rubygems REST API (thanks @qrush)
require 'open-uri'
require 'active_support/json'
require 'awesome_print'
ap ActiveSupport::JSON.decode(open("http://rubygems.org/api/v1/gems/#{ARGV[0]}.json"))
@drnic
drnic / $
Created June 10, 2011 15:20
never fear $ in tutorials again
#!/usr/bin/env bash
"$@"