If you never heard of merb, stop reading this and go check it out!!!
One of things you will encounter when using the coolest and latest technologies is that they might contain bugs
. It is what we encountered in using datamapper 0.2.5 and do_postgres 0.2.3 in our merb 0.9.2 development stack for ShellShadow.
Here’s a quick look at how we add our patches into our merb app.
One of nasty bugs existing in datamapper 0.2.5 or 0.3.0 is that it will interpret NULL integer fields from a table as 0. It was a huge issue for us and had to be patched.
Turns out the culprit is not datamapper but in do_postgres. Many thanks to Adam French for pointing this out on #datamapper irc channel.
The buggy code was in [GEM_PATH]/gems/do_postgres-0.2.3/lib/do_postgres.rb in class Reader < DataObject::Reader method typecast(val, field_type).
The code in questions was:
when "INT2", "INT4", "OID", "TID", "XID", "CID", "INT8"
val.to_i
This should read:
when "INT2", "INT4", "OID", "TID", "XID", "CID", "INT8"
val == "" ? nil : val.to_i
The fix was a one liner but we needed to replace the entire method it was in.
This is how you apply your monkeypatch:
- Add your patch file under <your_merb_project>/lib. Lets call it do_postgres_patch.rb. We put our modified copy of typecast in this file.
- In <your_merb_project>/config/init.rb, you’ll see at the very bottom
Merb::BootLoader.after_app_loads do
### Add dependencies here that must load after the application loads:
# dependency "magic_admin" # this gem uses the app's model classes
end
In this block, add:
require 'core_extensions'
That’s it. Easy and all patched up!!!
