Skip to content

Instantly share code, notes, and snippets.

View mmacedo's full-sized avatar

Michel Pavan Macedo mmacedo

View GitHub Profile
@mmacedo
mmacedo / application_helper.rb
Created April 12, 2012 19:22
helper vs loop
module ApplicationHelper
def grid_for(collection, options={}, &block)
options[:columns] ||= 4
options[:class] ||= ''
content_tag :table, :class => options[:class] do
content_tag :tbody do
collection.to_a.in_groups_of(options[:columns]).collect {|row|
content_tag :tr do
row.collect{|col| content_tag :td, (capture(col, &block) unless col.nil?) }.reduce(:+)
end
@mmacedo
mmacedo / gist:2475470
Created April 24, 2012 01:51 — forked from inossidabile/gist:1582000
JQuery ajax upload.onprogress binding
$.ajax
# ...
beforeSend: (xhr) =>
if xhr.upload?
xhr.upload.onprogress = =>
console.log arguments...
@mmacedo
mmacedo / grouping.rb
Created April 25, 2012 19:01
Rails array grouping for Mongoid
module Enumerable
def in_groups_of(*args, &block)
to_a.in_groups_of(*args, &block)
end
def in_groups(*args, &block)
to_a.in_groups(*args, &block)
end
def split(*args, &block)
@mmacedo
mmacedo / mult.pls
Created September 11, 2012 06:37
mult - PL/SQL
CREATE OR REPLACE TYPE Multiplication
AS OBJECT (
runningProduct NUMBER,
STATIC FUNCTION ODCIAggregateInitialize
( actx IN OUT Multiplication
) RETURN NUMBER,
MEMBER FUNCTION ODCIAggregateIterate
( self IN OUT Multiplication,
@mmacedo
mmacedo / preview_xterm.sh
Created November 9, 2012 19:13
Preview oh-my-zsh themes
#!/usr/bin/env bash
# Backup and modify .zshrc
cp $HOME/.zshrc $HOME/.zshrc_
sed -i "s/^ZSH_THEME=/\# \0/" $HOME/.zshrc
echo >> $HOME/.zshrc
echo "echo \"\\n\\n\\033[1;31m\$ZSH_THEME\\033[0m\\n\\n\"" >> $HOME/.zshrc
# Using xterm
CMD="xterm -ls -e /usr/bin/env zsh"
@mmacedo
mmacedo / gist:5018850
Created February 23, 2013 07:36
Stub all instances
# Create a class method (anyone, anywhere)
User.stub :save
# Save original constructor
new_method = User.method(:new)
# Stub constructor
User.stub(:new).and_return do |*args|
# Call original constructor
instance = new_method.call(*args)
# Save original save method
save_method = instance.method(:save)
@mmacedo
mmacedo / source.fish
Last active August 9, 2017 04:49
Source bash/zsh/ksh files
function _exec_with
set -l shell $argv[1]
set -l file $argv[2]
set -l code $argv[3]
set -l source
switch "$shell"
case bash zsh ksh
set source .
case '*'
@mmacedo
mmacedo / source.zsh
Created July 28, 2013 01:08
Source bash files
_bash_exec () {
file="$1"
code="$2"
function_names="typeset -f | sed '/^{\s*\\\$/,/^}\s*\\\$/d' | sed 's/\s*[(][)]\s*\\\$//' | sort"
variable_names="env | grep -v '^_|PIPESTATUS|COLUMNS|SHLVL\\\$' | sort"
# Create temp files to catch the change of variables and functions
functions_before=$(mktemp)
functions_after=$(mktemp)
@mmacedo
mmacedo / translate.fish
Created August 1, 2013 04:25
Rename file names and contents
#!/usr/bin/env fish
for tool in ack rename perl
if not which $tool >/dev/null
echo You need $tool to run this tool >&2
exit 1
end
end
if [ (count $argv) != 3 ]
@mmacedo
mmacedo / keyboardEverywhere.js
Created September 6, 2013 05:02
User script that adds shortcuts to Google Translate (updated version)
// ==UserScript==
// @name Google Translate Keyboard Everywhere
// @description 25 keyboard shortcuts (with on-screen help) to use GT at rapid pace.
// @icon http://translate.google.com/favicon.ico
// @version 1.0-20130224
// @namespace http://jakub-g.github.com
// @author http://jakub-g.github.com
// @license Apache 2.0
// @downloadURL https://raw.github.com/jakub-g/greasemonkey-userscripts/master/googleTranslate/keyboardEverywhere.js
// @userscriptsOrg http://userscripts.org/scripts/show/...