Skip to content

Instantly share code, notes, and snippets.

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
@harlow
harlow / lookup_found_webhook.json
Last active September 6, 2015 04:20
Example webhook from Company API
{
"id": "custom_id", // the webhook_id passed in the lookup params
"status": 200,
"type": "company",
"body": {
// Company attributes
}
}
@harlow
harlow / capybara_example.rb
Created February 26, 2012 18:58
Capybara Cheat Sheet
# Navigating
visit('/projects')
visit(post_comments_path(post))
# Clicking links and buttons
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click('Link Text') # Click either a link or a button
click('Button Value')
@harlow
harlow / gist:2510198
Created April 27, 2012 15:32
Gitsucker
#!/usr/bin/env ruby
require 'open-uri'
require 'nokogiri'
require 'json'
class Repo
def initialize(name)
@name = name
end
@harlow
harlow / merge_pull_requests.md
Created October 26, 2012 19:10
Steps to accepting pull requests from open source contributors

Add contributors pull requests to your origin remote

# .git/config
[remote "origin"]
  fetch = +refs/heads/*:refs/remotes/origin/*
  fetch = +refs/pull/*/head:refs/remotes/origin/pr/*
  url = git@github.com:thoughtbot/[repo].git

Fetch and checkout the remote branch. Run bundle to make sure any new dependencies are installed.

@harlow
harlow / factory_girl_example.rb
Created December 4, 2012 20:39
Factory Girl Cheat Sheet
# This will guess the User class
Factory.define :user do
first_name 'John'
last_name 'Doe'
admin false
end
# This will use the User class (Admin would have been guessed)
Factory.define :admin, :class => User do |u|
@harlow
harlow / cache_control.rb
Created April 16, 2014 22:20
Rails 4 cache_control headers
def set_cache_control_headers(max_age = 1.second.to_s)
if request.method == "GET"
request.session_options[:skip] = true
response.cache_control[:public] = true
response.cache_control[:extras] = ['no-cache']
response.headers['Surrogate-Control'] = "max-age=#{max_age}"
end
end
@harlow
harlow / rules.md
Created January 28, 2013 19:43 — forked from henrik/rules.md
  1. Your class can be no longer than 100 lines of code.
  2. Your methods can be no longer than five lines of code.
  3. You can pass no more than four parameters and you can’t just make it one big hash.
  4. When a call comes into your Rails controller, you can only instantiate one object to do whatever it is that needs to be done.

You can break these rules if you can talk your pair into agreeing with you.

# app/models/customer.rb
class Customer
attr_reader :id, :country
def initialize(attrs = {})
@id = attrs[:id]
@country = attrs[:country]
end
def tax_code
@harlow
harlow / chat.rb
Created March 16, 2013 20:17 — forked from rkh/chat.rb
# coding: utf-8
require 'sinatra'
set server: 'thin', connections: []
get '/' do
halt erb(:login) unless params[:user]
erb :chat, locals: { user: params[:user].gsub(/\W/, '') }
end
get '/stream', provides: 'text/event-stream' do