Skip to content

Instantly share code, notes, and snippets.

@sadjow
sadjow / simple_form_bootrap.rb
Created September 17, 2013 20:15
SimpleForm bootstrap
# Use this setup block to configure all options available in SimpleForm.
SimpleForm.setup do |config|
config.wrappers :bootstrap, tag: 'div', class: 'control-group', error_class: 'error' do |b|
b.use :html5
b.use :placeholder
b.use :label
b.wrapper tag: 'div', class: 'controls' do |ba|
ba.use :input
ba.use :error, wrap_with: { tag: 'span', class: 'help-inline' }
ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
# encoding: UTF-8
# pt-BR translations for Devise
pt-BR:
devise:
confirmations:
confirmed: 'Sua conta foi confirmada com sucesso. Você está logado.'
send_instructions: 'Dentro de minutos, você receberá um e-mail com instruções para a confirmação da sua conta.'
send_paranoid_instructions: 'Se o seu endereço de e-mail estiver cadastrado, você receberá uma mensagem com instruções para confirmação da sua conta.'
failure:
already_authenticated: 'Você já está logado.'
@sadjow
sadjow / routes.rb
Created September 12, 2013 19:30
Rails routes documentation gist
ApplicationName::Application.routes.draw do
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
# Example of regular route:
# get 'products/:id' => 'catalog#view'

Friendly URLs

By default, Rails applications build URLs based on the primary key -- the id column from the database. Imagine we have a Person model and associated controller. We have a person record for Bob Martin that has id number 6. The URL for his show page would be:

/people/6

But, for aesthetic or SEO purposes, we want Bob's name in the URL. The last segment, the 6 here, is called the "slug". Let's look at a few ways to implement better slugs.

@sadjow
sadjow / en.yml
Created August 16, 2013 19:15 — forked from denyago/en.yml
Very quickly implemented MIME Type validator for CarrierWave. See also: https://gist.github.com/1009861 and https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Validate-attachment-file-size
en:
errors:
messages:
wrong_content_type: "is the wrong content type"
@sadjow
sadjow / remove.sh
Created July 11, 2013 20:01
How to remover a DEFINER from MySQL
cat new_schema.sql | sed -e 's@/\*!500[0-9][0-9] DEFINER=[^\*]*\*/@@' > new_schema2.sql

My issues with Modules

In researching topics for RailsCasts I often read code in Rails and other gems. This is a great exercise to do. Not only will you pick up some coding tips, but it can help you better understand what makes code readable.

A common practice to organize code in gems is to divide it into modules. When this is done extensively I find it becomes very difficult to read. Before I explain further, a quick detour on instance_eval.

You can find instance_eval used in many DSLs: from routes to state machines. Here's an example from Thinking Sphinx.

class Article < ActiveRecord::Base
@sadjow
sadjow / functions.js
Created May 10, 2013 00:03
Update a JavaScript object with a dotted path string. You pass the string path and the object you want.,
// company = {}
// Use putObject("owner.name", company,"Sadjow");
// Returns {owener: {name: "Sadjow"}}
function putObject(path, object, value) {
var modelPath = path.split(".");
function fill(object, elements, depth, value) {
var hasNext = ((depth + 1) < elements.length);
if(depth < elements.length && hasNext) {
@sadjow
sadjow / deploy.rb
Created April 11, 2013 21:13
Capistrano configuration file.
require "bundler/capistrano"
#set :rvm_ruby_string, "1.9.3"
set :rvm_type, :system
set :use_sudo, false
default_run_options[:pty] = true
set :shared_children, shared_children + %w{public/uploads}
@sadjow
sadjow / load_addthis_after_ajax_calls.js
Created April 1, 2013 15:23
Dynamic load addthis tag after ajax calls
$(document).ajaxStop(function() {
if (window.addthis) {
window.addthis = null;
window._adr = null;
window._atc = null;
window._atd = null;
window._ate = null;
window._atr = null;
window._atw = null;