Skip to content

Instantly share code, notes, and snippets.

View valterbarros's full-sized avatar
👊
Code makes me happy

Valter Barros valterbarros

👊
Code makes me happy
View GitHub Profile
@lucasmazza
lucasmazza / xhr.rb
Created November 24, 2010 16:43
Using xhr with rspec + rack/test
module XhrHelpers
def xhr(path, params = {})
verb = params.delete(:as) || :get
send(verb,path, params, "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest")
end
alias_method :ajax, :xhr
end
RSpec.configuration.include XhrHelpers, :type => :controller
@mateusg
mateusg / devise.pt-BR.yml
Last active December 23, 2023 15:15 — forked from alexandreaquiles/devise.pt-BR.yml
pt-BR translations for Devise
# encoding: UTF-8
# pt-BR translations for Devise
pt-BR:
devise:
confirmations:
confirmed: "Sua conta foi confirmada com sucesso. Você está logado."
send_instructions: "Dentro de minutos, você receberá um e-mail com instruções para a confirmação da sua conta."
send_paranoid_instructions: "Se o seu endereço de e-mail estiver cadastrado, você receberá uma mensagem com instruções para confirmação da sua conta."
failure:
already_authenticated: "Você já está logado."
@ericsaboia
ericsaboia / gist:994614
Created May 27, 2011 03:58
Changelog ActiveRecord:Base Rails 3.1
# ActiveRecord::Base#dup and ActiveRecord::Base#clone semantics have changed to closer match normal Ruby dup and clone semantics.
# Calling ActiveRecord::Base#clone will result in a shallow copy of the record, including copying the frozen state. No callbacks will be called.
# Calling ActiveRecord::Base#dup will duplicate the record, including calling after initialize hooks. Frozen state will not be copied, and all associations will be cleared. A duped record will return true for new_record?, have a nil id field, and is saveable.

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

@wejrowski
wejrowski / block-to-partial
Created August 17, 2011 22:10
Passing Blocks to Rails partials
<%
#
# I originally needed to create a sidebar partial that had a div area where I could place custom links into. Finally found it.
# When I was trying to figure this out before and realized I needed to pass the view into :layout, rather than just using render 'layout'. Not sure why the block can't be passed otherwise but nonetheless...
#
# For explanation of options see
# http://guides.rubyonrails.org/layouts_and_rendering.html#options-for-render
#
%>
@tiagodavi
tiagodavi / variantes.php
Created November 24, 2011 02:40
Variáveis Variantes
<?php
//(Variáveis Variantes)Não é só o conteúdo da variável que pode mudar mas também seu nome
$variavel = 'nome';
// $$ Cria uma nova variável representada pelo contéudo de $variavel
$$variavel = '@auhtor: Tiago Davi';
//=> @auhtor: Tiago Davi
echo $nome.'<br>';
$frameworks = array(
'codeigniter' => 'leve, simples, flexível e baixa curva de aprendizado',
@dblock
dblock / failures_formatter.rb
Created May 4, 2012 19:52
RSpec tests broken up into suites with retry.
# inspired by https://github.com/rspec/rspec-core/pull/596
require 'rspec/core/formatters/base_formatter'
module RSpec
module Core
module Formatters
class FailuresFormatter < BaseFormatter
def dump_failures
return if failed_examples.empty?
@aspyct
aspyct / sort.rb
Last active October 29, 2023 03:08
Ruby implementation of quicksort, mergesort and binary search
# Sample implementation of quicksort and mergesort in ruby
# Both algorithm sort in O(n * lg(n)) time
# Quicksort works inplace, where mergesort works in a new array
def quicksort(array, from=0, to=nil)
if to == nil
# Sort the whole array, by default
to = array.count - 1
end
@jkappers
jkappers / src.js
Created October 15, 2012 15:06
Force line wrapping in SVG text tags
(function() {
var svgns = "http://www.w3.org/2000/svg";
function forceTextWrappingOn(node, width) {
console.log(node);
var chars = node.firstChild.nodeValue.split(' '),
x = parseInt(node.getAttribute('x'), 10),
y = parseInt(node.getAttribute('y'), 10),
index = 0,
tspan, tspanWidth, textNode
@partkyle
partkyle / delegates.coffee
Created November 23, 2012 23:57
Coffeescript Delegates
class Something
constructor: ->
@delegateObject = new Messager
class Messager
testing: (int) ->
console.log int
woot: (bool) ->
console.log bool
nothing: (array) ->