Ask for Help

Interesting Things

  • If you're setting up a postgres database using ActiveRecord, and you see an error that looks like this:

    new encoding (UTF8) is incompatible with the encoding of the template database (SQL_ASCII)

then the problem may be that your template1 database (the default database that postgres copies from when you CREATE DATABASE) is configured to use ASCII, rather than UTF8. Try adding this option to the relevant section of your database.yml:

template: template0

More information about Template Databases in postgres

  • If you donate to Travis CI, then you will receive free stickers.

Kris HicksKris Hicks
git reset -p for fun and profit
edit Posted by Kris Hicks on Wednesday May 16, 2012 at 06:36AM

I've been using git reset -p a lot recently and I think it makes sense to clarify what it is that it does because when I first started using it I found it a little confusing.

I sometimes realize that an earlier commit contains some change that I don't want, so I want to remove it from the commit. This also works when not rebasing, so for simplicity I'll use an example where the commit to be modified is already HEAD. Previously I would have done:

git reset HEAD~1
git add -p
git commit -C <treeish>
git checkout .

Or, in English:

  • take off the HEAD commit, adding it to the working directory
  • add back the parts you want to keep
  • make a new commit using the message from what used to be the HEAD commit
  • throw away the changes you didn't want

With git reset -p this process is a little different. Here's what it looks like:

git reset -p HEAD~1
git commit --amend -C HEAD
git checkout .

Again, in English:

  • apply to the index the negations of certain parts of the HEAD commit
  • amend the HEAD commit with the negations
  • throw away the changes you don't want

How does this work?

In the second example you added -p to the reset command. This will reset only parts of the original commit, leaving it intact otherwise. That's worth stating a different way: When you reset -p, the original commit remains unchanged. The only changes are made to your working directory and index.

The trick is to know what you're doing when you're saying "y" to a hunk git presents to you for resetting. Say you added a line to the commit originally:

foo
+ bar
baz

But you want to get rid of it. When you git reset -p, git will ask you:

foo
- bar
baz
Apply this hunk to index [y,n,q,a,d,/,e,?]?

If you say 'y', Git will apply that hunk to the index. What you also get, however, is the original hunk (that added "bar") in your working directory.

To summarize, your working directory will have:

foo
+ bar
baz

While the index has:

foo
- bar
baz

While the commit (unchanged, remember) has:

foo
+ bar
baz

You now have a chance to tell git what you want to do, without having done anything to the original commit yet. In the example above, you wanted to get rid of the addition of the "bar" line. To do that, you want to take what's in the index (the negation of the addition of "bar") and apply it to the commit, making it go away:

git commit --amend -C HEAD

Then you still have in your working directory the adding of "bar", which in this case you just want to get rid of, so:

git checkout .

I like using reset -p because it makes it really easy to make small changes to a commit, removing something I added or putting back something I deleted.

reset -p allows you to more easily get a grip on the changes you've made and the ones you're about to make. It also makes much better use of Git, in that you can do even more interesting operations when in the resulting state, which I won't go into now as to avoid information overload.

And there you have it.

Jonathan BergerJonathan Berger
FOWD Day 2: Notes On Design
edit Posted by Jonathan Berger on Tuesday May 15, 2012 at 06:41AM

Brendan Dawes, hacker and interaction designer, trawled through his sketchbook to deliver an inspiring opening talk about his work, what inspires him, and some Rules for making. Full notes after the jump.

Justin RichardJustin Richard
Standup 5/14/12 - Watch your SafeBuffer
edit Posted by Justin Richard on Monday May 14, 2012 at 06:03PM

Interesting Things

ActiveSupport::SafeBuffer do not work with all the features of #gsub. Mind your strings, or fix this bug in ruby!

# normal: 
1.9.3-p125 :004 > "foo".gsub(/(?<my_string>.*)/) { $~[:my_string]*2 }
 => "foofoo" 

# broken:
1.9.3-p125 :005 > "foo".html_safe.gsub(/(?<my_string>.*)/) { $~[:my_string]*2 }
 => "" 

# round trip, back to working
1.9.3-p125 :006 > "foo".html_safe.to_str.gsub(/(?<my_string>.*)/) { $~[:my_string]*2 }
 => "foofoo"
</my_string></my_string></my_string>

Ash HoganAsh Hogan
Director of Product Management at Sharespost
edit Posted by Ash Hogan on Monday May 14, 2012 at 03:14PM

At Pivotal Labs, one of the services we provide our clients is helping them interview and hire. Pivotal Labs and our clients place a strong emphasis on Agile development and its many aspects: Pair Programming, Test-Driven Development, rapid iterations, and frequent refactoring.

Below is the job description for a Director of Product Management with Sharespost which is the only online platform that efficiently connects forward-thinking investors with late-stage venture-backed companies and their shareholders. They have the largest online network of private company investors ever assembled and the broadest offering of independent, third-party research. It’s all designed to make the process of managing secondary liquidity and raising primary capital easier and more transparent than ever before.

Sharespost is hiring - here's why you want to work there:

Ken MayerKen Mayer
Deploy strategies for HerokuSan
edit Posted by Ken Mayer on Monday May 14, 2012 at 09:42AM

Deploy Strategies

If you look at the network graphs of heroku_san on github, you'll see a number of branches where the only change is the deletion of the following line from the deploy task:

  • stage.migrate

If more than a few people are willing to take the effort to fork a gem just so they can delete 1 line, something smells. The reason is that these forkers were using something other than Rails+ActiveRecord+SQL in their project. Some were using Sinatra, others were using Rails, but with CouchDB.

The raison d'être for the heroku_san gem is to make Heroku deploys dirt simple. So, if people are making whole forks to customize the deploy task, we should make it less painful.

Jonathan BergerJonathan Berger
FOWD Day 1: Tapworthy Mobile Design
edit Posted by Jonathan Berger on Monday May 14, 2012 at 05:56AM

Designing for Touch with Josh Clark

Day 1 of the Future of Web Design was a fantastic workshop on designing for mobile with Josh Clark aka @globalmoxie, fellow Brooklynite and author of Tapworthy, a sharp guy, and a great speaker. Hop below the fold for the full notes.

Ken MayerKen Mayer
From customer requirements to releasable gem
edit Posted by Ken Mayer on Sunday May 13, 2012 at 11:42AM

One of the many pleasures of working at Pivotal Labs is that we are encouraged to release some of our work as open source. Often during the course of our engagements, we write code that might have wide-spread use. Due to the nature of our contracts, we can not unilaterally release such code. Those rights belong to the client. And rightly so. So, it is an even greater pleasure when one of our clients believes in "giving back" to the community, as well.

One such example is this modest gem, attribute_access_controllable which allows you to set read-only access at the attribute level, on a per-instance basis. For example, let's say that you have a model Person with an attribute birthday, which, for security purposes, cannot be changed once this attribute is set (except, perhaps, by an administrator with extraordinary privileges). Any future attempts to change this attribute will result in a validation error.

e.g.

> alice = Person.new(:birthday => '12/12/12')
=> #<Person id: nil, attr1: nil, created_at: nil, updated_at: nil, read_only_attributes: nil, birthday: "0012-12-12"> 
> alice.attr_read_only(:birthday)
=> #<Set: {"birthday"}> 
> alice.save!
=> true
> alice.birthday = "2012-12-12"
=> "2012-12-12" 
> alice.save!
ActiveRecord::RecordInvalid: Validation failed: Birthday is invalid, Birthday is read_only
> alice.save!(:skip_read_only => true)
=> true

Setting this up is trivial, thanks to a Rails generator which does most of the heavy lifting for you.

rails generate attribute_access Person

After that, you need only know about one new method added to your class:

#attr_read_only(*attributes) # Marks attributes as read-only

There are a few others, but this one, plus the new functionality added to #save and #save! will get you quite far.

And if that's all that you were looking for when you stumbled across this article, then there's no need to read any further. Go install the gem and have fun (and may your tests be green when you expect them to be).

Jonathan BergerJonathan Berger
The Future of Web Design in London
edit Posted by Jonathan Berger on Friday May 11, 2012 at 11:04AM

I'm very excited to head to London next week to speak Code Literacy for Designers at the always-excellent Future of Web Design Conference. Working at Pivotal Labs, I've learned a lot over the last few years about how Agile software development and design interact, and I'm really looking forward to engaging in the conversation in one of my favorite cities. If you'll be at FOWD, please come check it out at 11:15 on Wed! And if you're in London and want to talk UX, drop me a line!

From the talk description:

Do you spend half your day on mockups in Illustrator and the other half on Javascript in a text editor? Know anyone who does? The way we work is changing. Rigid, traditionally defined roles like "Designer" and "Developer" are being displaced by interdisciplinary skillsets and a culture of collective product ownership. In this talk, we’ll investigate how treating Coding as Literacy can affect the way decisions are made and work gets done, describe how varying levels of literacy among teammates facilitate effective agile design and development, and discuss how designers can get literate in technical topics.

Carrierwave has an RMagick module that offers a handy manipulate! method that takes a block where you can modify the image, and then it saves the changes to the image.

But sometimes you only want to interrogate the image, and there's no need to save any changes.

After spending probably 30-45 minutes trying to figure out how to read an image that may not necessarily exist on your local disk, the simplest solution we found was to call uploader#read to get the contents of the file as a string, and then pass that into Magick::Image.from_blob.

Other articles: