Text

I just stumbled upon a really interesting article which discusses the various name schemas used around the world. And although I thought I knew quite a bit about that topic, I never thought about the implications it has on web- and data design.

So if you are developing apps for people fromm various cultural backgrounds, do them and yourself the favor and read Personal names around the world @wrc and think about your forms and fields.

Now that I know all that stuff, I finally understand why I had to clean/modify quite a lot of data by hand in one of my recent projects.

Text

I just stumbled upon a little gem called “class_exec” while reading ruby’s Module docs. I don’t know about other developers preferences, but I really really hate eval(). It is fine in JavaScript for creating JSON objects, but apart from that I really do not like it. No matter if is a simple eval, instance_eval or class_eval. Writing code in strings and evaluating it afterwards just seems wrong. It is something which always bothered me.

As it seems kept on using it although there is a much more beautiful solution to the problem. Here is the description of the method:

Evaluates the given block in the context of the class/module. The method defined in the block will belong to the receiver.

So great! Welcome class_exec, good bye class_eval!

I don’t know why I did not find this earlier, but I guess because the web is full of blog posts and code samples which utilize class_eval. And nobody really updated the stuff after ruby 1.8.7 was released and the class_exec method introduced. I guess it is time to change that!

Text

Having an active community with lots of active developers and tons of blogs sharing code and setup might be considered great. And usually it is. But in case of cucumber I guess it was not always the best and therefore the removal of web_steps.rb in cucumber 1.1.0 is a huge and important step.

I have to admit, i never really liked cucumber! I t felt wrong and was annoying to maintain. So I sticked with rspec and integration testing was not really handled. But then I read the post The training wheels come off by Aslak Hellesøy and I finally understood why I did not like cucumber. I was writing code just not in ruby but some other language the web_steps file provided. 

Checking other blogs about the release and common errors/mistakes people make when using cucumber I just started to rewrite my existing cucumber features and adding new ones. And It is fun: Clear and easy scenarios, clear and maintainable step definitions. I like!

And here a few more links which might come in handy:

Cheatsheets for cucumber and capybara:

Directly access an object if it's present

rubyquicktips:

If you want to access an object only if it’s present, you can use Rails’ Object#presence.
The API docs on presence have a good explanation:

This is handy for any representation of objects where blank is the same as not present at all. For example, this simplifies a common check for HTTP…

Source: rubyquicktips

Text

I just spent quite some time on a “bug” in one of my migrations. Run after run, it failed to populate the database properly. Even after adding the obligatory

  MyModel.reset_column_information

But after checking the database carefully, I realized that some rows had the intended data, while others didn’t. Long story short: Don’t forget to reset the column information on all models which inherit from the base model. In my case it was something like:

  Account.reset_column_information
  Guest.reset_column_information
  User.reset_column_information

Text

I just finished coding my loop when it struck me and I searched for a solution which feels more ruby like.

What I was trying to achieve is executing a piece of code until it ran without an exception. This might sound stupid, but in some cases it makes perfect sense. In this specific case, I was fetching secure data from an external provider via SSL connection. Unfortunately, the SSL connection did not always open as expected. So I have to try till it works (or I give up).

Instead of using a loop, ruby provides us with keyword retry - What a wonderful little keyword:

begin
  # Put your code here which has to be executed
rescue YourExpectedException => ex
  retry
end

And if you wanna protect yourself from an endless loop, add something like retry unless i > 10.

Text

I must have missed the part of the changelog where it states that the errors.add_to_base call will be deprecated. Anyhow, I searched the web and almost every case refers to the new API using

errors[:base] = 'My error message'

or 

errors[:base] << 'My error message'

This might be fine for single language apps, but when you have to provide the interface in many languages and want to keep all your error messages in one language file, you would have to replace ‘My error message’ with a call like

errors[:base] << I18n.t('activerecord.errors.models.model_name.attributes.base.my_code')

Another method which I could not really find in any blog post out there is a simple

errors.add(:base, :my_code)

Far shorter and it gets automatically namespaced. So have fun ;)

Text

In order to properly capture all the relationships between actors and objects in one of my projects, I am currently evaluating the usage of a GraphDB and after some digging I decided to give neo4j a spin. It seems to be rather mature, stable and on the right track to push a proper REST API. Unfortunately, the API is still rather limited and cannot provide all the features I need. As I cannot switch my project to JRuby, I will try to come up with a neo4j server plugin which adds and exposes an API for querying an index with non-exact matches - in other words, which allows searching for data.

As I have never worked with neo4j before, have a limited JAVA skill set, and only found an outdated and limited API documentation, this project could become very interesting. Let’s see what happes ;)

Text

In case you are using a combination of Authlogic, Cucumber and the CookieSessionStore, be very careful with your domain configuration. As our project is working with various subdomains, we are using /etc/hosts with an .local extension on our dev machines. But be careful with the settings. In rails3 you are not supposed to set the session store in the environment config files, but in an initializer. If you are not careful and define a domain for cucumber, you might get into trouble, as it uses www.example.com internally. And all the cookies you are setting are invalid.

I should have thought about it earlier, but at least I found my mistake :(

Text

I would say that with Arel, rails is on the right track to get rid of the SQL statements. But there is still some work which needs to be done. Stuff like Model.where(:name => ‘My stuff’) is already working really smoothly. What I would love to see is an easier access to Arel’s operators such as eq and not_eq. It would be great if the where method would accept a block which provides access to the arel table. where{|t| t[:name].not_equal(‘my stuff’)}.

I guess I will look into this soon. But right now I have to finish the rails3 upgrade first :(