Skip to content

Instantly share code, notes, and snippets.

View davydovanton's full-sized avatar
:shipit:
Working from home

Anton Davydov davydovanton

:shipit:
Working from home
View GitHub Profile

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

http://www.facebook.com/sharer.php?u={url}&t={title}
http://www.liveinternet.ru/journal_post.php?action=n_add&cnurl={url}&cntitle={title}
http://www.livejournal.com/update.bml?event={url}&subject={title}
http://connect.mail.ru/share?url={url}&title={title}
http://www.odnoklassniki.ru/dk?st.cmd=addShare&st._surl={url}&title={title}
http://twitter.com/share?text={title}&url={url}
http://vkontakte.ru/share.php?url={url}
http://www.google.com/buzz/post?message={title}&url={url}
http://del.icio.us/post?url={url}&title={title}
http://digg.com/submit?url={url}&title={title}&media=news&topic=people&thumbnails=0
@davydovanton
davydovanton / Ruby tips
Last active August 29, 2015 14:01
Ruby tips
# Check type class in STI in rails
Post.uniq.pluck(:type).each do |class_type|
method_name = class_type.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase + '?'
define_method method_name do
type == class_type
end
end
# Use mb_chars.uppcase for many varibles
first_name, middle_name, last_name = [:first_name, :middle_name, :last_name].map{ |i| user.send(i).mb_chars.capitalize }
@davydovanton
davydovanton / gist:dfd90a973e0a73347404
Created July 8, 2014 13:54
Convert pathogen plugins to vundle
for i in */.; do
cd $i
repo=$(git remote show origin | grep 'Fetch URL:')
re="Fetch URL: https://github.com/(.+)"
if [[ $repo =~ $re ]]; then
var="$var\nPlugin '${BASH_REMATCH[1]}'"
fi
cd -
done
" Tools for searching and replacing text in Vim
" Search for the current visual selection
fun! StarSearch(direction)
let old_reg = getreg('"')
let old_regtype = getregtype('"')
exe 'normal! gvy\<esc>gV'
let @/ = EscapeMagic(@")
call histadd("/", @/)
call setreg('"', old_reg, old_regtype)
#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
get '/' do
erb :index
end
get '/follower_viz' do
@davydovanton
davydovanton / example.rb
Last active August 29, 2015 14:26
ConditionVariable simple example
require 'thread'
mutex = Mutex.new
condvar = ConditionVariable.new
puts "start"
Thread.new do
puts "in thread"
sleep 5
puts "after thread sleep"

Zero downtime deploys with unicorn + nginx + runit + rvm + chef

Below are the actual files we use in one of our latest production applications at Agora Games to achieve zero downtime deploys with unicorn. You've probably already read the GitHub blog post on Unicorn and would like to try zero downtime deploys for your application. I hope these files and notes help. I am happy to update these files or these notes if there are comments/questions. YMMV (of course).

Other application notes:

  • Our application uses MongoDB, so we don't have database migrations to worry about as with MySQL or postgresql. That does not mean that we won't have to worry about issues with the database with indexes being built in MongoDB or what have you.
  • We use capistrano for deployment.

Salient points for each file:

@davydovanton
davydovanton / ffi_test.c
Last active September 2, 2015 18:05
ffi benchmarks
#include <stdlib.h>
#include <math.h>
int ffi_pow(int a, int n) {
return pow(a, n);
}
@davydovanton
davydovanton / gist:5aa215915488069614fc
Last active September 30, 2015 02:28
Simple jekyll plugin for create a html figure construction.
module Jekyll
module Tags
# Simple jekyll plugin for create a html figure construction.
# About figure:
# https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figure
#
# The figure tag should take this form:
#
# {% image_with_caption you_image_url %}
# This is caption block. This should be parsed as *markdown* [man](http://example.com/). Or not :)