Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View brianjlandau's full-sized avatar

Brian Landau brianjlandau

  • Walnut Creek, CA
View GitHub Profile
@brianjlandau
brianjlandau / deep_freeze.rb
Created September 2, 2009 16:05
Deep freeze for enumerable objects in Ruby
module Enumerable
def deep_freeze
unless self.is_a? String
frozen = self.dup.each do |key, value|
if (value.is_a?(Enumerable) && !value.is_a?(String))
value.deep_freeze
else
value.freeze
end
end
@brianjlandau
brianjlandau / A Costly Parade.markdown
Created September 14, 2009 14:56
An archive of _why's blog post on the "Zed Shaw Fiasco" http://hackety.org/2008/11/21/aCostlyParade.html

A Costly Parade

by why the lucky stiff

Adam Wiggins: First, Rubyists love elegance.

Daniel Lyons:Every programmer worth a damn thinks they love elegance.

Rubyists love life. Boy, I tell you. They love humans. They love cars!! They looooooove dishes of real, actual food. You don’t even know. Airplanes in mid-air, refueling? They love that!

@brianjlandau
brianjlandau / gist:4162654
Created November 28, 2012 17:21
Setting up a Rails engine for MySQL, RSpec & Capybara

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
brianjlandau / soft_delete.rb
Created December 14, 2011 00:32
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.
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
brianjlandau / optimistic_locking_form_for.rb
Created April 23, 2010 18:27
This puts the lock version of the model in the form so that Optimistic locking behaves a little more as expected.
module ActionView
module Helpers
module OptimisticLockingFormFor
def self.included(base)
base.alias_method_chain :form_for, :optimistic_locking
end
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
brianjlandau / gist:da0bab27dcf1d8691f6e
Last active November 16, 2019 01:23
Email validator based on DNS
require 'resolv'
class EmailValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if Resolv::DNS.new.getresources(value.split("@").last, Resolv::DNS::Resource::IN::MX).empty?
record.errors[attribute] << (options[:message] || "does not have a valid domain")
end
rescue Resolv::ResolvError, Resolv::ResolvTimeout
record.errors[attribute] << (options[:message] || "does not have a valid domain")
end
@brianjlandau
brianjlandau / top_brews.sh
Last active November 10, 2019 02:58
Get the top 500 homebrew formula and browse
curl "https://formulae.brew.sh/api/analytics/install-on-request/homebrew-core/90d.json" | \
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" | \
less
@brianjlandau
brianjlandau / gist:176754
Created August 28, 2009 02:59 — forked from defunkt/gist:162444
Rails Capistrano deploy using git as our deployment strategy. You'll need git version >=1.5.6.6 on your server for this to work.
# you'd obviously have more settings somewhere
set :scm, :git
set :repository, "git@github.com:defunkt/github.git"
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
brianjlandau / paperclip_s3_migrator.rb
Last active July 5, 2018 08:32
This allows you to migrate files on S3 stored by paperclip to a new location.
# Example usage:
#
# PaperclipS3Migrator.new(
# [User, BlogPost, Page],
# ':attachment/:id/:style/:filename',
# ':class/:id/:attachment/:style/:updated_at_:filename'
# ).migrate
#
class PaperclipS3Migrator < Struct.new(:klasses, :old_path, :new_path)
@brianjlandau
brianjlandau / jquery.benchmark.js
Created November 30, 2009 19:41
A useful function for benchmarking a block of code and displaying the results on the page. *Depends on jQuery
// based on methodology developed by PPK:
// http://www.quirksmode.org/blog/archives/2009/08/when_to_read_ou.html
(function($){
$.benchmark = function(times, result_selector, func){
var startTime = new Date().getTime();
while (times != 0){
func();
times--;
}