Skip to content

Instantly share code, notes, and snippets.

View rafaelp's full-sized avatar

Rafael Lima rafaelp

View GitHub Profile
@rafaelp
rafaelp / gist:2788297
Created May 25, 2012 13:58
Bandolera
23, você tá sem bandolera?
Numa altua dessa do campeonato, o Sr. té sem bandolera, seu 23.
Quer dizer o companheiro seu cai no chão, baleado, Sr. vai pegar esse fuzil e vai fazer o que?
Vai jogar no chão?
- Não Sr.
# Ao invés de fazer isso:
def do_action(hashed_code = vendor.hashed_code)
get :edit, invite_id: hashed_code
end
# Vamos fazer isso:
def do_action(params = {})
get :edit, {invite_id: vendor.hashed_code}.merge(params)
@rafaelp
rafaelp / integrate.rake
Created March 21, 2012 20:27
Rake task to synchronous continuous deployment of Rails application deployed on Heroku
# -*- encoding : utf-8 -*-
namespace :integration do
namespace :heroku do
task :add_remote do
sh "git remote add staging git@heroku.com:nomedoprojeto-staging.git" if `git remote |grep staging`.strip.blank?
sh "git remote add production git@heroku.com:com:nomedoprojeto-production.git" if `git remote |grep production`.strip.blank?
end
task :check do
var = `heroku config -s --app nomedoprojeto-staging|grep INTEGRATING_BY`
@rafaelp
rafaelp / deploy.rake
Created March 21, 2012 20:25
Rake task to deploy Rails project to Heroku
# -*- encoding : utf-8 -*-
namespace :heroku do
namespace :deploy do
PRODUCTION_APP = 'nomedoprojeto-production'
STAGING_APP = 'nomedoprojeto-staging'
def run(*cmd)
system(*cmd)
raise "Command #{cmd.inspect} failed!" unless $?.success?
end
@rafaelp
rafaelp / gist:1976687
Created March 5, 2012 04:55
A solution to a more obscure problem related to the "vulnerability" of mass assignment:
# account.rb
class Account < ActiveRecord::Base
has_many :users
has_many :services
end
# user.rb
class User < ActiveRecord::Base
belongs_to :account
end
@rafaelp
rafaelp / attr_acessible_security.rb
Created March 5, 2012 03:59
How to protect against mass assignment attack
# Put this file on config/initializer
# This will create an empty whitelist of attributes available for mass assignment for
# all models in your app. As such, your models will need to explicitly whitelist
# accessible parameters by using an attr_accessible declaration. This technique is best
# applied at the start of a new project. However, for an existing project with a thorough
# set of functional tests, it should be straightforward and relatively quick to insert this
# initializer, run your tests, and expose each attribute (via attr_accessible) as dictated
# by your failing tests.
@rafaelp
rafaelp / gist:1925879
Created February 27, 2012 18:01
Is Ruby Javascript like?
ruby-1.9.2-p290 :001 > 0/0
ZeroDivisionError: divided by 0
from (irb):1:in `/'
from (irb):1
from /Users/rafael/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'
ruby-1.9.2-p290 :002 > 1.0/0
=> Infinity
ruby-1.9.2-p290 :003 > 0.0/0
=> NaN
ruby-1.9.2-p290 :004 >
@rafaelp
rafaelp / heroku_migrate_from_bamboo_to_cedar.rake
Created December 24, 2011 01:39
Rake task to migrate an application from bamboo stack do cedar stack on Heroku
# -*- encoding : utf-8 -*-
# Rafael Lima (http://rafael.adm.br)
# License: http://creativecommons.org/licenses/by/2.5/
#
# ATTENTION: This is a initial work, use it at your own risk!
#
# Usage:
# $ APP_NAME=myappname rake heroku:migrate_from_bamboo_to_cedar
@rafaelp
rafaelp / concourses_from_file
Created November 29, 2011 20:23
Como refatorar esse método?
# conteúdo do CSV
SF,Técnico Legislativo,Direito Constitucional,Constitucionalismo
SF,Técnico Legislativo,Direito Constitucional,Teoria do Estado
# método para refatorar
def self.concourses_from_file(file_path)
return @concourses unless @concourses.nil?
@concourses = {}
CSV.foreach(file_path) do |row|
@concourses[row[0].upcase] ||= {}
@rafaelp
rafaelp / startupdev-pol-stats-v1
Created November 22, 2011 19:20
Stats of POL
+----------------------+-------+-------+---------+---------+-----+-------+
| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
| Controllers | 24 | 24 | 3 | 4 | 1 | 4 |
| Helpers | 2 | 2 | 0 | 0 | 0 | 0 |
| Models | 114 | 100 | 6 | 12 | 2 | 6 |
| Libraries | 41 | 37 | 0 | 1 | 0 | 35 |
| Model specs | 258 | 242 | 0 | 0 | 0 | 0 |
| Controller specs | 64 | 57 | 0 | 2 | 0 | 26 |
| Routing specs | 28 | 26 | 0 | 0 | 0 | 0 |