Monthly Archives: May 2011

Interesting Things

Mike Gehard mentioned that there is a movement to extract ActiveResource from Rails. For now, it’s been shot down. But don’t bet against the haters! Work is underway to rewrite it from scratch. We’ll probably hear more about this as RailsConf proceeds.

Interesting Things

Beware setting class variables in Rails Initializers: Schubert warned us that if you’re setting vars on your Rails classes inside of config/initializers, you’ll see weird things happen in development mode.

If you set a class var on a model in an initializer, the value will be available on your first request to the app. However, upon the second request, Rails will reload the class, but it will not reload the initializers. At this point, you’ll have lost the value.

Bash Brace Expansion: If you ever find yourself renaming a file in some faraway path, you think to yourself, “Wouldn’t be nice if I didn’t have to specify the entire path and filename twice?” Many shells provide you with a nice shortcut.

As mentioned in yesterday’s standup blog, my pair and I encountered some problems with YAML parsing over the last few days, and now that I think I understand it I wanted to document it for posterity.

Psych is a new YAML parser which presumably is better than what came before it, but can’t merge hash keys correctly and doesn’t work with delayed job. The merging of hash keys is serious, as our standard databse.yml defines a common section, and back references it merging in individual database name and settings. When psych is loaded, we get a blank database name, which makes active record pretty much useless.

Ruby 1.9.2 optionally compiles psych into ruby if you have libyaml installed on the computer. Some gems will require psych if it’s available, thus poisoning any future YAML parsing which does not expect psych’s pedantic (and currently broken) behavior.

You can always look at the value of the YAML constant – it can varry between YAML, Syck and Psych, depending on what’s loaded. You can switch the yamler by adding YAML::ENGINE.yamler = ‘syck’, but you need to make sure this happens at every code entry point.

For now, I’m no longer install libyaml and libyaml-devel on servers, which got there because I had been following RVM’s information ruby prerequisites. I’ll also write a chef recipe to assert that libyaml is not installed.

Interesting

Kris Hicks reassures us that the vim path through git interactive rebases need not lead to maddness. If you’d like to do an interactive rebase in your editor of choice (rather than Textmate, the Pivotal default) you can set the GIT_EDITOR flag. So go ahead to the terminal and

export GIT_EDITOR=vim && git rebase -i origin/master

Vim will launch, changes will be made, commits will be squashed, and all will be right with the world. Until you try to save; after making your changes and :wq-ing, the terminal will admonish you Could not execute editor.

The problem is a vi + Mac OS X + git incompatibility with pathogen (the vim package manager). To fix it, add the following lines to your .vimrc file:

We’re planning to decommission our gems.pivotallabs.com RubyGems server on Friday, June 17th. If you are dependent on gems hosted there, and the logs suggest there aren’t many of you, please find an alternate source for your gems.

Since Bundler has become available, there are now other options for making custom or forked gems available to Ruby applications, so we no longer have a need for our own dedicated Gem server. You can point to rubygems.org or direct bundler to the github repos for all of the gems we are currently hosting. We hope this doesn’t cause any significant inconvenience and please leave a comment if you have concerns.

Ask for Help

“Rubymine is excluding code in our gem’s spec/ directory from being indexed, can that be changed?”

We assume this is an optimization, because most people aren’t interested in anything from a gem except the lib/ directory. There doesn’t appear to be a way to change this behavior.

“How can I override the gem version requirements on annoying gems in my project?”

We’re trying to bring in rails 2.3.2 + rack 1.1.x, but rails explicitly requires the old version of the rack gem. Can we do something sneaky to change the requirements without forking and modifying the gem??

“Is there a good set of practices for exposing ActiveRecord models to JavaScript?”

Like using Backbone.js model support? Or maybe just straight #to_json on models and embedding them into HTML templates?

Have you upgraded RubyGems lately? Is your console suddenly filled with warnings like this?

NOTE: Gem::Specification#default_executable= is deprecated with no replacement. It will be removed on or after 2011-10-01.
Gem::Specification#default_executable= called from /Users/chaffee/.rvm/gems/ruby-1.9.2-p0/specifications/thin-1.2.7.gemspec:10.

You may be showing signs of a new malady known as Warningitis! So far there is no cure, but doing the following will temporarily cure your symptoms:

Ask for Help

“A file is being loaded twice by Rails, and I’m not sure why. How do I figure out what’s reloading it?”

Try putting this at the top of the file:

puts caller
puts "-" * 80

#caller is a Kernel method which returns the backtrace at the time it’s called as an array of strings. Every time the file is loaded, you’ll see the backtrace of what loaded it. The second line makes a separator so you can see the break between stacktraces.

“Is there a command that will make a new file in a path of directories that don’t exist yet? Like mkdir -p but for files? Maybe touch -p?”

No. No one knows why.

Interesting Things

Chrome Tab Overview: Chrome can now show you an overview of all tabs in a window like Exposé does for windows. It’s an experimental feature. To enable it, go to , enable “Tab Overview”, and restart Chrome. Then three-finger swipe down to see all your tabs.

ERROR: current transaction is aborted: On Postgres, sometimes RSpec will show the error “ERROR: current transaction is aborted, commands ignored until end of transaction block.” The backtrace of this error is not useful, because it’s the backtrace of the first operation to use the database connection after some error occurred. These errors are tricky to debug.

The people who hit it had luck with temporarily turning off use_transactional_examples in the RSpec config, which let them see the error. Just remember to turn it back on, because transactions keep your tests clean and fast. (Note: use_transactional_examples and use_transactional_fixtures are synonymous.)

Lately I’ve had the opportunity to use Jasmine to test drive a whole bunch of Javascript and am loving it. If you haven’t had a chance to take Jasmine for a spin, I recommend you take some time to do so.

When I heard that Rails 3.1 was going to include CoffeeScript I decided to work to figure out how I could write both my production code as well as my specs in Coffeescript.

Using this gist as a guide, I came up with these detailed instructions:

Start up the Jasmine server using rake jasmine and point your browser to http://localhost:8888. You should see 0 specs, 0 failures. Since you have changed the location that Jasmine looks for spec files, you aren’t picking up the example Jasmine specs any longer.

Create a new spec file in spec/javascripts/math_spec.coffee with the following contents:

Refresh your Jasmine browser window and you should see 1 passing spec. Again, Guard has compiled your implementation file and Jasmine uses it to satisfy the spec.

At some point, it would be nice to figure out a way to run Jasmine specs directly from the .coffee files and against the implementation .coffee files without having to use Guard to compile them. With the above steps, you still have to check in the compiled files into source control so your tests can be run in the CI environment. If you miss one of the compiled specs or one of the compiled implementation files, your CI environment may report improper results.

Ask for Help

The dialog animates out (asynchronously, of course), and it’s hard to have a test pass when it successfully disappears.

“In Javascript, How can we create an array of n empty arrays?”

One attempt, (new Array(n)).map(function() { return [] }) failed, because new Array(n) creates an array, n elements long, full of undefined. As it turns out, map() skips undefined in an array.

[Correction: new Array(n) creates an array which says its length is n, but which doesn’t have any elements, not even undefined. The following does map over the undefined elements (for n=3), but isn’t very useful to us.

[undefined, undefined, undefined].map(function() { return [] })

Thanks to John Pignata for pointing that out.]

Interesting Things

The latest version of Chrome Frame for Internet Explorer doesn’t require admin rights to install.