Skip to content

Instantly share code, notes, and snippets.

View shyam-habarakada's full-sized avatar
🌱
In the weeds

Shyam Habarakada shyam-habarakada

🌱
In the weeds
  • Smartsheet, 10Kft
  • Seattle, WA
  • X @shyamh
View GitHub Profile
@shyam-habarakada
shyam-habarakada / rails-model-before-save-fixtures.rb
Created November 13, 2011 17:23
rails models with before_save callbacks and fixtures
# == Schema Information
#
# Table name: users
#
# id :integer(4) not null, primary key
# first_name :string(255)
# last_name :string(255)
# email :string(255)
# thumbnail :string(255)
# user_type_id :integer(4)
@shyam-habarakada
shyam-habarakada / rails-3.2-errors.md
Created January 23, 2012 20:51
rails 3.2 errors

Getting the error undefined method `accept' for nil:NilClass in my controller. This is on the development environment running on OSX with mysql. Below is the full stack trace and also my bundle/gem setup information.

$ ruby --version
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.0.0]

RAILS FRAMEWORK TRACE

@shyam-habarakada
shyam-habarakada / rails-3.2-issue--Gemfile.rb
Created January 23, 2012 22:13
rails-3.2-issue--Gemfile
source 'http://rubygems.org'
gem 'rails', '3.2.0'
gem 'rake', '0.9.2.2'
gem 'mysql2', '0.2.6'
# Gems used only for assets and not required in production by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
@shyam-habarakada
shyam-habarakada / bash-rc-update.sh
Created March 20, 2012 15:08
bash-rc-update.sh
function color_my_prompt {
local __user_and_host="\[\033[00;30m\]\u@digitalone"
local __cur_location="\[\033[00;31m\]\w"
local __git_branch_color="\[\033[32m\]"
local __git_branch='`git branch 2> /dev/null | grep -e ^* | sed -E s/^\\\\\*\ \(.+\)$/\(\\\\\1\)\ /`'
local __prompt_tail="\[\033[35m\]$"
local __last_color="\[\033[00m\]"
export PS1="$__user_and_host $__cur_location $__git_branch_color$__git_branch$__prompt_tail$__last_color "
}
if [ "$color_prompt" = yes ]; then
# Requires the presence of a failover node that will get activated when the site is in maintenance mode, and will be disabled when the site is live.
namespace :deploy do
namespace :web do
desc <<-DESC
Disable via Cloud Load Balancers
DESC
task :lbdisable do
@shyam-habarakada
shyam-habarakada / rails-assert.rb
Created July 3, 2014 23:04
A simple rails assert method
def assert(message = 'assertion failed')
unless block_given? and yield
if Rails.env.development? or Rails.env.test?
raise message
else
Rails.logger.warn "(assert) #{message}"
end
end
end
# SSL self signed localhost for rails start to finish, no red warnings.
# 1) Create your private key (any password will do, we remove it below)
$ openssl genrsa -des3 -out server.orig.key 2048
# 2) Remove the password
$ openssl rsa -in server.orig.key -out server.key

Rails naming conventions

General Ruby conventions

Class names are CamelCase.

Methods and variables are snake_case.

Methods with a ? suffix will return a boolean.

record = Post.new(:title => 'Yay', :body => 'This is some insert SQL')
# easiest way to achieve this is by calling protected #arel_attributes_values (tested in
# rails 3.2.13). the alternative is to build the entire insert statement using arel >_>
record.class.arel_table.create_insert \
.tap { |im| im.insert(record.send(:arel_attributes_values, false)) } \
.to_sql