Skip to content

Instantly share code, notes, and snippets.

View emilpetkov's full-sized avatar

Emil Petkov emilpetkov

View GitHub Profile
@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 / 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)

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

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
@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| "\\'"}
@AndrewRadev
AndrewRadev / highlight_search.vim
Created July 29, 2011 09:10
Highlight trailing whitespace only in normal mode
autocmd! InsertLeave,WinEnter * call s:Highlight()
autocmd! InsertEnter * call s:Unhighlight()
hi link HighlightSpace Search
function! s:Highlight()
match HighlightSpace /\s\+$/
endfunction
function! s:Unhighlight()
match none
@skanev
skanev / README.markdown
Created August 11, 2011 13:35
Convert a <select> with <optgroups> to two <select>s that are interconnected

If you have a select with option groups:

<select data-nested-select="Select country">
  <option>Select city</option>
  <optgroup label="Bulgaria">
    <option value="sofia">Sofia</option>
    <option value="plovdiv">Plovdiv</option>
  </optgroup>
  <optgroup label="Sweden">

Stockholm

@AndrewRadev
AndrewRadev / LICENSE
Last active March 20, 2024 14:25
Execute a vim motion on the "next" text object
MIT License
Copyright (c) 2017 Andrew Radev
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
@AndrewRadev
AndrewRadev / tabularize.vim
Last active September 26, 2015 22:48
Tabularize mappings for specific use cases
" Tabularize mappings
"
" sa= -- align by equals
" sa> -- align by "=>"
"
" and so on. Note that any character can be entered and the mappings will
" attempt to align by that, in the simplest way possible.
"
" sa| -- equivalent to ":Tab/|"
"
@mxcl
mxcl / uninstall_homebrew.sh
Created August 26, 2011 11:25
Uninstall Homebrew
#!/bin/sh
# Just copy and paste the lines below (all at once, it won't work line by line!)
# MAKE SURE YOU ARE HAPPY WITH WHAT IT DOES FIRST! THERE IS NO WARRANTY!
function abort {
echo "$1"
exit 1
}
set -e