markevans/dragonfly — pull request #183 by DimaSamodurov (2012-04-10 00:33:31+00:00)
Title: Use default_internal encoding for uid generated by data store.

Background:
ruby 1.9
Encoding.default_internal and Encoding.default_external set to 'UTF-8'

Migrating from Ruby 1.8.7 to ruby 1.9.3 and running test suite i got several failures related to character encoding.

One failure was thrown by comparison of urls generated by Dragonfly.

Test case (integration test with capybara):
- Create a couple of model objects with images.
- Display list of images on the page.
- Verify image urls of objects created are present on the page.

Last step failed because image url of newly created object and image url of object loaded from database were different.

Looking deeper i found that uid of just created temp_object had 'US-ASCII' encoding but when it was saved to database as 'image_uid' and loaded back it got 'UTF-8' encoding.
And marshaling of equal strings with different encoding produces different results.

Discovering reasons why new uid has 'US-ASCII' encoding i found that is a Ruby behaviour.
Fixnum.to_s.encoding always return 'US-ASCII' for example, that leads BSON::ObjectID.to_s.encoding equal 'US-ASCII'. I guess Fixnum.to_s does not use Encoding.default_internal for performance or historical reasons. Anyway as different data stores can use different approaches of generation uids it's better to make them return value with specified encoding.

So I updated .store method to return value encoded with Encoding.default_internal . May be this should be default_external instead, but they rarely differ.