Skip to content

Instantly share code, notes, and snippets.

View reu's full-sized avatar

Rodrigo Navarro reu

View GitHub Profile
@reu
reu / deploy.rb
Created November 6, 2010 12:41
Capistrano deploy recipe for database configuration
# Bundler Integration
require "bundler/capistrano"
# Application Settings
set :application, "yourapplicationname"
set :user, "serveruser"
set :deploy_to, "/home/#{user}/rails-applications/#{application}"
set :rails_env, "production"
set :use_sudo, false
set :keep_releases, 3
require "benchmark"
n = 100000
klass = Benchmark
Benchmark.bm do |b|
b.report do
n.times { "visit_#{(klass.name || '').gsub('::', '_')}" }
end
class NewYachtModel < ActiveRecord::Base
belongs_to :new_yacht
has_many :contents, :class_name => 'NewYachtTopicContent'
has_many :topics, :class_name => 'NewYachtTopic', :through => :contents
accepts_nested_attributes_for :contents
end
class NewYachtTopic < ActiveRecord::Base
@reu
reu / ssh-copy-id
Created February 3, 2011 13:45
ssh-copy-id
#!/bin/sh
# Shell script to install your public key on a remote machine
# Takes the remote machine name as an argument.
# Obviously, the remote machine must accept password authentication,
# or one of the other keys in your ssh-agent, for this to work.
ID_FILE="${HOME}/.ssh/id_rsa.pub"
if [ "-i" = "$1" ]; then
module FormHelper
def nested_form_for(object, *args, &block)
concat form_for(object, *(args << { :builder => NestedFormBuilder }), &block)
execute_after_nested_form_callbacks
end
def execute_after_nested_form_callbacks
@after_nested_form_callbacks ||= []
@after_nested_form_callbacks.inject(String.new.html_safe) do |output, callback|
output << callback.call
scope :page, lambda { |number|
per_page = 2
limit(per_page).offset(per_page * ([number.to_i, 1].max - 1))
} do
def per(number)
limit(number).offset(offset_value / limit_value * number)
end
def num_pages
(except(:offset, :limit).count.to_f / limit_value).ceil
@reu
reu / video.rb
Created February 23, 2011 16:24
# encoding: UTF-8
require "open-uri"
class Video < ActiveRecord::Base
class YoutubeVideoValidator < ActiveModel::Validator
def validate(record)
unless record.youtube_code.include? 'youtube.com/watch?v='
record.errors[:base] << "URL não é um endereço válido de um vídeo do youtube"
end
end
abstract public class Cliente {
protected double saldo;
public Cliente(float saldo){
this.saldo = saldo;
}
public void depositar(float valor){
this.saldo = this.saldo + valor;
}
# encoding: UTF-8
module ApplicationHelper
def sharing_on_social_networks(subject)
social_networks = {}
social_networks[:twitter] = "http://twitter.com/share?text=Estou vendo #{truncate(subject, :length => 60)} na página da Soulfitness. Veja também em&url=http://www.soulfitness.com.br"
String.new.html_safe.tap do |output|
social_networks.each_pair do |key, value|
output << link_to(image_tag(key.to_s + ".png", :width => 21, :height => 21), value, :class => key, :target => "_blank")
end
end
@reu
reu / init.d
Created April 6, 2011 17:56
RAILS_ENV=test DISPLAY=:2.0 rake db:migrate spec:acceptance
ISPLAY=:2.0
NAME=Xvfb
INSTALL_DIR=/usr/bin
DAEMON=$INSTALL_DIR/$NAME
DAEMON_ARGS="-ac -screen scrn 1024x768x24 $DISPLAY"
PIDFILE=/var/run/$NAME.pid
DAEMON_USER=root
DAEMON_GROUP=$DAEMON_USER