Blog


Apr

Campfire Stories: March 26-29


Monday, March 26

Nothing to report.

Tuesday, March 27

Waging War on Whitespace (Using TextMate)

Getting rid of annoying diff cruft.

http://blogobaggins.com/2009/03/31/waging-war-on-whitespace.html

Wednesday, March 28

Jump

No comment.

Thursday, March 29

CSS3 Gradient Buttons the Right Way

CSS buttons

Guide to creating nice buttons with just CSS.

http://theosom.com/p/A07g

Paper

Lovely new sketching app for the iPad.

http://www.fiftythree.com/paper

Friday, March 30

Bang methods; or, Danger, Will Rubyist!

Don't overuse the bang!

http://dablog.rubypal.com/2007/8/15/bang-methods-or-danger-will-rubyist

Pastel

Beautiful web app template with lots and lots of features.

http://pattern.dk/pastel/home.html

Our logo

Our logo

We finally have our logo over the entrance to our office.

Trusted Keys

Anders T released a new open source library for dealing with mass assignment of attributes.

http://github.com/unders/trusted_keys

Mar

Campfire Stories: March 19-23


Monday, March 19

CarrierWave Direct

Add-on to Jonas' file upload library CarrierWave for direct uploads to S3 and background processing of the files.

http://github.com/dwilkie/carrierwave_direct

Tuesday, March 20

Working with time zones in Ruby on Rails

Nicklas wrote a blog post with some guidelines for dealing with timezone issues in Ruby on Rails.

http://elabs.se/blog/36-working-with-time-zones-in-ruby-on-rails

hubot, mustache me cjkihlbom

hubot, mustache me cjkihlbom

No comment.

Sonos for Mac

Sonos for Mac

The new version of the Sonos controller app for Mac OS X is a huge improvement.

Wednesday, March 21

Browser you love(d) to hate

Microsoft is doing a big marketing campaign for IE9. Not bad.

http://browseryoulovedtohate.com/

Thursday, March 22

rspec-fire

More resilient test doubles for RSpec, from Xavier Shay.

http://github.com/xaviershay/rspec-fire

Photoshop CS6 Beta

The new version of Photoshop is out in beta. Jimmy and Johannes are very excited about this.

http://bjango.com/articles/photoshopcs6/

exciting

Tesseract

Tesseract is a JavaScript library for filtering large multivariate datasets in the browser. Tesseract supports extremely fast (under 30ms) interaction with coordinated views, even with datasets containing a million or more records; we built it to power analytics for Square Register, allowing merchants to slice and dice their payment history fluidly.

http://square.github.com/tesseract/

GitHub's Ruby style guide

Pair programming really helps everyone keep a consistent coding style, but GitHub's style guide was an interesting read nonetheless.

http://github.com/styleguide/ruby

Friday, March 23

Celluloid

Actors and futures in Ruby, wrapped in a nice API.

http://github.com/celluloid/celluloid

How to write a bug report

Having a problem with an open source library? Here are Jonas' tips on writing a good bug report that will increase your chance of getting help from the library author.

http://elabs.se/blog/38-how-to-write-a-bug-report

CSS 3D Clouds

Check out the Michael Bay preset.

http://www.clicktorelease.com/code/css3dclouds/

Baldur's Gate for iPad

Baldur's Gate on iPad

Baldur's Gate is coming to the iPad! Forget Google Maps, iBooks or iPhoto. This is the killer app.

http://www.macrumors.com/2012/03/23/baldurs-gate-enhanced-edition-coming-to-ipad/

Mar

How to write a bug report


This is a blog post I've wanted to write for a really long time. Every couple of days I get an issue reported to one of my open-source projects which looks something like this:

Capybara doesn't work

It's totally broken. I tried everything. Why doesn't it work? Please fix it for me!

Okay, maybe it's not quite that bad. As you might guess, such a bug report is completely useless, it's just a burden to everyone involved in the project to have to tackle this kind of stuff. Worse than that, the person who posted this isn't going to get the help they need, and their possibly very legitimate issue is not going to get fixed.

Every time I receive one of these I wish there were some kind of source I could point these people to, so they might learn how to contribute with insight to open-source projects, instead of being a burden. This is that source.

Closed source

This stuff only applies to open-source, free as in beer and speech projects. Closed source projects are a different game, though some of this advice may still be useful.

The holy grail of bug report templates

This is where you should start. I can't remember which project I first saw this template on, so unfortunately I can't give credit where it's due, suffice to say I didn't invent it and am not claiming credit for it.

Answer these three very simple questions:

  • What did you do?
  • What did you expect to happen?
  • What happened instead?

Just by answering these three questions, you're already doing better than most bug reports out there.

If you encounter an error, any kind of error, make sure to include a stack trace. Those are immensely useful. Also be sure that the stack trace is complete, some frameworks such as Rails or test frameworks like RSpec and Cucumber tend to filter the stack trace. In RSpec and Cucumber, run your tests with -b to get a full stack trace. If you can't get a stack trace, make sure to expand upon the third question as much as possible.

A bug report isn't a question

If you're asking why something broke, if you're wondering what the cause of a particular behaviour is, you aren't actually writing a bug report, you're asking a question.

Asking questions is good, and I encourage you to reach out when you need help and have exhausted all other options. Asking questions on an issue tracker however is bad. Issues need to be closed, they need to be resolved; a question cannot really be resolved. It can be answered, but then that answer needs to be acknowledged and accepted and so on. There is a lot of process involved in closing each issue and it takes time away from the maintainers to focus on more important things.

Most open source project have a mailing list, use that for asking questions. If there isn't one, use StackOverflow or a more generic mailing list. If you've posted to one of these and you didn't get an answer in two weeks or so, investigate again and see if you can't find the answer yourself. If you can find it, make sure to answer your own question in the forum you asked it, so that others who have the same question in the future may find your answer through the almighty Google. If all of that's failed, that's when you're allowed to open an issue for a question.

When you send a bug report, you should be able to at least speculate at what the cause might be. Saying feature X broke doesn't help at all. Most open source projects of decent size are used by thousands of people, they have massive, comprehensive test suites. The most basic functionality in the project very likely isn't broken. So even if you have a legitimate issue, it likely happened under a specific set of circumstances. You need to pare away possible causes until you are left with the most minimal set of conditions needed to replicate the issue. Which brings us to:

Replication

While it isn't strictly necessary for a bug report, it's immensely helpful to have some way to replicate the issue. It's especially helpful to have an executable way of replicating an issue.

I generally dislike sample applications, such as Rails applications for this. There are too many possible areas where breakage could occur, and it takes too long for me to understand how all the pieces fit together. If you need a complete sample application, you likely don't understand the issue well enough yet to send a good bug report. Pare it down further until you truly understand what components cause the problem.

The absolute best way is of course to send a failing test case. You definitely don't need to actually fix the problem, though of course we appreciate it if you do. Understanding a code base can be troublesome, so it's cool if you don't want to spend the time to understand how best to resolve an issue. But if you do spend the time to write a test case, you reduce the time it takes the maintainers to solve your problem by an order of magnitude. Seriously.

A well formatted email

If you're sending a message to a mailing list, be aware that reading code or stack traces inline in an email message is quite painful. Either keep the code samples very short or link to a pastie/gist or something. Reading long code samples without syntax highlighting and possibly with automatically wrapped lines is quite painful.

Debugging

Debugging is a central part of software development. You cannot get by as a programmer without being good at debugging. Use the skills you use to solve problems in your applications when you encounter what you perceive as bugs in open source projects. Debugging really is a simple process:

  1. Isolate the cause
  2. Fix it

Where 99.8%¹ of the time is spent on point 1. If you don't do the first point, you are effectively asking someone else to do most of your work for you. For free.

Be polite

The people who write open-source projects do not owe you anything. They've invested a lot of time, which you get for free, but they are under no obligation to keep providing that time to you, or to provide more of their time just because you demand it.

The most common form of this I see are the emails I get to my personal email account from people asking for help. Asking me for help in a private forum such as email is asking for a handout. It's a form of begging. Please don't do it. I hate getting these emails, and I hate having to reply that I don't answer questions sent to me privately.

Some people believe that instead, you owe authors for the software they provide, but I don't really believe that either. I think the only thing you do owe them is to treat them with the same respect and politeness that you would extend to anyone else.

So no one owes each other anything really, and that's cool.

Don't be disappointed by the way, if it takes a while for your issue to get a response. It's cool if you ping the author after a couple of weeks if nothing has happened. Sometimes one can lose motivation, or become engrossed in something else.

Summary

Be polite. Be explicit. Be as knowledgeable about the causes of your issue as you can be. Answer these following questions:

  • What did you do?
  • What did you expect to happen?
  • What happened instead?

¹ totally made up number

Mar

Working with time zones in Ruby on Rails


Rails provides great tools for working with time zones but there's still a lot of things that can go wrong. This blog post aims to shed some light on these gotchas and provide solutions to the most common problems.

The one that probably has tricked me the most times is the fact that Rails fools you to believe it got you all covered all the time (pardon the pun). Don't get me wrong. I want Rails to do as much work for me as possible. But I've learnt the hard way that I can't get away with not knowing when and how Rails is helping me. Another gotcha is the fact that you have more time zones in play than you might first believe. Consider the following: db, server, dev machine, system configured, user specific configured and the browser.

Configure your Rails app

So what tools do we have at our disposal as Rails developers? The most important one is the config.time_zone configuration in your config/application.rb file. ActiveRecord will help you convert from and to (which the documentation fails to explain) UTC and the time zone of your choice. This means that if all you're doing is having users post times through a form and use Active Record to persist it you're good to go.

Processing time information

So what about actually doing something with the time information before persisting it? That's when it becomes tricky.

Parsing

When parsing time information it's important to never do it without specifying the time zone. The best way to do this is to use Time.zone.parse (which will use the time zone specified in config.time_zone) instead of just Time.parse (which will use the computer's time zone).

Work with Numerical and ActiveRecord attributes

Method calls like 2.hours.ago uses the time zone you've configured, so use these if you can! The same thing is true for time attributes on ActiveRecord models.

post = Post.first
post.published_at #=> Thu, 22 Mar 2012 00:00:00 CDT -05:00

ActiveRecord fetches the UTC time from the database and converts it to the time zone in config.time_zone for you.

Date vs Time

Time has date information but Date does NOT have time information. Even if you don't think you care you might realize that you do sooner then later. Be safe and use Time (or DateTime if you need support for times very far from the present).

But let's say you're stuck with a Date that you need to treat as a Time, at least make sure to convert it to your configured time zone:

1.day.from_now # => Fri, 03 Mar 2012 22:04:47 JST +09:00
Date.today.in_time_zone # => Fri, 02 Mar 2012 00:00:00 JST +09:00

Never use:

Date.today.to_time # => 2012-03-02 00:00:00 +0100

Querying

Since Rails know that your time information is stored as UTC in the database it will convert any time you give it to UTC.

Post.where(["posts.published_at > ?", Time.current])

Just be sure to never construct the query string by hand and always use Time.current as the base and you should be safe.

Working with APIs

Supplying

Building a web API for others to consume? Make sure to always send all time data as UTC (and specify that this is the case).

Time.current.utc.iso8601 #=> "2012-03-16T14:55:33Z"

Read more about why iso8601 is advisable here: http://devblog.avdi.org/2009/10/25/iso8601-dates-in-ruby/

Consuming

When you get the time information from an external API which you don't have control over you simply need to figure out the format and time zone it's sent to you with. Because Time.zone.parse might not work with the format you receive you might need to use:

Time.strptime(time_string, "%Y-%m-%dT%H:%M:%S%z").in_time_zone

This assumes time_string a iso8601 formated string. strptime will throw a very unintuitive error complaining on the format argument when in reality the problem is that the time string's format mismatches the format template argument. in_time_zone defaults to use the Rails configured time zone.

Why there's no strptime method on Time.zone when there's a parse beats me.

Working with multiple user time zones

Many systems needs to support users entering and viewing time information in a variety of time zones. To achieve this you need to store each user's time zone (probably just one of the time zone string names found in rake time:zones:all). Then to actually use that time zone the most common pattern is to simply create a private method in your ActionController and run it as an around filter.

around_filter :user_time_zone, :if => :current_user

def user_time_zone(&block)
  Time.use_zone(current_user.time_zone, &block)
end

This will do the same thing as config.time_zone but on a per request basis. I still recommend to change the default config.time_zone to a time zone that is a good default for your users. (Thank you Matt Bridges for pointing out the potential problems with using a before_filter instead of an around_filter.)

Testing

All the above is something that your tests should catch for you. The problem is that you as the user and your computer as the development server happen to reside in the same time zone. This is rarely the case once you push things to production.

There is Zonebie, a gem that helps you deal with this. I haven't had time to try it out myself yet, but it looks promising. If you find this to be overkill, at least make sure that your tests run with Time.zone set to another time zone than the one your development machine is in!

Cheat Sheet

DOs

2.hours.ago # => Fri, 02 Mar 2012 20:04:47 JST +09:00
1.day.from_now # => Fri, 03 Mar 2012 22:04:47 JST +09:00
Date.today.in_time_zone # => Fri, 02 Mar 2012 22:04:47 JST +09:00
Date.current # => Fri, 02 Mar
Time.zone.parse("2012-03-02 16:05:37") # => Fri, 02 Mar 2012 16:05:37 JST +09:00
Time.zone.now # => Fri, 02 Mar 2012 22:04:47 JST +09:00
Time.current # Same thing but shorter. (Thank you Lukas Sarnacki pointing this out.)
Time.zone.today # If you really can't have a Time or DateTime for some reason
Time.current.utc.iso8601 # When supliyng an API (you can actually skip .zone here, but I find it better to always use it, than miss it when it's needed)
Time.strptime(time_string, "%Y-%m-%dT%H:%M:%S%z").in_time_zone # If you can't use time.zone.parse

DON'Ts

Time.now # => Returns system time and ignores your configured time zone.
Time.parse("2012-03-02 16:05:37") # => Will assume time string given is in the system's time zone.
Time.strptime(time_string, "%Y-%m-%dT%H:%M:%S%z") # Same problem as with Time#parse.
Date.today # This could be yesterday or tomorrow depending on the machine's time zone.
Date.today.to_time # => # Still not the configured time zone.

Epilogue

I hope you've learned something from this post. I sure did while writing it! If you have any feedback on how it can be improved, or if you spot any errors, please let me know by posting a comment below!

Ruby and Rails version

This article was first written in March 2012. Back then Rails 3.2 was the new hot and as you all know a lot happens in Rails-land in two and a half years and will continue to do so. I will do my best to keep the article accurate and up to date with the latest versions of Rails. If you spot anything that is reported deprecated or not working please let me know in the comment section below!

  • Article publish date: 2012-03-20
  • Article last updated: 2014-11-04
  • Last verified Rails version: 4.1.7
  • Last verified Ruby version: 2.1.4p265

There is a git repository which you can clone:

git clone git@github.com:ramhoj/time-zone-article.git
cd time-zone-article
bundle install
rake db:create:all db:migrate db:test:prepare
rspec spec/

The Rails application is running on the version defined above and has been verified to work under the described Ruby version above. If you want to make sure things are working in the version of Rails or Ruby that you're using please fork the repository and make the necessary adjustments and run the test suite. If you want more in-debt, hands-on of the examples this repository's test suite aims to help with this too.

Changelog

See the git repository's commits.

Mar

Campfire Stories: March 12-16


I missed a week while traveling, but here's another edition of Campfire Stories, pulled from our Campfire rooms last week.

Monday, March 12

MongoDB Stockholm

Conference about MongoDB in Stockholm, April 5th.

http://www.10gen.com/events/mongodb-stockholm

Tuesday, March 13

Elabs team on Coderwall

http://coderwall.com/teams/4f271951973bf00004000661

Hating on IE6

Coderwall encourages users to update their profiles by showing you "embarrasing" defaults.

http://coderwall.com/blog/2012-02-23-hating-on-IE6

Nordic Ruby speakers

We announced the speakers for Nordic Ruby, the Ruby conference that we organise every year. We have an amazing lineup of speakers again this year, and we can't wait for the event in June.

http://nordicruby.org/speakers

Landing page for ProjectPuzzle

As we mentioned in our previous Campfire Stories post, we've been working on an app to help with project scheduling. We're getting closer to launch, but in the mean time we've put up a landing page where you can sign up if you're interested in learning more about the app when it launches.

http://projectpuzzle.com/

Elycharts

Elycharts looks like an interesting alternative to Highcharts JS.

http://elycharts.com/

Kalandae

A framework agnostic Javascript date picker.

http://github.com/ChiperSoft/Kalendae

Wednesday, March 14

Strong Parameters

Gem from the Rails core team to help deal with mass assignment security issues. An alternative to our own trusted_attributes.

http://github.com/rails/strong_parameters

Using event capturing to improve Basecamp page load times

37signals describes how they improved page load times for the new Basecamp by deferring Javascript event bindings.

http://37signals.com/svn/posts/3137-using-event-capturing-to-improve-basecamp-page-load-times

Entypo

More pictograms.

http://www.entypo.com/

Thursday, March 15

Dashboard

Detail from the internal dashboard we're working on.

Friday, March 16

Where's _why?

Wonderful article about the disappearance of "Why the lucky stiff", one of the heroes of the Ruby community.

http://www.slate.com/articles/technology/technology/2012/03/…

PaintCode

A vector drawing app that generates resolution independent Objective-C drawing code for Mac OS X and iOS.

http://www.paintcodeapp.com/