Who are we?

We are a group of enthousiastic people working for Nedap, a Dutch company with a single goal: solve problems.

Search
Monday
Sep172007

Solving PO translation conflicts

If your using gettext in an svn environment, you probably have had the following problem: You and your colleague add some code and some translations. Now you check in your work. What will happen is that the last one o the two who checked in has a major problem. He has a conflict in the po and mo, as they differ. But his mo and po are also completely wrong, as they are based on a now old version of your application. To make things worse, subversion does an extremely bad job in resolving this, as it tries to juggle the three versions.

Luckally I added something to the latest version of my gettext generators plugin: the svnmerge rake task. It works as follows:

 rake gettext:pomerge FOLDER=/po/nl-NL/ APP=moves VER1=mine VER2=r318

What this does is create a new po and mo file so that they reflect you current mess. It then fills the new po translation with translations coming from the two conflicting versions (the mine and the r.318) in this example. It does this by using msgmerge in a smart way (so you need msgmerge for this).

After this is done, mark your po, pot and mo as resolved, run rake gettext:makemo and check your source in with a smile.

This was brought to you live from the lounge of Railsconf europe 2007 :).

Sunday
Jul292007

Sort, sort_by and group_by

I wanted to put the spotlight on some small but very useful ruby on rails functions: sort, sort_by and group_by.

Let's start with sort and sort_by. Its basic function should not come as a surprise: it sorts a list.

[1,3,2,4].sort => [1,2,3,4]

These functions really shine when you use objects inside the list.

a = { :key => 0, :value => 'a', :extra => 0 }
b = { :key => 1, :value => 'b', :extra => 0 }
c = { :key => 2, :value => 'c', :extra => 1 }
d = { :key => 3, :value => 'd', :extra => 1 }

[a,c,d,b].sort_by{ |i| i[:key] } => [a,b,c,d]

The previous example already contains the correct objects to demonstrate the use of group_by so we just give that example now.

[a,b,c,d].group_by{ |i| i[:extra] } => [ 0=>[a,b], 1=>[c,d] ]

As you can see this gives us the power of the SQL group_by statements right inside ruby. Sure, this cannot beat the speed of the SQL equivalent. But sometimes when speed is not really an issue or when you just can't query the right results, group_by can be a real life-saver.

As a bonus, when you want to group_by or sort_by functions on objects, you can use this pretty shorthand:

[a,b,c,d].group_by(&:length) => [ 3 => [a,b,c,d] ]

If you are wondering why the above works, it is just basic ruby. Any function that is defined to receive a &block can be called by either giving it a block of code in parenthesis or by passing it a proc object. The &:length is just shorthand for &proc{ |i| i.length }. Too bad you can't pass it arguments using the shorthand, then you'd be able to write:

[a,b,c,d].group_by(&:fetch, :key)

Hope you find this useful, if anyone knows if the above code is indeed possible in another form, please comment!

Update: I recently found out you can also use arrays in combination with these functions. So you can sort by multiple values. If you pass array [a,b] to a sort function it will sort by a and then sort by b. Very handy!

Tuesday
May082007

Ruby + SOAP + Multiple servers

The tutorials make it sound so easy. Plug and play even. Unfortunately reality (as always) bites hard.

SOAP integration is a piece of cake if you have one server and no authentication, but when you do the trouble starts. Although the problem is easy to solve once you know how, the lack of documentation on the subject is annoying.

Ruby's current SOAP implementation supports basic http authorisation during communication, however it does not support getting the WSDL file trough the same authorisation... (Argh! 1 ) So you have to pre-fetch the WSDL file and store it as a file, since Ruby does not support reading the WSDL file from a string (Argh! 2).

Since we have multiple servers running the same application on different ports the cached WSDL file is never correct...(Argh! 3) We solve this by overriding the WSDL's proxy setting.

The final code is short but it took me a while to find all this stuff out so I thought i'd post it here:

require 'soap/wsdlDriver'

# Connection settings
url   = "http://testingserver:3000/webservice"
usr  = "***"
pw  = "***"

wsdl_file = "wsdl/cached_file.wsdl"

factory = SOAP::WSDLDriverFactory.new(wsdl_file).create_rpc_driver
factory.options["protocol.http.basic_auth"] << [ url, usr, pw ]
factory.options["protocol.http.ssl_config.verify_mode"] = nil
factory.proxy.endpoint_url = url
Tuesday
May012007

InnoDB Database migration

I was looking for a migration that could convert my MyISAM tables to InnoDB, as I had mistakenly created some MyISAM tables. This sounds simple, but I wasn't able to find anything that did this. This simple migration I created, does the trick nicely.

class InnodbEngine < ActiveRecord::Migration
def self.up
  tables= [ 'addresses',
           'assets',
           'etcetera']

  for table in tables
    begin
      table_info = select_one "SHOW TABLE STATUS LIKE '" + table + "'"
      if (table_info['Engine'] != 'InnoDB')    
        execute 'ALTER TABLE `' + table + '` ENGINE=InnoDB'
      else
        puts table + ' allready InnoDB, skipping for migration'
      end
    rescue
      puts 'Skipping table ' + table
      puts 'Table not found or InnoDB not supported.'
    end
   end
end

def self.down
 # Not needed
end

end
Page 1 ... 1 2 3 4