duncanbeevers

Clip Fixnum to Range


over 2 years ago by duncanbeevers on dweebd.
class Fixnum def clip_to range r = range.first < range.last ? range : (range.last)..(range.first) return [ [ r.min, self ].max, r.max ].min end...

has_many with arguments


over 2 years ago by duncanbeevers on dweebd.
Rails Associations are powerful tools, but sometimes they can be a bit over-reaching. Let’s examine a hypothetical has_many association and how it...

Improve page-load speed with deferred content insertion


over 2 years ago by duncanbeevers on dweebd.
Displaying ads on a site can be a little tricky. Many ad networks simply provide you with a snippet of javascript that must be embedded directly into a...

Assert two arrays contain the same elements (updated)


over 2 years ago by duncanbeevers on dweebd.
I recently posted on determining whether two arrays contain the same elements irrespective of ordering. The approach supplied works for some basic Ruby...

Oops, dupes


over 2 years ago by duncanbeevers on dweebd.
So you forgot to add a uniqueness constraint to a table in your db and over time, race conditions have wreaked havoc on your validates_uniqueness_of validations...

Capture ActionMailer output for your QA team


over 2 years ago by duncanbeevers on dweebd.
Last month, Thoughtbot’s Matt Jankowski wrote up a post about How to not accidentally send thousands of ‘beta invites’. The method outlined...

Assert two arrays contain the same elements


over 2 years ago by duncanbeevers on dweebd.
UPDATE: A more comprehensive method of comparing arrays is covered in this updated article. When you don’t care what order items in an array are in...

Temporarily modifying a constant


over 3 years ago by duncanbeevers on dweebd.
Sometimes I want to verify the value within a constant is handled in a certain way, independently of what the actual value in the constant is defined as...

Yes, but what was the error message?


over 3 years ago by duncanbeevers on dweebd.
Verifying errors are raised is handy, but are you sure you’re capturing the right one? Check the message! # assert_raise allows you to check for the...

validates_not_format_of


over 3 years ago by duncanbeevers on dweebd.
# Validations based on negations of regular expressions. # validates_not_format_of takes an array of attributes, # a regular expression to apply to them...