Skip to content

Instantly share code, notes, and snippets.


Brian Landau brianjlandau

View GitHub Profile
brianjlandau / gist:4162654
Created Nov 28, 2012
Setting up a Rails engine for MySQL, RSpec & Capybara
View gist:4162654

Setup RSpec and Capybara on a new Rails engine

  1. rails plugin new ENGINE_NAME --full --database=mysql

  2. Add to gemspec:

s.add_development_dependency 'rspec-rails'
s.add_development_dependency 'shoulda-matchers'
s.add_development_dependency 'capybara'
s.add_development_dependency 'database_cleaner'
brianjlandau / soft_delete.rb
Created Dec 14, 2011
This is a soft delete mixin for ActiveRecord 3.x. All you need to do is add a `deleted_at` column to the ActiveRecord models you mix this into.
View soft_delete.rb
module SoftDelete
extend ActiveSupport::Concern
included do
define_model_callbacks :soft_delete
define_model_callbacks :recover
default_scope where(:deleted_at => nil)
class_eval do
class << self
alias_method :with_deleted, :unscoped
brianjlandau / optimistic_locking_form_for.rb
Created Apr 23, 2010
This puts the lock version of the model in the form so that Optimistic locking behaves a little more as expected.
View optimistic_locking_form_for.rb
module ActionView
module Helpers
module OptimisticLockingFormFor
def self.included(base)
base.alias_method_chain :form_for, :optimistic_locking
def form_for_with_optimistic_locking(record_or_name_or_array, *args, &block)
form_for_without_optimistic_locking(record_or_name_or_array, *args) do |form_with_locking|
brianjlandau / gist:da0bab27dcf1d8691f6e
Last active Nov 16, 2019
Email validator based on DNS
View gist:da0bab27dcf1d8691f6e
require 'resolv'
class EmailValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if"@").last, Resolv::DNS::Resource::IN::MX).empty?
record.errors[attribute] << (options[:message] || "does not have a valid domain")
rescue Resolv::ResolvError, Resolv::ResolvTimeout
record.errors[attribute] << (options[:message] || "does not have a valid domain")
brianjlandau /
Last active Nov 10, 2019
Get the top 500 homebrew formula and browse
curl "" | \
jq ".formulae |
to_entries |
sort_by([(.value[].count | gsub(\",\"; \"\") | tonumber)] | add) |
reverse |
map({key: .key, value: ([(.value[].count | gsub(\",\"; \"\") | tonumber)] | add)})[0:500] |
from_entries" | \
brianjlandau / gist:176754
Created Aug 28, 2009 — forked from defunkt/gist:162444
Rails Capistrano deploy using git as our deployment strategy. You'll need git version >= on your server for this to work.
View gist:176754
# you'd obviously have more settings somewhere
set :scm, :git
set :repository, ""
set :branch, "origin/master"
set :migrate_target, :current # this tells capistrano where to run the migration. otherwise it would try to use the latest release directory (/path/to/app/releases/2012XXXXXXXXX)
set :use_sudo, false
set :ssh_options, {:forward_agent => true} # so you can checkout the git repo without giving the server access to the repo
set :rails_env, 'production'
# These are here to override the defaults by cap
brianjlandau / paperclip_s3_migrator.rb
Last active Jul 5, 2018
This allows you to migrate files on S3 stored by paperclip to a new location.
View paperclip_s3_migrator.rb
# Example usage:
# [User, BlogPost, Page],
# ':attachment/:id/:style/:filename',
# ':class/:id/:attachment/:style/:updated_at_:filename'
# ).migrate
class PaperclipS3Migrator <, :old_path, :new_path)
brianjlandau / jquery.benchmark.js
Created Nov 30, 2009
A useful function for benchmarking a block of code and displaying the results on the page. *Depends on jQuery
View jquery.benchmark.js
// based on methodology developed by PPK:
$.benchmark = function(times, result_selector, func){
var startTime = new Date().getTime();
while (times != 0){
brianjlandau / .babelrc
Last active Mar 29, 2018
Rails 5.1 webpacker config for react + jest + enzyme
View .babelrc
"presets": [
"modules": false,
"targets": {
"browsers": "> 1%",
"uglify": true

Keybase proof

I hereby claim:

  • I am brianjlandau on github.
  • I am brianjlandau ( on keybase.
  • I have a public key ASADZdepX0J5-Vj2nmxaby26kkElaVpSHeHAW6n6Hg3j_Qo

To claim this, I am signing this object:

You can’t perform that action at this time.