Skip to content

Instantly share code, notes, and snippets.

@sadjow
sadjow / rmagick.sh
Created January 25, 2013 17:50 — forked from zoras/rmagick.sh
sudo apt-get install libdjvulibre-dev libjpeg-dev libtiff-dev libwmf-dev libmagickcore-dev libmagickwand-dev libmagick++-dev rvm
sudo gem install rmagick
ok!
Fetching: rmagick-2.13.1.gem (100%) Building native extensions. This could take a while ... Successfully installed rmagick-2.13.1 1 gem installed
require 'RMagick' #=> true

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 / 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"

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.

# 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.'
# 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 / 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

Capybara Actions

# Anchor
click_link 'Save'

# Button
click_button 'awesome'

# Both above
# 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
# Generates SQL from migrations
#
# Usage:
# rake db:[migrate,rollback]:with_sql
#
# The previous rake task will run migrations and create a file in `db/migrate_sql`
# Depending on migrate or rollback a file will be created with the respective
# sufix.
#
# Drawbacks: