Freitag, Oktober 31, 2008

legacy databases with datamapper

For my current project I choose datamapper wich has some advantages over ActiveRecord. I need a thread safe ORM which doesnt need to do much but performing real fast. I am dealing with legacy table names but couldnt find any documentation how this is done though its very simple:

Your model needs a method storage_name like :

def self.storage_name(repository)
  "your_legacy_table_name"
end

Repository can be ignored if you are just dealing with one database.

ps: afcool83 mentioned a smarter way to do this:

storage_names[:default]='your_legacy_table_name'

Mittwoch, Oktober 15, 2008

the power of testing

I really thought we are over this point yet but it turns out to be a mistake. I work in a couple of projects in parallel so I meet a lot of developers and see many different types. I am really surprised to realize that the need to write tests still is so much underestimated.

All the classics like "no time to write tests", "is it really worth the effort" to "I thought this is just a webapp, so there is no need to write tests".

It happend to often too me, so here is my lesson learned if you start a new project with existing code. Ask for the tests. Take a peek a their test code, it gives you far more insight then any arbitrary code. It doesnt mean dont take any projects where no tests have been written, but it gives you different arguments. Its like dealing with legacy code, you cannot start implementing features as everything is fine.
But if the project leaders dont realize this fact forget the project, its a death march.


To say it with Obie Fernandez words
"Delivering Rails applications without automated test coverage is irresponsible and unprofessional."

Sonntag, September 21, 2008

620 Errors while testing with Google Geocoding

If you are using Googles Geocoding Api you might experience occasional problems with 620 errors, especially while testing. The reason for this is Google prohibits too many calls in a short period. But for most testcases geocoding is not an issue so you should disable it for these.

Fortunatly in Ruby its easy to override existing methods to mock them. I use the geokit plugin and with it this could be done by the following method:

def disable_geocoding
GeoKit::Geocoders::Geocoder.instance_eval do
def geocode(a)
res = GeoKit::GeoLoc.new
res.lat = 13.423007
res.lng = 52.534194
res.success = true
res
end
end
end



Include this in your testhelper and call disable_geocoding in your setup method.

Montag, August 04, 2008

Annoying floating point conversion in Ruby

Try (4.85*100).to_i in irb and you will get:
484

This is weired and totaly unexpected for me. I understand that floating points are not accurate and conversions have their limits. Type "%.32f" % 4.85 and you see the real value of this float. The reason is that to_i does just a truncate.

If you want the expected result you have to use:
(4.85*100).round.

Freitag, Juli 18, 2008

Some GTD criticism

I believe in chaos. Not that I say I love it. We are all in it by nature. For me its not a fight against, its more a dealing with it. You can try to keep its entropy low but you always need creativity to deal with the unforseen. GTD on the other hand is very strict and makes many assumptions. For me its hard to put all my tasks into one big pot and to decide when to write this or that task down for instance. GTD is sometimes too inflexible for certain situations. Sometimes projects need more attention, and they have much shorter cycles than less imortant or long running tasks. They grow bigger and they split or subprojects emerge and so on.
I am sure I did not get everything Allen is saying us so I am misinterpreting one or the other point. But I mean, hey, this book is like a bible, and I dont like the idea you have to read the same book many times to get a simple thing.
Dont get me wrong, I am still using GTD, not too dogmatic, as its fits and I am sattisfied with it. Good tools like Omnifocus are a great help there. I am still experimenting with it but I think dealing with the habits is more important because they are the source when we did a failure. So for me GTD is just a small tool in a complex world ;-)

Samstag, Juni 28, 2008

Render a simple Javascript with Rails

Though its a simple task there is no well documented solution. Most I googled did not work. I just needed to write a large javascript array dynamicly. If you just do a render :text => js your resulting content type will be text/html which was a problem for the IE. But setting the mime_type like

render :text => js,:mime_type => Mime::Type.lookup("application/javascript")

does not do the trick. What you have to do is setting the content type in the headers variable:

headers["Content-Type"] = "text/javascript".

I got the best results when I also added a .js to the route so you can call it like a static javascriptfile. If you want to do more complicated things you may check out the minus_r plugin of Dan Webb:

Freitag, Juni 20, 2008

Fat model

Today I had the chance to enjoy a dinner with David Black and some other Ruby folks in Berlin. We were discussing about legacy Rails code. Many newbies tend to write fat controllers with many pages of codes and 30 something methods. One was arguing that this is also Java style. I am now thinking thats not prefered even in Java though it was engouraged in J2EE because Entity Beans had been such a crap. Its just functional programming style where you end when you dont understand object orientation.

Montag, Juni 16, 2008

state of the art javascript gui

That gui of the north280 guys is really awesome. you have got to check out their presentation tool. everything is so smooth, it doesnt look like a webgui. at least with my Safari. If you want to read about the background story and the developers tuaw has more. hopefully they publish their javascript libraray objective-j soon.

Samstag, Juni 14, 2008

Merb presentation

While I heard a lot of postive stuff about merb I had no time to play with it, but this presentation on confreaks is definitly worth seeing. Ezra Zygmuntowicz explains some of the advantages of merb. I like the idea of a hacker should understand his framework he is working with. Therefor no magic in the framework. The magic is for your code. What I found interesting as a Rails developer he also has some concrete critics at Rails, mainly at the Actionpack where he started refactoring some really old parts there.

Montag, April 28, 2008

Upgrade to rails2

Yeah, since last week palabea runs on rails2 on production. The actual refactoring was painless and even the production deploy had just minor bugs. There are tiny changes coming with the upgrade - like empty parameters are now blank and not nil - which fell through our test coverage. So a quick look over all your controllers is needed if you want to avoid surprises in production.

Donnerstag, April 03, 2008

Adding parameters to routes

Routes are definitly one of the black holes in rails development. Most of the time you are dealing with standard configurations. You have to learn them, but there are not too many options you have.
But last time I wanted something special: to add an parameter to a url if a special route is called. We have /videos and we have /videos?lecture=true. Both are videos, share the same table and model, but they are treated differently in the GUI. So I thought definining a new route /lectures would be nice. I should call the same methods as for /videos but with a special parameter added. There is no way to do it. But hey I thought thats Ruby. What about adding a block, or override a special method. Then I started digging into the code. ... Wow, thats hot stuff, where to start? Jamis Buck tried but I couldn't figure out where and how to place my hook.
Finally I resignated and added a before_filter to the controller which does a regexp check against the request_uri. This does also the job, but I added code to the controller which is part of the routes logic, so really I dont like the solution. But it works fine for now.

Freitag, März 14, 2008

External rails plugin resources (and how to get rid of them)

Since it seems state of the art to keep plugins as external resources I tried myself to install them via script/plugin -x URL. 
Maybe I am missing some points here, of course it has some advantages, but in reality this has some big flaws. I would like to keep control over my sourcecode and dont want to deploy on production suddenly realizing there is a new version of a plugin out there. 
These external biests are really hard to handle with svn, like merging. Or imagine you need to make changes to these resouces.
Sad but true I found no documentation how to remove these plugins. It cost me some effort to find out since I am no svn guru. But its pretty easy. Just call

svn propedit svn:externals vendor/plugins. 

If you get a message that no SVN_EDITOR is defined you have to do that:

export SVN_EDITOR=mate

Delete all the external resource entries (file should be blank afterwards) and commit the vendor/plugins directory. Maybe you need a clean checkout before to do this properly.