Skip to content

Instantly share code, notes, and snippets.

Avatar
🪕
Scruggs not drugs

Norman Clarke norman

🪕
Scruggs not drugs
View GitHub Profile
@norman
norman / fixtures_spec.rb
Created Oct 13, 2021
Test the validity of your Rails fixtures
View fixtures_spec.rb
# frozen_string_literal: true
require "rails_helper"
describe "Fixtures" do
fixture_table_names.each do |table_name|
model_class = table_name.singularize.classify.constantize
it "has valid #{table_name}" do
expect(model_class.all).to all be_valid
end
View keybase.md

Keybase proof

I hereby claim:

  • I am norman on github.
  • I am compay (https://keybase.io/compay) on keybase.
  • I have a public key whose fingerprint is 1A08 8ED4 32C3 612F 5FF5 677E 74BA BFD3 3159 0D75

To claim this, I am signing this object:

@norman
norman / etag.rb
Last active May 31, 2016
One way to make Rails consider the layout in an etag.
View etag.rb
# By default, changes to the layout temnplate won't cause a Rails etag
# to change. I suppose this is to facilitate Turbolinks. Sometimes you
# might want to change an etag in response to changes in the layout -
# this is particularly true when working in development mode. This is
# one way to do it, there are probably other (read: likely better) ways
# to do this too.
class FooController < ApplicationController
# There's probably some API method to get the name of the layout
# rather than hard-coding it like I did here, but I'm too lazy to
# look it up right now.
View gist:21b79a7024febb755b91
### Keybase proof
I hereby claim:
* I am norman on github.
* I am compay (https://keybase.io/compay) on keybase.
* I have a public key whose fingerprint is 3059 0FBA AFDE E001 D557 C63A 298B 0120 ED7D 0E39
To claim this, I am signing this object:
@norman
norman / pg.txt
Last active Aug 29, 2015
Optimal development-only Postgres settings for my Macbook Pro with 8GB RAM.
View pg.txt
#------------------------------------------------------------------------------
# CUSTOMIZED OPTIONS
#------------------------------------------------------------------------------
# Add settings for extensions here
#
# Some stupid, dangerous settings only used to speed things up on a local
# development machine, where if data is lost I'll just wipe and recreate.
# Never ever ever use these in production or anything approximating
# production.
View gist:851e1de4a1a575b7e2ef
According to my records your subscription will expire on 14 June 2014
LostCousins subscriptions are NOT renewed automatically unlike subscriptions for most other
genealogy sites - I prefer to depend on your generosity, not your forgetfulness.
The good news is that if you renew your subscription NOW it won't start until your existing
subscription expires - you'll get a full 12 months from the date shown above (so you don't have
to leave it until the last minute). Even better news is that the price hasn't changed since 2005 -
it's still just 10 pounds for a single subscription!
@norman
norman / gist:8300884
Created Jan 7, 2014
Benchmark comparing Haml 4.0.5 to Haml 4.1.0.beta.1
View gist:8300884

Haml 4.0.5

ruby 2.0.0p195 (2013-05-14 revision 40734) [x86_64-darwin12.4.0]
------------------------------------------------------------------------

1000 Iterations
Rehearsal --------------------------------------------------------
compiled haml pretty   0.550000   0.000000   0.550000 (  0.551526)
compiled haml ugly     0.270000   0.000000   0.270000 (  0.270457)
View gist:8136799

FriendlyId uses Rails's extending method to avoid overwriting your model's find method. Internally this is implemented similar to this:

def self.friendly
  all.extending(friendly_id_config.finder_methods)
end

This however comes with a performance impact, because extending invokes Ruby's extend, which blows away MRI's method cache. To work around this, FriendlyId lets you include a :finders module which overrides your model's find to increase performance.

@norman
norman / factories.rb
Last active Dec 22, 2015
A very minimal alternative to FactoryGirl?
View factories.rb
module FlexMinder
module Factories
def factory(klass, attributes)
name = klass.to_s
underscored = name.underscore
class_eval(<<-END, __FILE__, __LINE__ + 1)
def build_#{underscored}(attributes = {})
attributes = valid_#{underscored}_attributes.deep_merge(attributes)
#{name}.new(attributes)
@norman
norman / benchmarks.txt
Last active Dec 20, 2015
FriendlyId 5.0 versus 4.0
View benchmarks.txt
## FriendlyId 5.0
------------------------------------------------------------------------
Using ruby 2.0.0 AR 4.0.0 with sqlite3 (in-memory)
Rehearsal -------------------------------------------------------------------------
find (without FriendlyId) 0.410000 0.000000 0.410000 ( 0.412285)
find (in-table slug) 0.890000 0.000000 0.890000 ( 0.911959)
find (in-table slug; finders addon) 0.670000 0.010000 0.680000 ( 0.686639)
find (external slug) 1.660000 0.040000 1.700000 ( 1.721548)
insert (without FriendlyId) 0.960000 0.010000 0.970000 ( 0.977678)