Skip to content

Instantly share code, notes, and snippets.

View Odaeus's full-sized avatar

Andrew France Odaeus

View GitHub Profile
@ngauthier
ngauthier / README.md
Created November 19, 2012 15:43
Model Accessors

Model Accessors

I really like this pattern for providing accessors to the models used in a controller. Alternatively, you usually see a before_filter to set the model from the params hash only on the actions that need it. IMO this is abusing a before_filter because the filter chain is for pre-processing the request. This does sort of make sense if you want to redirect away from the page gracefully if the record can't be found, but you can do that by just calling the accessor in a filter to make sure it's there.

This means that views are more like partials with locals, which is nice. It also means the accessor is as easy to test as a named filter. And as usual it pulls the method out of the action to share, which is also nice.

Also, it achieves the same ends as gems that try to OO the view, but without a gem and with terse code. The model can be swapped for a presenter if necessary.

@coreyhaines
coreyhaines / use_validation_helpers.rb
Created August 9, 2012 15:52
Thoughts on ActiveModel::Validator as a validation service
# I love the helpers, they help to make the intention clear.
# I want to be able to do this. That is, using the validation helpers inside a validator object,
# rather than having to do manual validation.
class EmployeeValidator < ActiveModel::Validator
def validate(record)
record.validates_presence_of :email
end
end
@panthomakos
panthomakos / benchmark.rb
Created May 3, 2012 20:06
Benchmark Your Bundle
#!/usr/bin/env ruby
require 'benchmark'
REGEXPS = [
/^no such file to load -- (.+)$/i,
/^Missing \w+ (?:file\s*)?([^\s]+.rb)$/i,
/^Missing API definition file in (.+)$/i,
/^cannot load such file -- (.+)$/i,
]
@freshtonic
freshtonic / gist:1390291
Created November 24, 2011 00:00
Run Postgres Specs in Ramdisk
  • This creates a 560mb ramdisk. Adjust the size accordingly. I think the number at the end of the command is the number of disk blocks. They are 2kb in size for me.
  • Restarting postgres is not necessary; you can create the ramdisk and tablespace while postgres is running.
  • You will lose all data in the ramdisk tablespace when you shut your machine down

  $ diskutil erasevolume HFS+ "postgres_ramdisk" `hdiutil attach -nomount ram://1165430`
  Started erase on disk1
  Unmounting disk
  Erasing
 Initialized /dev/rdisk1 as a 569 MB HFS Plus volume