Archive 2014


December 30, 2014

Form endpoints for designers and developers. Doesn’t require any WYSIWYG form building, or obtrusive embed codes. Build your form in your own html, and FormKeep takes care of the rest.

November 25, 2014

You can use reorder on an ActiveRecordRelation to replace all existing order methods. It’s super useful when merging scopes that may have imported their own ordering that is no longer applicable.

November 24, 2014

Study the docs. As an example, there are so many methods on Enumerable that you can leverage to eliminate temporary variables. Don’t spend all your time reading the docs, but every once and a while it’s good to review what’s already built in the standard library that you don’t need to re-invent.

If you’re looking up a record by anything other than the primary_key, don’t accept arbitrary arguments. Lazily looking up a record is great, but if you’re using something like

def destroy_do_dad(expecting_an_id_but_an_object_was_passed_in)
  DoDad.where(foreign_system_id: expecting_an_id_but_an_object_was_passed_in).destroy

you have a huge vulnerability. #to_param, will come into play, and you’re now potentially finding a completely arbitrary record and destroying it.

November 21, 2014

Let the database do all of the heavy lifting for counting your ActiveRecord objects.

Record.pluck(:owner_id).each_with_object( { |id, counts|
  counts[id] += 1

Sums up the number of records for a owner in Ruby. The same can be achieved via ActiveRecord.

November 10, 2014

When accessing the Rails flash messages, use strings instead of symbols. I thought it was a HashWithIndifferentAccess...but it doesn’t appear to be.

November 8, 2014

Responding to http in Go on Dreamhost is pretty easy. The trick is to use FactCGI to serve it up.

I did this via a trivial conditional for my setup of developing on OSX, and deploying to Linux.

if runtime.GOOS == "linux" {
  fcgi.Serve(nil, nil)
} else {
  http.ListenAndServe(":2112", nil)

To cross-compile to Linux, you use the Go runtime flags.

GOOS=linux GOARCH=386 go build

November 7, 2014

I was reminded how evil DST is. It’s just the worst. Writing tests around it is the best. Just a reminder that 2am-3am on March 8, 2015 DOES NOT EXIST.

November 6, 2014

Some random commands from tinkering with Vagrant today:

  • dscacheutil -flushcache # Flush the DNS cache

JavaScript Array’s map function takes a optional second argument, of what to bind to this.

November 5, 2014

Gulpfiles. I don’t know what I learned about them, but I helped a friend troubleshoot why and gulp-sass combined to produce a Segmentation Fault 11. Something with libsass, and rolling back the node-sass version fixed it.


November 4, 2014

Gourmet Service Objects is a post worth returning to from time to time. After slogging through some code to understand what it did, I wrote it into a service. The code wasn’t substantially different, but the fact that it had a name as first-class citizen instantly made it easier to understand.

Because my dudepal @jabronus gave me an SVG of my avatar, I started learning how to use SVG in the browser. I even made my avatar blink using CSS animations. Supes cool.

November 3, 2014

git log --pretty=oneline gives you the one-line per a commit output.

git log --pretty=format:"%an" gives you formatting on commits. All the interpolations available are listed in the man git-log under “PRETTY FORMATS”.

attr_readonly exists in Rails for making fields immutable. But we’re in Rails, so we don’t use big words like immutable, we use readonly. Caveat: you can still change the values in memory, which has other consequences around validations and any derived values.

October 29, 2014

Having opinions is more fun that trying to please everyone. Engage in helpful debate with reasonable people. Change your mind when presented with new data that should change your mind. Attack the problems together. Defend your teammates, not your opinions.

October 23, 2014

You can be incredibly productive, but feel unproductive. Fixing lots of small bugs leads to this. Keeping a list of everything completed such as Trello helps you remember that you’re making a lot of small progress when it feels like you’re not making any large progress.

There are many ways to write even the simplest statement.

# Whitelisted arguments.  # Which is most readable while being concise?

# 1
trusted = %w[approve reject].find { |i| i == untrusted }

# 2
trusted = (%w[approve reject] & [untrusted]).first

# 3
trusted = %w[approve reject].include?(untrusted) ? untrusted : nil

# 4
trusted = case untrusted; when *%w[approve reject]; untrusted; end

October 22, 2014

Aggressively eliminate state. After working with ReactJS for a bit, I am starting to see the merits of eliminating state. I’m even beginning to like methods prefixed with get and set. Eliminating state means eliminating side-effects.

Writing adolescent JS code seems to lead to better JS code. What I mean by adolescent code, is that it refuses to take responsibility. It lets somebody else own the state of the world. All it does is ask for values, and do something with them.

If you want to launch Vim from a Ruby script, use system 'vim'. Backticks won’t work, because they want to consume STDOUT. exec takes over the process, so you couldn’t do work afterwords. system works.

October 21, 2014

Take breaks.

Lately, I’ve been taking breaks. Today I didn’t. And at end-of-day, the difference in energy level left for friends & family is substantially depleted.

October 20, 2014

Small changes are easier than big changes. Renaming things seems like a huge win, but when you’re renaming big things, small subtleties will emerge. These pitfalls are easier to see in others’ proposed changes than your own.

If you’re going to rename something, have a good purpose for it.

Look for the dumb solution. Sometimes refactoring isn’t needed yet. You don’t know enough about the problem to find the better solution, yet.

October 16, 2014

It’s important to know how to revert a commit, because sometimes you accidentally commit strait to master.

If you caught yourself before pushing upstream, you’re good. Just

git checkout -b temporary
git branch -D master
git checkout master

This assuming it automatically pulls master back down from upstream, which is my default setup.

If you did push upstream, you’ll basically want to create a commit that reverts the latest commit. Hopefully it’s only one commit.

git format-patch -1 HEAD
git apply [newly_created_patch_name] -R

October 15, 2014

You have to mull through a lot of terrible names, and iterate on terrible code to arrive at a consistent, reasonable API. Today I was building an ActiveSupport::Concern whose purpose will be to cache attributes that are expensive to compute. The names of the methods changed a handful of times. And even now they may not be finalized.

I left the code for a few hours, and returned to it with a fresh set of eyes. The names were terrible. Their implied meaning was confusing. But because of this gap, I was able to see some of of the consistent shortcomings. I also saw patterns emerging, that lead to a more consistent API for the final iteration (of the day).

October 14, 2014

I started making a before_filter that would redirect a request from a bot. After returning to the code, I decided it would be better to rescue_from SomeCustomError, with: :what_was_previously_a_before_filter so that the code did not have to run on every single request. This probably amounts to minimal performance gains in terms of actual execution time, but I think it brought clarity to the code by having the happy path occur first.

Taking the time to organize your folders/files into something predictable needs to be done quickly when you’re working with branches of code. Every divergent branch means a delay in making it happen.

October 13, 2014

Reminded of a proposal I made long ago. For responsive design, start at one size, and head in one direction, using either min-width or max-width, but not both.

I spent time today moving some behavior of a model into a concern, specific to that model. Rather than putting it into app/models/concerns/person_pending_request_count I put it into app/models/person/pending_request_count. I liked this change, because it communicated that only Person should be using this. It’s not a general mixin, though it could easily become one. Then in the person class, I just included it with include Person::PendingRequestCount.

September 30, 2014

Rails UJS’ ajax:success and ajax:error only apply to XHRs originating from a data-remote="true" source. If you’d like them to apply to all XHRs in your page or application, you’re gonna have to wire that up.

In slim, you can use <, > or both after your tag to add leading or trailing whitespace, respectively.