Skip to content

Instantly share code, notes, and snippets.

@sadjow
sadjow / gist:4162756
Created November 28, 2012 17:36
Create a server with http-proxy node.js - Multiple domains.
var http = require('http'),
httpProxy = require('http-proxy');
httpProxy.createServer(function (req, res, proxy) {
if(req.headers.host == 'sadjow.com') {
proxy.proxyRequest(req, res, {
host: 'sadjow.com',
port: 3000
});
@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;
# https://37signals.com/svn/posts/2742-the-road-to-faster-tests
# spec/support/performance/deferred_garbage_collection.rb
class DeferredGarbageCollection
DEFERRED_GC_THRESHOLD = (ENV['DEFER_GC'] || 15.0).to_f
@@last_gc_run = Time.now
def self.start

Capybara Actions

# Anchor
click_link 'Save'

# Button
click_button 'awesome'

# Both above
@sadjow
sadjow / slugable.rb
Created September 18, 2013 17:14 — forked from brennovich/slugable.rb
# app/models/concerns/slugable.rb
module Slugable
extend ActiveSupport::Concern
included do
validates_format_of :slug, :without => /^\d/
before_save :generate_slug
end
# Use this setup block to configure all options available in SimpleForm.
SimpleForm.setup do |config|
# you need an updated simple_form gem for this to work, I'm referring to the git repo in my Gemfile
config.input_class = "form-control"
config.wrappers :bootstrap, tag: 'div', class: 'form-group', error_class: 'error' do |b|
b.use :html5
b.use :placeholder
b.use :label
b.use :input
@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.