Skip to content

Instantly share code, notes, and snippets.

@seanlinsley
seanlinsley / gist:9196255
Created February 24, 2014 20:23
NGINX: cache those compiled assets!
location ~* "-[a-z0-9]{32}\.(png|gif|jpg|jpeg|css|js)$" {
expires max;
}
@seanlinsley
seanlinsley / foo.rb
Created May 18, 2014 03:56
Strip empty strings!
class Foo < ActiveRecord::Base
before_save :strip_empty_strings
def strip_empty_strings
self.class.columns.select{ |c| c.type == :string }.each do |c|
send "#{c.name}=", send(c.name).try(:strip).presence # sets to nil if completely empty
end
end
end
@seanlinsley
seanlinsley / application.rb
Created May 19, 2014 01:40
Custom lib loading
module Example
class Application < Rails::Application
# Loads ruby files in a given directory.
# NOTE: `require_dependency` auto-reloads on file change when in development.
# POSTERITY: this was needed to prevent auto-namespacing of lib/a/b.rb files.
def loader(*dirs)
dirs.each do |dir|
Dir["#{Rails.root}/#{dir}/*.rb"].each do |path|
d = dir.gsub /lib\/?/, '' # removes lib and lib/
@seanlinsley
seanlinsley / database.yml
Last active August 29, 2015 14:19
Rails: retry connecting to Postgres
development: &default
adapter: postgresql
database: app_development
connect_retry_timeout: 1
@seanlinsley
seanlinsley / gist:193b93d5854c433a2b18
Last active August 29, 2015 14:20
why no implicit arguments for super inside define_method?
# setup
class User
def name; 'Bob'; end
end
class Object
def wrap(method, &block)
prepend Module.new { define_method method, &block }
end
end
@seanlinsley
seanlinsley / child.rb
Last active August 29, 2015 14:28
Inheritable settings between two models, in Rails
class Child < ActiveRecord::Base
belongs_to :parent
INHERITABLE_SETTINGS = %i[foo bar baz]
INHERITABLE_SETTINGS.each do |attr|
define_method attr do
value = read_attribute(attr)
default = default_setting(attr)
inherited = inherited_setting(attr)
@seanlinsley
seanlinsley / at.rb
Last active September 2, 2015 19:20
def at(time)
original = Time.now
allow(Time).to receive(:now) { time }
yield
ensure
allow(Time).to receive(:now) { original }
end
at 12.hours.from_now do
# your test
source 'https://rubygems.org'
gem 'rails', '3.2.0'
gem 'mysql2'
# Using this branch of ActiveAdmin to allow batch editing of records.
#gem 'activeadmin', :git => 'https://github.com/mattvague/active_admin.git', :branch => '270_batch_edit_api'
gem 'activeadmin', '0.4.0'
gem 'sass-rails', '3.2.4'
@seanlinsley
seanlinsley / hack.sh
Created June 2, 2012 22:29 — forked from erikh/hack.sh
OSX For Hackers
#!/usr/bin/env sh
##
# This is script with usefull tips taken from:
# https://github.com/mathiasbynens/dotfiles/blob/master/.osx
#
# install it:
# curl -sL https://raw.github.com/gist/2108403/hack.sh | sh
#
@seanlinsley
seanlinsley / Rakefile
Created November 30, 2012 10:48
Faster rake is better rake
# Rakefile inspired by http://rhnh.net/2010/09/07/speeding-up-rails-rake
def load_rails_environment
require File.expand_path('../config/application', __FILE__)
require 'rake'
TimeAudit::Application.load_tasks
end
# By default, do not load the Rails environment. This allows for faster
# loading of all the rake files, so that getting the task list, or kicking
# off a spec run (which loads the environment by itself anyways) is much quicker.