Skip to content

Instantly share code, notes, and snippets.

View reu's full-sized avatar

Rodrigo Navarro reu

View GitHub Profile
@reu
reu / gist:1433464
Created December 5, 2011 12:37
capybara-hax
class ActiveRecord::Base
mattr_accessor :shared_connection
@@shared_connection = nil
def self.connection
@@shared_connection || retrieve_connection
end
end
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
@reu
reu / .zshrc
Created October 20, 2011 18:11 — forked from bernardeli/.zshrc
highlighting code for keynote with highlight
first: brew install highlight
second: add to your .zshrc (or .bashrc)
function hlr {
highlight --syntax ruby -k Menlo -K 20 -s edit-xcode $1 | pbcopy
}
third: paste the highlighted code to your Keynote talk.
require "socket"
socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
socket.bind(Socket.pack_sockaddr_in(3000, "localhost"))
socket.listen(1)
loop do
connection = socket.accept[0]
connection.gets.match /name=(.+) (.+)/
connection.write "#{$2} 200 OK\nContent-Type: text/html\n\nHello #{$1}"
connection.close
end
# simple preforking echo server in Ruby
require "socket"
# Create a socket, bind it to localhost:2424, and start listening.
# Runs once in the parent. All forked children inherit the socket's
# file descriptor.
acceptor = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
address = Socket.pack_sockaddr_in(4242, "localhost")
acceptor.bind(address)
acceptor.listen(10)
module SugarScopes
def like(query)
relation = clone
query.each do |column, value|
relation = relation.where(arel_table[column].matches(value))
end
relation
end
def asc(column = :id)
class User
has_many :posts
end
class Post
belongs_to :user
has_many :comments
def self.search(comment_description)
joins(:comments).where(:comments => { :description => comment_description })
class String
def encrypt(options = {})
crypt :encrypt, options
end
def decrypt(options = {})
crypt :decrypt, options
end
private
@reu
reu / gist:1244065
Created September 27, 2011 01:41
AR::Relation with attr_encrypted monkey patch
module AttrEncryptedRelation
def self.included(base)
base.class_eval do
alias_method_chain :build_where, :attr_encrypted
end
end
private
def build_where_with_attr_encrypted(opts, other = [])
@reu
reu / routes.rb
Created September 18, 2011 19:59
Dynamic root_url based on the user being logged or not
root :to => "home#show", :constraints => UserLoggedConstraint.new(false)
root :to => "profile#show", :constraints => UserLoggedConstraint.new(true)
@reu
reu / gist:1211986
Created September 12, 2011 18:24
iptables
-A INPUT -i lo -j ACCEPT
-A OUTPUT -o lo -j ACCEPT
-A OUTPUT -p udp -o eth0 --dport 53 --sport 1024:65535 -j ACCEPT
-A INPUT -p udp -i eth0 --sport 53 --dport 1024:65535 -j ACCEPT
-A OUTPUT -o eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p tcp -i eth0 --dport 22 --sport 1024:65535 -m state --state NEW -j ACCEPT
-A INPUT -p tcp -i eth0 --dport 80 --sport 1024:65535 -m state --state NEW -j ACCEPT
-A OUTPUT -j ACCEPT -m state --state NEW,ESTABLISHED,RELATED -o eth0 -p tcp -m multiport --dport 80,443 -m multiport --sport 1024:65535
-A INPUT -j ACCEPT -m state --state ESTABLISHED,RELATED -i eth0 -p tcp
-A INPUT -j DROP