Olson released

If you liked my post Using I18n and Draper to Render Database Attributes, you might like Olson.

Check it out!

Comments
Comments

Google maps api with custom markers in internet explorer

I recently had an issue with custom map icons for the google maps api. They didn’t show up at all in internet explorer 7 or 8. I guess the markers were there but the custom images failed to load correctly because you could still interact with them.

Some quick googling found that other folks have had the same issue.

Some suggestions were to: switch back to the google maps api v2, use .ico images instead of .png, or use the MarkerImage class. Going back to v2 doesn’t sound like a great plan to me, and I couldn’t get any of the other suggested workarounds to work, but I did discover a hack.

I discovered that the custom icon on this page works fine in internet explorer. After some tinkering, I discovered that the critical difference was the draggable option. In internet explorer, when the draggable option is enabled on my custom icons, they show up just fine, but when the draggable option is disabled, they disappear. In fact, I can change the draggable option back and forth with a timeout or in console and watch the custom markers disappear and reappear.

Well hmmm, that’s neat, my custom markers show up in internet explorer now, and I don’t need to finagle .ico files any more, but I don’t want them to be draggable. Below is a gist of my solution; enable draggable, but disable dragging by capturing the “drag” event. It’s definitely a hack, but I thought I’d share in case it saves anyone else some time.

Comments

A quick Sublime Text 2 snippet

‘nuf said

Comments

Git tip: git checkout —merge

Do you ever find yourself doing this?

Here’s a nice little shortcut:

Comments

Migration Tests

Earlier today I set out to write a migration with logic (it adds a column and then migrates data), and I thought “wouldn’t it me nice to test this migration”. I’ve seen people write migration tests before, and other blog posts show lots of setup and weirdness. I ended up scraping this migration (and the concept), but I thought I’d post how freaking easy it was to write a simple migration spec:

Normally, I wouldn’t write a spec just for changed columns, but this serves as a nice example and starting point for more complex migrations :)

Comments

Using I18n and Draper to Render Database Attributes

TL;DR;: Check out my additions to ApplicationDecorator in this gist.

Update: This has been released as a gem: Olson.

When my models have an attribute that matters to the code (like Admin#role or User#status), I like to store the value as a string that makes sense as an identifier. For example, User#status might be ‘active’ or ‘awaiting_approval’. However, when it comes time to render the admin’s role or the users status in the view, we want to show ‘Awaiting approval’ instead of ‘awaiting_approval’. Another example of this sort of thing is the #type attribute for STI.

Ok, this isn’t too hard, we can just use #humanize. But, here’s what happens:

Ok, let’s be fair. All of these solutions are actually quite fine. In most cases Ya Ain’t Gonna Need anything more complicated. The helper version handles most situations just fine.

However, after a bunch of this I tend to end up with a bunch of methods in my model that seem to be somewhat presentation related, and/or methods in my helper that seem like they belong to an object and not in the “global” view namespace.

Enter decorators

A decorator (or presenter) is an object that holds the presentation logic for a model, so that the model can stick to the business logic. I’ve been using a great gem called Draper. I won’t go into too much detail about how to use Draper (check out the Github readme or Railscast).

Here’s how you would implement the above pattern with Draper:

Then this is our view:

Bonus

Now:

My Abstractions

And now the reason for this post. I find that I use this pattern frequently, so I generalized it to ApplicationDecorator. It adds a class method ApplicationDecorator.humanizes that can be used in each decorator to define attributes that need automatic humanization.

The full source can be found here: https://gist.github.com/1338134 (Show inline)

Here’s how you would use it:

And in the view:

To Conclude

I like this because each layer is really simple and really focuses on only what it needs to.

  • The view doesn’t have to know that that data is not user-friendly.
  • The model isn’t polluted with methods designed for the view.
  • There isn’t much complexity or black-magic to make this abstraction simple.

If this pattern works out in my current project I will probably pull this out into a gem. Would anyone else find this useful? If I do I’ll be looking for name suggestions…

Comments

I Can Haz Disqus

I’ve added comments to this blog. So, if you wanted to comment on my posts, now you can!

Comments

How to get GetBundles working with RVM

Ever since I set rvm to use ruby 1.9.2 as my default, GetBundles hasn’t been working.

A quick peek at TextMateGetBundles.log revealed that there were syntax errors in Support/getBundles.rb. Here’s what I did to make GetBundles use ruby 1.8.7

This is how I got GetBundles to work with rvm.

Read More

Comments
Do you practice specific skills with repetition and intent? Athletes do drills. Musicians hone difficult passages. What do you do?
Comments
Next »