jBoxer

I change the directions of small pieces of metal for a living.

Changing a Rails 3 project name

| Comments

I’ve had to change Rails 3 project names a few times now. It’s too bad there’s no rake command for it. Here’s the next best thing: a step-by-step guide.

For the sake of this example, let’s say I’m changing my name name from “Teach” to “Learn”.

Update your app’s module name

In /config/application.rb, change the name of the module:

/config/application.rb
1
2
3
4
5
module Learn # Used to be `module Teach`
  class Application < Rails::Application
    # ...
  end
end

Update references to your app’s module name

Your app’s module name should appear in /config.ru:

/config.ru
1
2
3
4
# This file is used by Rack-based servers to start the application.

require ::File.expand_path('../config/environment',  __FILE__)
run Learn::Application # Used to be `run Teach::Application`

And in /Rakefile:

/Rakefile
1
2
3
4
5
6
7
#!/usr/bin/env rake
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require File.expand_path('../config/application', __FILE__)

Learn::Application.load_tasks # Used to be `Teach::Application.load_tasks`

Your app’s module name should also appear in a bunch of files under the /config directory. It should appear in /config/environments.rb.

/config/environments.rb
1
2
# Initialize the rails application
Learn::Application.initialize! # Used to be `Teach::Application.initialize!`

It should also appear in all the environment-specific configurations in /config/environments/*.rb (which should be development.rb, production.rb, test.rb, and any other environments you have configured). Change them all:

/config/environments/*.rb
1
2
3
4
Learn::Application.configure do # Used to be `Teach::Application.configure do`
  # Settings specified here will take precedence over those in config/application.rb
  # ...
end

And in your initializers (under /config/initializers/). At the very least, /config/initializers/secret_token.rb and /config/initializers/session_store.rb should have it:

/config/initializers/secret_token.rb
1
2
3
4
5
6
# Your secret key for verifying the integrity of signed cookies.
# If you change this key, all old signed cookies will become invalid!
# Make sure the secret is at least 30 characters and all random,
# no regular words or you'll be exposed to dictionary attacks.
Learn::Application.config.secret_token = 'garbagestring'
# Used to start with `Teach::Application`
/config/initializers/session_store.rb
1
2
3
4
5
6
7
8
9
Learn::Application.config.session_store :cookie_store, key: '_learn_session'

# Use the database for sessions instead of the cookie-based default,
# which shouldn't be used to store highly confidential information
# (create the session table with "rails generate session_migration")
# Learn::Application.config.session_store :active_record_store

# For the sake of cleanliness, I changed it from `Teach::Application` in both the actual
# line of code and the comment. Also, `:key` used to be '_teach_session'.

And in your /config/routes.rb file:

/config/routes.rb
1
2
3
Learn::Application.routes.draw do # Used to be Teach::Application.routes.draw
  # Routes in here
end

Finally, for consistency’s sake, if the names of your databases include your app name, you should update those in /config/database.yml

/config/database.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
development:
  adapter: mysql2
  encoding: utf8
  reconnect: true
  database: learn_development # Used to be teach_development
  username: root
  password:

test:
  adapter: mysql2
  encoding: utf8
  reconnect: true
  database: learn_test # Used to be teach_test
  username: root
  password:

production:
  adapter: mysql2
  encoding: utf8
  reconnect: true
  database: learn_production # Used to be teach_production
  username: root
  password:

Search your app and lib folders

grep through your /app and /lib folders to find any mentions of your project name and change them. This might be a little tedious if you have a name that conflicts with a bunch of Ruby/Rails stuff (like “def” or something stupid like that), but you only have to do it once.

Done!

That’s it! Now you have a renamed Rails 3 app.

Note: I did this on Rails 3.2.1. It’s possible some things will be different on other versions.

BostInnovation’s Seth Priebatsch Article is Childish Garbage

| Comments

Disclaimer: I was SCVNGR’s lead iOS developer for almost two years. I started a new job at GitHub last month. This is neither a critique nor a defense of the actions or words of Seth Priebatsch or SCVNGR.

Today, BostInnovation published an article called Seth Priebatsch Thinks Everyone at BostInno is Painfully Stupid… Really?. In it, they publish an internal SCVNGR email in which Seth insults them, then spend the rest of the article defending themselves from his insults and offering him advice on how to have fun and run a company.

Like most media outlets, I have mixed feelings about BostInnovation. The article in question, however, is childish garbage.

The are some points to be made here that anyone who is techy enough to read BostInnovation knows. There’s no way to prove this email is real. There’s no way to prove that the “leaker” didn’t tweak it. But those aren’t the important part.

The important part is, this was an internal email. Were it not for BostInnovation, under 100 people would’ve read it, all of whom work for SCVNGR. This email was no threat to BostInnovation’s readership. This is reminiscent of elementary school kids intercepting a passed note and reading it out loud to all their friends.

If the focus of this article was “Seth is lying to the media and investors,” that would be a different story (and would require significantly more evidence). I would still be a bit off-put by the publishing of an internal email in a private company, but at least that would be news worth reading. Instead, the focus is “CEO of company thinks we’re dumb, so here’s why he’s weird and mean.” If I were an advertiser on BostInnovation, I would be pretty upset with this.

Bash’s PS1 Syntax: The Inspiration for Brainfuck?

| Comments

I just spent way too much time struggling to get my Bash PS1 variable working right. Originally, it looked like this (parse_git_branch is a Bash function I have defined elsewhere):

Old PS1
1
PS1="\w\e[35m\$(parse_git_branch)\e[m > "

Now, it looks like this:

New PS1
1
PS1="\w\[\e[35m\]\$(parse_git_branch)\[\e[m\] > "

The difference? I wasn’t escaping my color codes correctly, which was causing Terminal to wrap long commands onto the beginning of the same line.

Let me break down Bash’s color code syntax, because it makes me so angry. A minimal Bash color code looks like this:

Minimal Bash color code
1
\e[1;30m

\e[ means “here comes a color code” I guess.

1 means “make it bold”. If you don’t want bold, leave it out.

; means “I’m finished telling you that this is bold”. If you don’t want bold, or if you want default-colored text (explained below), leave it out.

30 means “dark gray”. Here’s an exhaustive list of valid numbers (no number means default color):

  • 30 = dark gray
  • 31 = red
  • 32 = green
  • 33 = yellow
  • 34 = blue
  • 35 = purple
  • 36 = turquoise
  • 37 = light gray

0-29 don’t do anything (at least in Terminal.app on OS X 10.7.2). No idea why they start at 30.

m means “my color code is over”.

If you match that format, all subsequent characters will be that color (until you define another color code).

But that’s not all! This sequence is escaped incorrectly. If you have it in your PS1, Terminal will look correct on startup, but if you type a long command that wraps past the first line, Terminal will wrap it back to the beginning of the line you’re on and start overwriting characters.

I bet you think the fix is to match the open square bracket with a closed one, right? If so, give yourself a half pat on the back, then punch yourself in the face. Here’s how to escape it correctly.

How to escape a Bash color code
1
2
\e[1;30m     # Wrong
\[\e[1:30m\] # Right

You need to close match the open bracket (with an escaped close bracket), but also add another open (escaped) bracket to the front, also with no closer.

Anyone wanna educate me on the reason for all this madness? Right now, it just feels like some dude thought regular expression and strftime syntax were too verbose.

Using acts_as_list in a polymorphic scope

| Comments

If you’re using acts_as_list in a polymorphic scope, you need to define the scope a little bit differently to make everything work right.

Here’s the non-polymorphic example from acts_as_list’s README:

acts_as_list in a normal (non-polymorphic) scope
1
2
3
4
class TodoItem < ActiveRecord::Base
  belongs_to :todo_list
  acts_as_list scope: :todo_list # Note the single-item scope
end

We pass :todo_list as the scope (which acts_as_list converts to the todo_list_id column).

Now, let’s say we’re making a polymorphic Picture model (like in the Polymorphic Associations section of the Ruby on Rails Guides), and we want pictures to be sortable and reorderable. Here’s the right way to do it:

acts_as_list in a polymorphic scope (the RIGHT way)
1
2
3
4
class Picture < ActiveRecord::Base
  belongs_to :imageable, polymorphic: true
  acts_as_list scope: [:imageable_id, :imageable_type]
end

My pre-ARC Objective-C memory management conventions

| Comments

iOS 5 is coming soon, and introduces ARC. ARC will make Objective-C memory management significantly simpler. But, it will be a long time before iOS 5 is ubiquitous, and I’m not sold on the subset of ARC that will be available for iOS 4. So, in the meantime, I thought people might benefit from seeing the conventions I use to keep Objective-C memory management simple. Most iOS developers probably already follow something close to this, but it doesn’t hurt to have it written out.

If you follow these five simple rules, you should pretty much never get a memory leak or a crash from over-releasing.


1. Always declare @properties for your object instance variables

If you’re putting an instance variable on a class and it’s an object, declare a property for it.

Moving to Octopress

| Comments

I’m now moving all my stuff from Media Temple and Tumblr to here. All posts before this one are reposts.

My tips for an awesome WWDC

| Comments

I’ve seen a bunch of people who just got back from WWDC posting advice on how to have the best time possible. I figured I should do the same thing. Here are my tips.

Don’t get in the keynote line early

Even if you want to sit near the front, you don’t need to get in line until around 8:30am. It’s really easy to just run and push past all the submissive nerds and get an awesome seat.

Live-blog the keynote

People who aren’t at the keynote would much rather read you repeating Steve Jobs’ announcements sentence-by-sentence on Twitter than watch a live feed with pictures. Also, other people in the audience who are following you will love reliving the moment when they look through their tweets later on.

Complain about the food

You paid a lot of money for tickets, which entitles you to demand customized gourmet meals. Food should be the main concern at any tech conference, and if you aren’t absolutely overjoyed by what you’re putting in your mouth, you aren’t getting your money’s worth.

Don’t silence your phone before a session begins

I know they ask you to before every session (and you should roll your eyes and say “do they have to say this every time?” when they do), but they’re really just forced to say that by law. In reality, all the speakers helped make these devices, and it’ll make them proud to see you using them. In fact, feel free to quietly answer your phone right in the middle of a talk. No one will notice.

Code during sessions

There are very few places in the world where you will have the opportunity to write code. A conference that you paid $1600 to attend is one of these places, and you should take advantage of that. Besides, you can just watch the session videos later.

Don’t go to any parties afterwards

You’re probably better at programming than the other attendees. Meeting them is pointless, and won’t be fun (and all the parties charge lots of money for alcohol, especially at Apple’s beer bash). Also, your hotel room is another one of the aforementioned few places in the world where you can code, so you should stay there and do that instead.

These tips will guarantee you a quality WWDC 2012. Also, don’t read any other WWDC tip posts, they’re probably trying to troll you.

The misguided priorities of Twitter

| Comments

Today, Twitter announced (with some awesome spin) that they would hamstring the direct message capabilities of all 3rd-party Twitter apps.

It’s been clear for awhile that one of Twitter’s monetization strategies is to force everyone to use their mobile apps over those of third-parties. This will let them show promoted trending topics in the newly-neutered Dickbar to all mobile users. I understand that Twitter needs to find a way to make money, but this direction strikes me as extremely misguided.

I believe this lock-in strategy will never create enough revenue to cover a non-trivial portion of their operating costs (never mind the $10 billion they turned down from Google). In the meantime, it will alienate the developers who were once the lifeblood of the Twitter ecosystem. Without these developers, the 3rd party apps that Twitter does want (cool aggregations, visualizations, etc.) will not be built, and new developer-fueled uses of the service will not be able to thrive.

It seems to me that the folks making business decisions at Twitter are severely underestimating the importance of certain parts of the community, and/or severely overestimating the potential revenue from their own mobile apps.

How to reset/revert a single file with Git

| Comments

I’m making this post more for my own reference than to help anyone else, but feel free to comment if you have questions or anything.

If you’ve made a commit with git (let’s call it “commit A”), and you want to make another commit (let’s call it “commit B”) that, among other things, reverts the changes made to a single file (let’s call it file1.rb) in commit A, use the following command:

git reset commit-a -- path/to/file1.rb

That’ll create two sets of changes: a copy of the changes you made in commit A, and the inverse of the changes you made in commit A. The inverse is staged, while the copy is unstaged. Nine times out of ten, you’ll want to commit the staged changes (which, as they’re the inverse of the changes in commit A, will result in a revert of file1) and discard the unstaged ones.

My Doorbell Is Better Than Your “Please Rob Me”

| Comments

“Hey man, I know you really like Foursquare, but you should check out this new site called Please Rob Me. Foursquare is actually really dangerous, cuz now it’s easy for robbers to know when you’re not at your house!”

I’m a Foursquare user, and I’ve heard variations of this from over a dozen of my friends in the past 24 hours. For the record, I think “Please Rob Me” is a really funny and creative idea, and I love that they made it. However, anyone who is legitimately giving privacy/security advice over this is being ridiculous.

First of all, most people are out of the house from 9am to 5pm. Robbers know this, and Foursquare checkins don’t change it.

Second, there’s absolutely zero indication of whether or not anyone else is home, or how soon you’ll be getting back. These are much more crucial pieces of knowledge than “a single person was not in his house at this time”.

Last, and most important, a superior technology has existed for decades: the doorbell. Run around a neighborhood ringing doorbells of houses that look like they might be vacated, and break into the ones that no one answers.

“Oh but people don’t answer their doorbell every time, so a robber might break into an occupied house!”

Right. Just like people don’t have to actually be at a place to check into it on Foursquare, and like how checking in on Foursquare doesn’t mean no one else is home. There are potential false positives with both methods (and if there weren’t, people would be getting robbed a lot more). The point is, breaking in based on doorbell-ringing is much less dangerous than doing it based on Foursquare checkins, as it’s reasonably likely that no one is home if no one answers the doorbell

In reality, privacy via anonymity has always been a pretty shaky concept. Yes, checking into Foursquare does give robbers one more tool to make their job easier, but in the face of much better tools (like the doorbell), it’s negligible. If you chose to be a Foursquare user in the first place, Please Rob Me should have no impact on your decision.