Skip to content

Instantly share code, notes, and snippets.

Avatar

Brian Landau brianjlandau

View GitHub Profile
@brianjlandau
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
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
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
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 Nov 16, 2019
Email validator based on DNS
View gist:da0bab27dcf1d8691f6e
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 Nov 10, 2019
Get the top 500 homebrew formula and browse
View top_brews.sh
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 Aug 28, 2009 — 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.
View gist:176754
# 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 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:
#
# 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 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:
// 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--;
}
@brianjlandau
brianjlandau / .babelrc
Last active Mar 29, 2018
Rails 5.1 webpacker config for react + jest + enzyme
View .babelrc
{
"presets": [
[
"env",
{
"modules": false,
"targets": {
"browsers": "> 1%",
"uglify": true
},
View keybase.md

Keybase proof

I hereby claim:

  • I am brianjlandau on github.
  • I am brianjlandau (https://keybase.io/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.