<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>geekQ tech blog</title>
 <link href="http://blog.geekq.net/atom.xml" rel="self"/>
 <link href="http://blog.geekq.net/"/>
 <updated>2010-08-11T16:10:15+02:00</updated>
 <id>http://blog.geekq.net/</id>
 <author>
   <name>Vladimir Dobriakov</name>
   <!-- email></email -->
 </author>

 
 <entry>
   <title>Add column safely in MySQL and other idempotent db migrations</title>
   <link href="http://blog.geekq.net/2010/08/11/add-column-safely-mysql"/>
   <updated>2010-08-11T00:00:00+02:00</updated>
   <id>http://blog.geekq.net/2010/08/11/add-column-safely-mysql</id>
   <content type="html">&lt;h1 id='add_column_safely_in_mysql_and_other_idempotent_db_migrations'&gt;Add column safely in MySQL and other idempotent db migrations&lt;/h1&gt;

&lt;p&gt;With all the NoSQL hype nowadays this post regarding MySQL probably appears a bit archaic. But this is what kept me busy last two days.&lt;/p&gt;

&lt;p&gt;In our current Rails project we agreed with admins to use DDL and SQL scripts to implement the db structure- (and sometimes db content-) adjustments.&lt;/p&gt;

&lt;h2 id='context'&gt;Context&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;as little downtime as possible and sometimes even less is required&lt;/li&gt;

&lt;li&gt;if off time is approved, then such upgrade is done in the night&lt;/li&gt;

&lt;li&gt;developers have no access to production system&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id='advantages_of_sql'&gt;Advantages of SQL&lt;/h2&gt;

&lt;p&gt;Using hand crafted DDL and SQL scripts gives following advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;our admins have better feeling while being able to run single statements from the migration script and recover in trivial cases&lt;/li&gt;

&lt;li&gt;when data changes, e.g. normalization/denormalization needed, the SQL statements are much faster than Rails code involving creation of huge amounts of Ruby objects&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id='mysql_solution'&gt;MySQL solution&lt;/h2&gt;

&lt;p&gt;I also wanted to make the scripts idempotent so in case there were some problems and the structure/data conversion run only partially it would be possible to do some changes and rerun the whole script. It should be easy, I thought, just prepend every change statement with an if. With Oracle or Microsoft SQL Server it is just a matter of seconds.&lt;/p&gt;

&lt;p&gt;In fact, there is an &lt;code&gt;IF NOT EXISTS&lt;/code&gt; clause for &lt;a href='http://dev.mysql.com/doc/refman/5.0/en/create-table.html'&gt;&lt;code&gt;CREATE TABLE&lt;/code&gt;&lt;/a&gt; in MySQL. But unfortunately there is no one for &lt;a href='http://dev.mysql.com/doc/refman/5.0/en/alter-table.html'&gt;&lt;code&gt;ALTER TABLE ADD COLUMN&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Searching the internet does not help in case of MySQL. The official documentation is insufficient and the signal to noise ratio in MySQL relatet forums is lower, than for every other technology I&amp;#8217;ve ever seen. Even stackoverflow contains &lt;a href='http://stackoverflow.com/questions/972922/add-column-to-mysql-table-if-it-does-not-exist'&gt;unprecise or unhelpful&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It took me some time to explore all the ways and byways of MySQL programming. The problems I had to deal with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;IF&lt;/code&gt; statements only work in stored procedures, not when run directly, e.g. in mysql client&lt;/li&gt;

&lt;li&gt;more elegant and concise &lt;code&gt;SHOW COLUMNS&lt;/code&gt; does not work in stored procedure&lt;/li&gt;

&lt;li&gt;the syntax for delimiting statements is strange in MySQL, so you have to redefine the delimiter to be able to create stored procedures. Do not forget to switch the delimiter back!&lt;/li&gt;

&lt;li&gt;INFORMATIONAL_SCHEMA is global for all databases, do not forget to filter on &lt;code&gt;TABLE_SCHEMA=DATABASE()&lt;/code&gt;. &lt;code&gt;DATABASE()&lt;/code&gt; returns the name of the currently selected database.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is the working solution (tried out on MySQL 5.0 for Solaris):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;DELIMITER $$

DROP PROCEDURE IF EXISTS upgrade_database_1_0_to_2_0 $$
CREATE PROCEDURE upgrade_database_1_0_to_2_0()
BEGIN

-- rename a table safely
IF NOT EXISTS( (SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE()
        AND TABLE_NAME=&amp;#39;my_old_table_name&amp;#39;) ) THEN
    RENAME TABLE 
        my_old_table_name TO my_new_table_name,
END IF;
 
-- add a column safely
IF NOT EXISTS( (SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE()
        AND COLUMN_NAME=&amp;#39;my_additional_column&amp;#39; AND TABLE_NAME=&amp;#39;my_table_name&amp;#39;) ) THEN
    ALTER TABLE my_table_name ADD my_additional_column varchar(2048) NOT NULL DEFAULT &amp;#39;&amp;#39;;
END IF;

END $$

CALL upgrade_database_1_0_to_2_0() $$

DELIMITER ;&lt;/code&gt;&lt;/pre&gt;</content>
 </entry>
 
 <entry>
   <title>workflow 0.4 gem released</title>
   <link href="http://blog.geekq.net/2010/06/27/workflow-0.4.0-gem-released"/>
   <updated>2010-06-27T00:00:00+02:00</updated>
   <id>http://blog.geekq.net/2010/06/27/workflow-0.4.0-gem-released</id>
   <content type="html">&lt;h1 id='workflow_04_gem_released'&gt;workflow 0.4 gem released&lt;/h1&gt;

&lt;p&gt;Changes in this release:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;completely rewritten the documentation to match my branch&lt;/li&gt;

&lt;li&gt;switch to &lt;a href='http://github.com/technicalpickles/jeweler'&gt;jeweler&lt;/a&gt; for building gems&lt;/li&gt;

&lt;li&gt;use &lt;a href='http://gemcutter.org/gems/workflow'&gt;gemcutter&lt;/a&gt; for gem distribution&lt;/li&gt;

&lt;li&gt;every described feature is backed up by an automated test&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The documentation now lives on my &lt;a href='http://www.geekq.net/workflow/'&gt;personal site&lt;/a&gt;. Your own copy will be generated locally on &lt;code&gt;gem install workflow&lt;/code&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Vim as External Editor for Thunderbird 3 under Ubuntu</title>
   <link href="http://blog.geekq.net/2010/05/13/vim-external-editor-thunderbird-3-ubuntu"/>
   <updated>2010-05-13T00:00:00+02:00</updated>
   <id>http://blog.geekq.net/2010/05/13/vim-external-editor-thunderbird-3-ubuntu</id>
   <content type="html">&lt;h1 id='vim_as_external_editor_for_thunderbird_3_under_ubuntu'&gt;Vim as External Editor for Thunderbird 3 under Ubuntu&lt;/h1&gt;

&lt;p&gt;Recently I upgraded to Ubuntu 10.04 Lucid Lynx which includes the newest Thunderbird 3.0 email application. This version is really nice: offers tabbed interface for reading emails (unfortunately composing a new email &lt;a href='https://bugzilla.mozilla.org/show_bug.cgi?id=449299'&gt;does not work in a tab&lt;/a&gt;) and a very powerful search.&lt;/p&gt;

&lt;p&gt;But as always with the bleeding edge version, all add-ons stopped to work. Because I had to reconfigure everything anyway and find new ways for doing usual things like signatures wanted, at this occasion I decided to finally set up the &lt;a href='http://www.vim.org'&gt;vim&lt;/a&gt; as external editor for new messages.&lt;/p&gt;

&lt;p&gt;There a lot of howtos out there, none of them worked for me though and it took me some time to figure out, how to get the stuff working. So here is my documentation&amp;#8230;&lt;/p&gt;

&lt;p&gt;Assumptions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ubuntu 10.04 Lucid Lynx&lt;/li&gt;

&lt;li&gt;hence Thunderbird 3.0&lt;/li&gt;

&lt;li&gt;and gnome-terminal as the terminal of choice&lt;/li&gt;

&lt;li&gt;vim 7.0, not gvim&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Solution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;install the External Editor 0.8.0 Add-on from &lt;a href='http://globs.org/download.php'&gt;authors site&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;in Thunderbird menu Tools &amp;gt; Add-Ons &amp;gt; Preferences for &amp;#8220;External Editor&amp;#8221; set Text Editor to &lt;code&gt;gnome-terminal --geometry=110x40
--disable-factory -x vim&lt;/code&gt;&lt;/li&gt;

&lt;li&gt;create a new message with Ctrl+N&lt;/li&gt;

&lt;li&gt;press Ctrl+E to edit the message in vim&lt;/li&gt;

&lt;li&gt;&lt;code&gt;:wq&lt;/code&gt; to save the message, then the Thunderbird GUI is automatically updated with the new content&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Parameter explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I use &lt;code&gt;--geometry&lt;/code&gt; to make the editing window a bit bigger than default&lt;/li&gt;

&lt;li&gt;&lt;code&gt;--disable-factory&lt;/code&gt; ensures, that the call does not return immediately but waits until you have edited and saved the message&lt;/li&gt;

&lt;li&gt;&lt;code&gt;-x vim&lt;/code&gt; and not &lt;code&gt;-e vim&lt;/code&gt; is also important because the Add-on seems to simply append the parameters to the end of the line and to get the temporary file name properly propagated to vim it is crucial that all the remaining command line is interpreted as a single shell command&lt;/li&gt;
&lt;/ul&gt;
&lt;img src='/images/2010/thunderbird-vim/thunderbird-new-message.png' alt='Back in Thunderbird 3.0' style='border: 1px solid grey' /&gt;
&lt;h2 id='signatures'&gt;Signatures&lt;/h2&gt;

&lt;p&gt;Using a powerful and comfortable editor solves lot of other problems - for signatures, for example, I can now use vim abbreviations. Just put following in the .vimrc file&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;iab brr
  \&amp;lt;CR&amp;gt;Best Regards,
  \&amp;lt;CR&amp;gt;
  \&amp;lt;CR&amp;gt;Vladimir&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now I can type &lt;code&gt;brr&amp;lt;Enter&amp;gt;&lt;/code&gt; to get the complete signature. You can also easily create a macro which extracts the first name of the recipient and puts a greeting together.&lt;/p&gt;
&lt;img src='/images/2010/thunderbird-vim/edit-email-message-in-vim.png' alt='Edit an email message in vim' style='border: 1px solid grey' /&gt;
&lt;h2 id='fix_keyboard_shortcuts'&gt;Fix keyboard shortcuts&lt;/h2&gt;

&lt;p&gt;One of annoyances of Thunderbird 3.0 is that the keyboard shortcuts changed under linux. &amp;#8220;J&amp;#8221; and &amp;#8220;K&amp;#8221; keys were traditionally used to go to the next or previous message in the email message overview (like in vim). And now &amp;#8220;J&amp;#8221; means &amp;#8220;Mark as junk and delete&amp;#8221;!? WTF!? It took me three deleted messages till I got, what happened. You can reconfigure shortcuts with the &lt;a href='http://mozilla.dorando.at/keyconfig.xpi'&gt;keyconfig addon&lt;/a&gt; though.&lt;/p&gt;

&lt;h2 id='just_write_an_email'&gt;Just write an email&lt;/h2&gt;

&lt;p&gt;Sometimes while in the middle of doing something I remember that I have to tell X to person Y. So just switch to the email application to write new message. Oh, there are new emails in the inbox! So just skim over the subjects! Oh, this one looks interesting/important, lets check what is inside&amp;#8230; and the context is lost.&lt;/p&gt;

&lt;p&gt;Better solution is to bind &lt;code&gt;thunderbird -compose&lt;/code&gt; to a global keyboard shortcut. This opens a window just for composing an email without the distracting main email overview window.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Fix pragprog ebook fonts for source code listings</title>
   <link href="http://blog.geekq.net/2010/02/10/pragmatic-fix-ebook-monospaced-fonts"/>
   <updated>2010-02-10T00:00:00+01:00</updated>
   <id>http://blog.geekq.net/2010/02/10/pragmatic-fix-ebook-monospaced-fonts</id>
   <content type="html">&lt;h1 id='fix_pragprog_ebook_fonts_for_source_code_listings'&gt;Fix pragprog ebook fonts for source code listings&lt;/h1&gt;

&lt;p&gt;&lt;a href='http://pragprog.com/frequently-asked-questions/ebooks'&gt;Pragmatic programmers&lt;/a&gt; are pioneers of the ebook technology and have offered their books in both PDF and DRM free EPUB format for a long time.&lt;/p&gt;

&lt;p&gt;Now I can carry all my ebooks with me on my &lt;a href='http://en.wikipedia.org/wiki/Sony_Reader#PRS-505'&gt;Sony PRS-505&lt;/a&gt; ebook reader and read whatever I like while on commute. The eInk display is easy on eyes. Using an iPhone or iPad with their high glare displays for reading books is a joke compared to eInk!&lt;/p&gt;

&lt;p&gt;There is one shortcoming regarding books containing lots of source code though. Pragprogs use the default monospaced font. And the default monospaced font on Sony is unfortunately the owful Courier New - very wide and very ugly. So people at Sony are as &lt;a href='http://daringfireball.net/2007/07/iphone_fonts'&gt;clueless&lt;/a&gt; regarding typography as their counterparts at Apple. On PRS-505 some source code is cut off on the right side and what remains is very hard to read - so it is impossible to learn from this source code listings.&lt;/p&gt;

&lt;p&gt;&lt;img src='/images/2010/epub/courier-new-ugly.png' alt='Original version with Courier new' /&gt;&lt;/p&gt;

&lt;p&gt;It is easy to fix though.&lt;/p&gt;

&lt;p&gt;An EPUB file is simply a zip archive with&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the book text in html format&lt;/li&gt;

&lt;li&gt;some additional meta data like table of contents in XML files&lt;/li&gt;

&lt;li&gt;CSS&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;all standardized by &lt;a href='http://www.openebook.org/'&gt;International Digital Publishing Forum&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So we are going to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;unpack the EPUB archive&lt;/li&gt;

&lt;li&gt;adjust the style sheet&lt;/li&gt;

&lt;li&gt;pack the archive again&lt;/li&gt;

&lt;li&gt;and transfer the newly created file to the reader&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In addition we are going to install nicer additional fonts on the reader. Inconsolata and DejaVuSans are free fonts and perfect for displaying source code listings. The condensed version of DejaVuSans allows for even more characters per line.&lt;/p&gt;

&lt;p&gt;Just grab my &lt;a href='http://gist.github.com/300756#file_rakefile'&gt;Rakefile and stylesheet on github&lt;/a&gt;, adjust as needed and enjoy!&lt;/p&gt;

&lt;p&gt;Here is the result - very nice source code listing, isn&amp;#8217;t it?&lt;/p&gt;

&lt;p&gt;&lt;img src='/images/2010/epub/inconsolata-example.png' alt='Improved page using inconsolata font' /&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>HTML partials in Sinatra</title>
   <link href="http://blog.geekq.net/2009/12/22/html-partials-in-sinatra"/>
   <updated>2009-12-22T00:00:00+01:00</updated>
   <id>http://blog.geekq.net/2009/12/22/html-partials-in-sinatra</id>
   <content type="html">&lt;h1 id='html_partials_in_sinatra'&gt;HTML partials in Sinatra&lt;/h1&gt;

&lt;p&gt;I was looking for a way to generate an HTML partial (e.g. for web site navigation) within a Sinatra application. Sinatra itself does not have any opinion regarding partials. There are some add-ons out there e.g. &lt;a href='http://github.com/nesquena/sinatra_more'&gt;sinatra_more&lt;/a&gt; aiming for Rails-like helpers &lt;code&gt;content_tag&lt;/code&gt;, &lt;code&gt;input_tag&lt;/code&gt; etc.&lt;/p&gt;

&lt;p&gt;But it seems more natural to me to directly use an HTML/XML library. If I am referencing &lt;a href='http://wiki.github.com/tenderlove/nokogiri/generate'&gt;Nokogiri&lt;/a&gt; anyway (Nokogiri is a popular ruby wrapper around the libxml XML library), why not just use that?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;builder ||= Nokogiri::HTML::Builder.new do |doc|
  doc.ul {
    doc.li &amp;#39;hello&amp;#39;
    doc.li &amp;#39;admin&amp;#39;, :class =&amp;gt; &amp;#39;current&amp;#39; if current_user.is_admin?
  }
end
puts builder.doc.inner_html&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;produces&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt;hello&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;current&amp;quot;&amp;gt;admin&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We do not need any plug-ins and add-ons, it looks easy, does not it? Here is a more elaborated example:&lt;/p&gt;
&lt;script src='http://gist.github.com/261862.js?file=sinatra-nokogiri-menu.rb'&gt;
[Download source code][gist]
&lt;/script&gt;
&lt;p&gt;I&amp;#8217;ve also done some performance checking:&lt;/p&gt;
&lt;script src='http://gist.github.com/261862.js?file=is_fast_enough.rb'&gt;
[Download source code][gist_perf]
&lt;/script&gt;
&lt;p&gt;Results:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;                  total for 1000 loops, in milliseconds
Nokogiri, singleton builder     0.090000 
Nokogiri, recreate builder      0.270000 
HAML, singleton engine          0.170000 
HAML, recreate engine           1.220000 &lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;So HAML is significantly slower than xmllib, who could imagine that? And caching (evil) can make things faster, and certainly more messy. So I have to think about a helper for helpers that would help you organize your helpers and hide the caching and Nokogiri builder instantiation from you, without hiding too much and introducing leaky abstractions. Stay tuned!&lt;/strong&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Minimalist testing for Ruby applications</title>
   <link href="http://blog.geekq.net/2009/11/25/minimalist-testing-ruby"/>
   <updated>2009-11-25T00:00:00+01:00</updated>
   <id>http://blog.geekq.net/2009/11/25/minimalist-testing-ruby</id>
   <content type="html">&lt;h1 id='minimalist_testing_for_ruby_applications'&gt;Minimalist testing for Ruby applications&lt;/h1&gt;

&lt;p&gt;Last four months I have been developing web and backend applications for fun and profit without Rails - using the light weight &lt;a href='http://www.sinatrarb.com/'&gt;Sinatra&lt;/a&gt; framework. Besides technical solutions there is also an important philosophical attitude I have learned in that Sinatra community.&lt;/p&gt;

&lt;p&gt;In opposite to Rails they completely decline the &lt;a href='http://en.wikipedia.org/wiki/Cargo_cult'&gt;cargo cult&lt;/a&gt; (also &lt;a href='http://www.therailsway.com/2007/8/1/dangers-of-cargo-culting'&gt;mentioned by Koz&lt;/a&gt; in a slightly different context) where one writes a line or two, he has seen in the README of the plugin and expects something magical to happen. And often it does not happen or happens in an unexpected way.&lt;/p&gt;

&lt;p&gt;By contrast, in Sinatra community, if one develops a chunk of functionality, he thinks can be helpful for others, instead of creating a plugin or gem one publishes a gist at github so others can copy, paste and use it as inspiration for implementing the 5 percent of functionality they really need.&lt;/p&gt;

&lt;p&gt;I found out that for me the RSpec approach does not work. The tests should give me more confidence, that everything works, especially for refactoring. Through all the magic and extensive metaprogramming RSpec creates more uncertainty for me instead.&lt;/p&gt;

&lt;p&gt;Test::Unit is on the other side a bit &amp;#8220;underkill&amp;#8221; but it is easy to extended it with features I am missing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;use full sentences to name the tests&lt;/li&gt;

&lt;li&gt;declare tests as pending or non-critical&lt;/li&gt;

&lt;li&gt;enable the same level of abstraction in the test implementation and provide an overview of a test&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If folded in the editor, a test looks like following, providing an overview of what is going on in the test:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;-class SignupTest &amp;lt; FunctionalTestCase 

-  test &amp;#39;registration workflow&amp;#39; do
+    prepare &amp;#39;a customer with wrong bank details&amp;#39; do
+    expect &amp;#39;errors only after the offline check&amp;#39; do
+    expect &amp;#39;update should clean errors and bank details check identifier&amp;#39; do
   end
 end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This single test steps can be also printed out during the test execution if you set the environment variable &lt;code&gt;VERBOSE_TEST=true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For comparision here is the full source code of the integration test:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class SignupTest &amp;lt; FunctionalTestCase 

  test &amp;#39;registration workflow&amp;#39; do
    prepare &amp;#39;a customer with wrong bank details&amp;#39; do
      put &amp;#39;/customers/inva&amp;#39;, create_customer_xml(
        &amp;#39;account&amp;#39; =&amp;gt; &amp;#39;123456&amp;#39;,
        &amp;#39;bank-number&amp;#39; =&amp;gt; &amp;#39;36010043&amp;#39;
      ).to_xml, {&amp;#39;HTTP_X_ON_BEHALF_OF&amp;#39; =&amp;gt; &amp;#39;functional test&amp;#39;}
      assert_equal 202, last_response.status
    end

    expect &amp;#39;errors only after the offline check&amp;#39; do
      assert_equal 0, Customer.find_by_customer_id(&amp;#39;inva&amp;#39;).customer_errors.size
      
      Customer.check_and_promote_all

      a = Customer.find_by_customer_id(&amp;#39;inva&amp;#39;)
      assert_equal 1, a.customer_errors.size
      assert_match /invalid/, a.customer_errors.first.message
    end
    
    expect &amp;#39;update should clean errors and bank details check identifier&amp;#39; do
      get &amp;#39;/customer/inva&amp;#39;, {}, {&amp;#39;HTTP_X_ON_BEHALF_OF&amp;#39; =&amp;gt; &amp;#39;functional test&amp;#39;}

      new_doc = response_doc()
      new_doc.select_single_node(&amp;#39;//bank-name&amp;#39;).set_text(&amp;#39;Other Bank&amp;#39;)
      put &amp;#39;/customer/inva&amp;#39;, 
        new_doc.to_xml, 
        {&amp;#39;HTTP_X_ON_BEHALF_OF&amp;#39; =&amp;gt; &amp;#39;functional test&amp;#39;}
      a = customer.find_by_customer_id(&amp;#39;inva&amp;#39;)
      assert_equal 202, last_response.status
      assert_equal 0, a.customer_errors.size
      assert a.schufa_request_identifier.blank?, &amp;#39;request identifier is not cleaned.&amp;#39;
    end

  end
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So my testing infrastructure mainly consists of&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the excellent &lt;a href='http://www.brynary.com/2009/3/5/rack-test-released-a-simple-testing-api-for-rack-based-frameworks-and-apps'&gt;Rack::Test&lt;/a&gt; facility, is also standard for new Rails versions&lt;/li&gt;

&lt;li&gt;&lt;strong&gt;my Test::Unit extension, &lt;a href='http://gist.github.com/241963'&gt;grab it&lt;/a&gt; on github&lt;/strong&gt;&lt;/li&gt;

&lt;li&gt;my project-specific test_helper, which can programatically create entities filled with test data and contains project-specific assertion helpers&lt;/li&gt;
&lt;/ul&gt;</content>
 </entry>
 
 <entry>
   <title>Deploy JRuby web application with jetty-rackup</title>
   <link href="http://blog.geekq.net/2009/09/18/jetty-rack-up"/>
   <updated>2009-09-18T00:00:00+02:00</updated>
   <id>http://blog.geekq.net/2009/09/18/jetty-rack-up</id>
   <content type="html">&lt;h1 id='deploy_jruby_web_application_with_jettyrackup'&gt;Deploy JRuby web application with jetty-rackup&lt;/h1&gt;
&lt;img class='floatright' src='/images/2009/powered_by_jetty.gif' alt='Powered by Jetty' /&gt;
&lt;p&gt;I already &lt;a href='/2009/09/17/switching-to-jruby/'&gt;reported&lt;/a&gt; about our problems with different Ruby libraries. Bad library support is the main cause, why in our current project we decided to switch from MRI to JRuby for all the new application development.&lt;/p&gt;

&lt;p&gt;The tradition in the JRuby world is to do it the same way it is done in Java:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create a lengthy configuration in WEB-INF/web.xml&lt;/li&gt;

&lt;li&gt;package everything to jars and wars&lt;/li&gt;

&lt;li&gt;throw it in a servlet container - tomcat, jetty or glassfish&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;and all the howtos and tutorials assume that way.&lt;/p&gt;

&lt;p&gt;The Ruby way, in my opinion, is to use &lt;a href='http://rack.rubyforge.org/doc/'&gt;Rack&lt;/a&gt; and a &lt;a href='http://wiki.github.com/rack/rack/tutorial-rackup-howto'&gt;rackup&lt;/a&gt; script. So I wanted to &lt;strong&gt;&amp;#8220;invert the control&amp;#8221; and embed a Java web server inside my Ruby script&lt;/strong&gt;. That way is also supported by the Java&amp;#8217;s light weight web server &lt;a href='http://jetty.mortbay.com/jetty/'&gt;jetty&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So I ported rackup and adapted it for the usage with jetty. You can &lt;a href='http://github.com/geekq/jetty-rackup'&gt;install it from github&lt;/a&gt;. Now you can take your existing, say &lt;a href='http://www.sinatrarb.com/'&gt;Sinatra&lt;/a&gt;, application and reuse your existing config.ru&lt;/p&gt;

&lt;p&gt;Enjoy!&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Switching to JRuby</title>
   <link href="http://blog.geekq.net/2009/09/17/switching-to-jruby"/>
   <updated>2009-09-17T00:00:00+02:00</updated>
   <id>http://blog.geekq.net/2009/09/17/switching-to-jruby</id>
   <content type="html">&lt;h1 id='switching_to_jruby'&gt;Switching to JRuby&lt;/h1&gt;

&lt;p&gt;Currently I am working on an &amp;#8220;enterprise-y&amp;#8221; integration solution, where we offer a RESTful interface making heavy usage of HTTP and XML on the implementation side. While suitable for HelloWorld and blogging applications, all the standard Ruby libraries simply do not work in the corner cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;XML builder generates illegal XML&lt;/li&gt;

&lt;li&gt;REXML - occasionally the stack level is too deep :O&lt;/li&gt;

&lt;li&gt;Net::HTTP can not proper handle HTTP 1.1 and &amp;#8220;100 Continue&amp;#8221;&lt;/li&gt;

&lt;li&gt;hpricot - tag soup parsing and creation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While being thankful to all the great hackers, who developed the mentioned libraries, partly in their personal spare time, it is simply too much pain to use them in the &lt;a href='http://www.rubyrailways.com/dhh-fuck-the-real-world/'&gt;real world&lt;/a&gt; application, that has to 100% work.&lt;/p&gt;

&lt;p&gt;And at this point of time we would also like to get rid of ImageMagick.&lt;/p&gt;

&lt;p&gt;The JRuby is pretty mature nowadays and promises to eliminate debugging in mentioned areas thanks to java libraries, that evidently work. For years.&lt;/p&gt;

&lt;p&gt;That is why we decided to switch to JRuby for all the new application development. For automation tasks (organizing my life with Rake) the MRI still remains the platform of choice, especially due to its incredibly short start up time - 12ms on my machine.&lt;/p&gt;

&lt;p&gt;P.S. Just for completeness: you could use libxml and libcurl (known 100% working solutions written in C) with their respective Ruby wrappers, but&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I found the quality of libcurl wrappers not convincing&lt;/li&gt;

&lt;li&gt;searching for cross-platform compiling issues (production servers are Sun Solaris based) does not belong to our hobbies&lt;/li&gt;

&lt;li&gt;we wanted to solve all our problems at once. ;-) You know, the silver bullet.&lt;/li&gt;
&lt;/ul&gt;</content>
 </entry>
 
 <entry>
   <title>workflow 0.3 gem released</title>
   <link href="http://blog.geekq.net/2009/08/13/workflow-0.3-gem-released"/>
   <updated>2009-08-13T00:00:00+02:00</updated>
   <id>http://blog.geekq.net/2009/08/13/workflow-0.3-gem-released</id>
   <content type="html">&lt;h1 id='workflow_03_gem_released'&gt;workflow 0.3 gem released&lt;/h1&gt;

&lt;h2 id='better_error_messages'&gt;Better error messages&lt;/h2&gt;

&lt;p&gt;Helpful error messages is one of the hardest thing to achieve. Thanks to the &lt;code&gt;workflow&lt;/code&gt; user Kyle Burton for the proposal regarding transitions to an illegal state! Now if you have not defined e.g. &lt;code&gt;solved&lt;/code&gt; event but try to transition to it, you will get more meaningful message like &lt;code&gt;Event[solve]&amp;#39;s transitions_to[solved] is not a declared state.&lt;/code&gt; and immediately know, what to fix.&lt;/p&gt;

&lt;h2 id='separation_of_concerns'&gt;Separation of concerns&lt;/h2&gt;

&lt;p&gt;Intermixing of transition graph definition (states, transitions) on the one side and implementation of the actions on the other side for a bigger state machine can introduce clutter.&lt;/p&gt;

&lt;p&gt;To reduce this clutter it is now possible to use state entry- and exit- hooks defined through a naming convention. For example, if there is a state :pending, then instead of using a block:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;state :pending do
  on_entry do
    # your implementation here
  end
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;you can hook in by defining method&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def on_pending_exit(new_state, event, *args)
  # your implementation here
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;anywhere in your class. You can also use a simpler function signature like &lt;code&gt;def on_pending_exit(*args)&lt;/code&gt; if your are not interested in arguments. Please note: &lt;code&gt;def on_pending_exit()&lt;/code&gt; with an empty list would not work.&lt;/p&gt;

&lt;p&gt;If both a function with a name according to naming convention and the on_entry/on_exit block are given, then only on_entry/on_exit block is used.&lt;/p&gt;

&lt;h2 id='have_fun'&gt;Have fun!&lt;/h2&gt;

&lt;p&gt;You can easily install the newest version with&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo gem install workflow&lt;/code&gt;&lt;/pre&gt;</content>
 </entry>
 
 <entry>
   <title>Editing a LISP program on Ubuntu with vim</title>
   <link href="http://blog.geekq.net/2009/07/13/ubuntu-vim-edit-lisp-scheme"/>
   <updated>2009-07-13T00:00:00+02:00</updated>
   <id>http://blog.geekq.net/2009/07/13/ubuntu-vim-edit-lisp-scheme</id>
   <content type="html">&lt;h1 id='editing_a_lisp_program_on_ubuntu_with_vim'&gt;Editing a LISP program on Ubuntu with vim&lt;/h1&gt;

&lt;p&gt;Trying to fill the gap in my computer science education I started to learn LISP again.&lt;/p&gt;

&lt;p&gt;I am using the &lt;a href='http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&amp;amp;tid=3305'&gt;SICP&lt;/a&gt; &amp;#8220;Structure and Interpretation of Computer Programs, 2nd Edition&amp;#8221; book from MIT, that is used to be the book for the first semester students (!) to learn programming.&lt;/p&gt;

&lt;p&gt;So I wanted to use the mit-scheme - a LISP interpreter written by the authors of the book. Unfortunately, there is no precompiled version for the 64 bit version of Ubuntu 8.10 Intrepid. But so far the scheme48 seems to be compatible to the examples from the book.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo aptitude install scheme48&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The command line of the scheme48 is not very convenient so I ended just copying and pasting snippets from the editor into the interpreter window.&lt;/p&gt;

&lt;p&gt;All the trolls say, the only way to edit and run LISP programs is to use emacs. There is a working automation solution for vim though - &lt;a href='http://vim.sourceforge.net/scripts/script.php?script_id=221'&gt;VIlisp.vim&lt;/a&gt;. It took me some time to get it work - here is my howto for Ubuntu 8.10 Intrepid:&lt;/p&gt;

&lt;h2 id='1_download_and_extract_the_vilispvim'&gt;1. Download and extract the &lt;a href='http://vim.sourceforge.net/scripts/script.php?script_id=221'&gt;VIlisp.vim&lt;/a&gt;&lt;/h2&gt;

&lt;pre&gt;&lt;code&gt;mkdir ~/.vim/lisp
tar -C ~/.vim/lisp --strip-components 1 -xf vilisp.2.3.tgz &lt;/code&gt;&lt;/pre&gt;

&lt;h2 id='2_install_the_required_perl_modules'&gt;2. Install the required Perl modules&lt;/h2&gt;

&lt;pre&gt;&lt;code&gt;sudo aptitude install libterm-readline-perl-perl \
  libterm-readline-gnu-perl libenv-ps1-perl&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In addition I had to install the Pty package through the Perl package-maangement CPAN&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo cpan
cpan&amp;gt; install IO::Pty
cpan&amp;gt; exit&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id='3_now_we_can_run_vilisp_which_connects_two_windows'&gt;3. Now we can run VIlisp which connects two windows&lt;/h2&gt;

&lt;h3 id='in_the_scheme_interpreter_window'&gt;In the scheme interpreter window:&lt;/h3&gt;

&lt;pre&gt;&lt;code&gt;export PERL_RL=gnu
perl funnel.pl $HOME/.lisp-pipe scheme48&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I&amp;#8217;ve created an alias for that&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;alias lisp=&amp;#39;PERL_RL=gnu perl $HOME/.vim/lisp/funnel.pl $HOME/.lisp-pipe scheme48&amp;#39;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;so I can just type &lt;code&gt;lisp&lt;/code&gt; instead.&lt;/p&gt;

&lt;h3 id='in_the_editor_window'&gt;In the editor window:&lt;/h3&gt;

&lt;pre&gt;&lt;code&gt;vim exercise1_3.scm&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In vim:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:so ~/.vim/lisp/VIlisp.vim&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;or put something like following to your &lt;code&gt;.vimrc&lt;/code&gt; file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;autocmd BufRead,BufNewFile *.lsp,*.lisp,*.scm so ~/.vim/lisp/VIlisp.vim &lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;so the functionality is loaded automatically for every scheme/lisp source file.&lt;/p&gt;

&lt;h2 id='4_now_you_can_use_shortcuts_described_in_the_readme_of_vilisp'&gt;4. Now you can use shortcuts described in the README of VIlisp&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;\ec&lt;/code&gt; to evaluate current s-expression&lt;/li&gt;

&lt;li&gt;&lt;code&gt;\eb&lt;/code&gt; sends current visual block to the interpreter&lt;/li&gt;
&lt;/ul&gt;</content>
 </entry>
 
 <entry>
   <title>RailsWayCon Berlin</title>
   <link href="http://blog.geekq.net/2009/05/27/railswaycon-berlin"/>
   <updated>2009-05-27T00:00:00+02:00</updated>
   <id>http://blog.geekq.net/2009/05/27/railswaycon-berlin</id>
   <content type="html">&lt;h1 id='railswaycon_berlin'&gt;RailsWayCon Berlin&lt;/h1&gt;

&lt;p&gt;The RailsWayCon in Berlin is over. All the talks I&amp;#8217;ve seen were extremely interesting. Here are my personal highlights:&lt;/p&gt;

&lt;p&gt;It seems that Ola Bini can create a new programming language effortlessly. On the examples of JRuby and &lt;a href='http://ioke.org/'&gt;Ioke&lt;/a&gt; he offered a very interesting look behind the scenes and showed the way from the AST (abstract syntax tree) via the runtime environment (frames, bindings) to the interpretation and compilation. He told that Ruby is especially hard to implement because of its very flexible syntax (little parentheses and semicolons, lot of whitespace) and other flexibility - eval, closures, the fact, that many functions accept an explicit binding object. The harder it is to implement Ruby, the better it is for developers, that use Ruby: they get a very powerful tool and can write very terse and at the same time very expressive source code.&lt;/p&gt;

&lt;p&gt;Neal Ford showed, that naming the things in a particular way immediately makes them sound more respectable. By introducing of terms &amp;#8220;cut points&amp;#8221;, &amp;#8220;advice&amp;#8221; instead of &amp;#8220;monkey patching&amp;#8221; everything sounds like advanced design patterns and not anymore as a hackery.&lt;/p&gt;

&lt;p&gt;I found the discussions with members of the Rails core team between the sessions and in the evening very helpful. In that environment even previously rejected &lt;a href='https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1459'&gt;patch&lt;/a&gt; can take a shortcut into the main repository.&lt;/p&gt;

&lt;p&gt;P.S. I gave a &lt;a href='http://it-republik.de/konferenzen/railswaycon/sessions/?tid=1229#session-2'&gt;talk&lt;/a&gt; on metaprogramming:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Metaprogramming considered harmful&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&amp;#8230;not all kinds of it, of course. People just discovered the power of this technique extensively use it, both in cases where it&amp;#8217;s a good match, but unfortunately also where it creates a lot of unexpected problems. With a selection of practical &amp;#8216;Metaprogramming gone wild&amp;#8217; examples I would like to show how less and more appropriate metaprogramming leads to simpler and better maintainable code.&lt;/p&gt;
&lt;/blockquote&gt;</content>
 </entry>
 
 <entry>
   <title>rubygems 1.3.3 released</title>
   <link href="http://blog.geekq.net/2009/05/25/rubygems-released"/>
   <updated>2009-05-25T00:00:00+02:00</updated>
   <id>http://blog.geekq.net/2009/05/25/rubygems-released</id>
   <content type="html">&lt;h1 id='rubygems_133_released'&gt;rubygems 1.3.3 released&lt;/h1&gt;

&lt;p&gt;Some time &lt;a href='http://www.innoq.com/blog/vd/2008/07/browsing_ruby_d.html'&gt;ago&lt;/a&gt; I modified the gem server to provide a convinient search for the documentation of locally installed gems.&lt;/p&gt;

&lt;p&gt;Now it is part of the &lt;a href='http://rubyforge.org/forum/forum.php?forum_id=32072'&gt;official&lt;/a&gt; rubygems 1.3.3 release. Just update your rubygems with&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo gem update --system&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;After you restart the &lt;code&gt;gem server&lt;/code&gt; you can see the new search box in the upper right corner of the page. You can type in a part of the gem name and after pressing the enter key you will be immediately redirected to the rdoc of the desired gem or presented with a list of matching gems.&lt;/p&gt;

&lt;p&gt;&lt;img src='/images/2009/rubygems/search-rdoc-html.png' alt='main page with the search box' /&gt;&lt;/p&gt;

&lt;p&gt;You can also define a custom search word in your browser (at least Firefox and Opera support this). I&amp;#8217;ve connected &lt;code&gt;shortcut:rdoc&lt;/code&gt; to &lt;code&gt;http://localhost:8808/rdoc?q=%s&lt;/code&gt;. Now I can type &amp;#8216;rdoc hp&amp;#8217; to jump directly to the documentation of the hpricot gem.&lt;/p&gt;

&lt;p&gt;&lt;img src='/images/2009/rubygems/search-rdoc.png' alt='address bar' /&gt;&lt;/p&gt;

&lt;p&gt;And if you use the &lt;a href='http://github.com/voloko/sdoc/tree/master'&gt;sdoc&lt;/a&gt; for generating your local Ruby documentation you can immediately continue with typing the name of then method. That way I typically need only about two seconds to get to the documentation of the desired method:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ctrl+T in Firefox for a new tab&lt;/li&gt;

&lt;li&gt;&amp;#8216;rdoc core&amp;#8217;, press ENTER&lt;/li&gt;

&lt;li&gt;&amp;#8216;insteval&amp;#8217;, press ENTER and I can read the documentation for the Object#instance_eval method.&lt;/li&gt;
&lt;/ul&gt;</content>
 </entry>
 
 <entry>
   <title>Düsseldorf on Rails with Gregg Pollack</title>
   <link href="http://blog.geekq.net/2009/04/23/duesseldorf-on-rails-with-gregg-pollack"/>
   <updated>2009-04-23T00:00:00+02:00</updated>
   <id>http://blog.geekq.net/2009/04/23/duesseldorf-on-rails-with-gregg-pollack</id>
   <content type="html">&lt;h1 id='dsseldorf_on_rails_with_gregg_pollack'&gt;Düsseldorf on Rails with Gregg Pollack&lt;/h1&gt;

&lt;p&gt;Yesterday we had Gregg Pollack from &lt;a href='http://railsenvy.com/'&gt;Rails Envy&lt;/a&gt; as a guest speaker at our small &lt;a href='http://groups.google.de/group/duesseldorf-on-rails/browse_thread/thread/55c59e36ace66e7b'&gt;local user group&lt;/a&gt; in &lt;a href='http://maps.google.com/maps?hl=en&amp;amp;q=duesseldorf&amp;amp;ie=UTF8&amp;amp;ll=51.231183,6.782684&amp;amp;spn=0.195848,0.236549&amp;amp;t=h&amp;amp;z=12'&gt;Düsseldorf&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;His Rails Envy webcasts are very popular. But to see and to hear his presentations live is even better!&lt;/p&gt;

&lt;p&gt;He talked about recent and upcoming changes in Rails and about other Rails 3 related stuff:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;modularization of Rails and how it should improve the competition between different O/R mappers, support for different JavaScript libraries etc.&lt;/li&gt;

&lt;li&gt;introducing Rack and Metal to Rails and scenarios, where performance can be significantly improved by introducing additional middleware like Rack::Cache&lt;/li&gt;

&lt;li&gt;reverse HTTP proxies and how it works (or does not work) in diffrent scenarios&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;He also talked about implementation details:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;how more consious definition of the Rails public API promises to decrease the pain caused by a typical switch to a newer minor version of Rails&lt;/li&gt;

&lt;li&gt;how less and less invasive meta programming and revival of object oriented techniques improves both the performance and the readability of Rails internal implementation (also my favorite topic)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All in once it was a great fun!&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Rails internationalization - removing validation message prefix</title>
   <link href="http://blog.geekq.net/2009/04/09/i18n-remove-validation-message-prefix"/>
   <updated>2009-04-09T00:00:00+02:00</updated>
   <id>http://blog.geekq.net/2009/04/09/i18n-remove-validation-message-prefix</id>
   <content type="html">&lt;h1 id='rails_internationalization__removing_validation_message_prefix'&gt;Rails internationalization - removing validation message prefix&lt;/h1&gt;

&lt;p&gt;Current ActiveRecord implementation always prepends an error message with the attribute name. This is because of design weakness in the handling of human messages in Rails.&lt;/p&gt;

&lt;p&gt;The data should be always interpolated into the message, string concatenation should be never used.&lt;/p&gt;

&lt;p&gt;Good:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;MY_MESSAGE = &amp;#39;%{attribute} is invalid&amp;#39;
return MY_MESSAGE % {:attribute =&amp;gt; &amp;#39;email&amp;#39;}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Bad:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;MY_MESSAGE = &amp;#39;is invalid&amp;#39;
return MY_MESSAGE + &amp;#39; &amp;#39; + &amp;#39;email&amp;#39;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&amp;#8230; because the correct order of the words in a sentence depends on language.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Generally it is not possible to create a message in human language by string concatenation. Only interpolation universally works!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Still bad (from validations.rb):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;attr_name + I18n.t(&amp;#39;activerecord.errors.format.separator&amp;#39;, 
  :default =&amp;gt; &amp;#39; &amp;#39;) + message&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I18n.t would not help, if you use &lt;code&gt;+&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Our custom message &amp;#8216;Gadget should have at least one screenshot.&amp;#8217; is corrupted by a prefix to &amp;#8216;Screenshots Gadget should have at least one screenshot.&amp;#8217;&lt;/p&gt;

&lt;p&gt;I hope one day it can be fixed in Rails by substantially refactoring the ActiveRecord validations and internationalization subsystem. For now I&amp;#8217;m fixing it with a dash of metaprogramming and monkey patching.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module ActiveRecord
  class Errors

    # allow a proc as a user defined message
    def add(attribute, message = nil, options = {})
      if options[:default].is_a?(Proc) # this is new
        message = options[:default].call(attribute)
        def message.full? do
          true
        end
      end
      message ||= :invalid
      message = generate_message(attribute, message, options) if 
        message.is_a?(Symbol)
      @errors[attribute.to_s] ||= []
      @errors[attribute.to_s] &amp;lt;&amp;lt; message
    end

    def full_messages(options = {})
      full_messages = []

      @errors.each_key do |attr|
        @errors[attr].each do |message|
          next unless message

          if attr == &amp;quot;base&amp;quot;
            full_messages &amp;lt;&amp;lt; message
          else
            attr_name = @base.class.human_attribute_name(attr)
            if message.respond_to? :full? and message.full? # new switch
              full_messages &amp;lt;&amp;lt; message
            else
              # messages for humans based on string concatenation are 
              # inherently broken - should not be used!
              full_messages &amp;lt;&amp;lt; attr_name + 
                I18n.t(&amp;#39;activerecord.errors.format.separator&amp;#39;, 
                  :default =&amp;gt; &amp;#39; &amp;#39;) + message
            end
          end
        end
      end
      full_messages
    end

  end
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;copy a paste the code into a initializer (underneath config/initializers folder)&lt;/li&gt;

&lt;li&gt;all translated messages created by a proc get the special &lt;code&gt;full?&lt;/code&gt; tag which is later used in full_messages to bypass the attr_name addition&lt;/li&gt;

&lt;li&gt;you can use attribute name in your proc to interpolate into the localized error message&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This dirty hack is only needed because of design weekness of rails. Some conceptial thoughts:&lt;/p&gt;

&lt;h3 id='short_and_full_error_messages'&gt;Short and full error messages&lt;/h3&gt;

&lt;p&gt;Distinction between short and full error message does not make sence&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;one can not be universarly infered from another for every human language&lt;/li&gt;

&lt;li&gt;specifing both manually hardly makes sense So there should be just error message with optional placeholder for the attribute name.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id='string_interpolation_everywhere'&gt;String interpolation everywhere&lt;/h3&gt;

&lt;p&gt;The data should be always interpolated into the error messages, never concatenated, because the order of the words in a sentence and gramatical rules differs from language to language.&lt;/p&gt;

&lt;h3 id='nonprogrammer_capable_translation_files'&gt;Non-programmer capable translation files&lt;/h3&gt;

&lt;p&gt;YAML files and ruby symbol driven translation keys are unsuitable for non-programmers and people without deep knowledge of the application to be translated.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;See &lt;a href='http://www.gnu.org/software/gettext/manual/gettext.html#Introduction'&gt;GNU gettext&lt;/a&gt; for a deeper explanation of internationalization concepts that work!&lt;/strong&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>ActiveRecord validations and internationalization</title>
   <link href="http://blog.geekq.net/2009/04/08/activerecord-i18n-validation-message"/>
   <updated>2009-04-08T00:00:00+02:00</updated>
   <id>http://blog.geekq.net/2009/04/08/activerecord-i18n-validation-message</id>
   <content type="html">&lt;h1 id='activerecord_validations_and_internationalization'&gt;ActiveRecord validations and internationalization&lt;/h1&gt;

&lt;p&gt;We recently switched to Rails 2.3 in our project. This version offers a new internationalisation system, which is a clear step forward, but its yaml based translation file storage and symbol driven message selection are unfortunately unsuitable for the real enterprise environment - handling translation files and separation of roles of developers and translators. The latter are driven by each country&amp;#8217;s very own marketing department.&lt;/p&gt;

&lt;p&gt;So we are going to continue using Gettext for localization. The old Masao&amp;#8217;s library we have been using before is not compatible with current Rails. So after some evaluation we desided to use &lt;a href='http://github.com/geekq/fast_gettext/tree'&gt;fast-gettext&lt;/a&gt; library as a foundation for our development, which was created by &lt;a href='http://github.com/grosser/fast_gettext/tree'&gt;Michael Grosser&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The Rails 2.3 release feels rough and the transition to the new internationalization system unfinished. You can see different generations of code in validations.rb that do not really fit together.&lt;/p&gt;

&lt;p&gt;There are still problems with custom validation messages. The build-in backend with symbol-based message keys is not an option for us or any other big-scale development environment (role separation etc., s. above). And ActiveRecord provides no other API for custom translated messages.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;validates_length_of :archives, :minimum =&amp;gt; 1,
  :message =&amp;gt; _(&amp;#39;Gadget requires at least one archive &amp;#39;) &lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;obviously does not work, as accurately &lt;a href='http://wiki.github.com/grosser/fast_gettext/activerecord'&gt;described&lt;/a&gt; by Michael.&lt;/p&gt;

&lt;p&gt;The underscore method is evaluated during the class loading. So instead of string we need a proc at this point, so it can account for locale, currently selected by user. We need a possibility to define a validation like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;validates_length_of :archives, :minimum =&amp;gt; 1,
  :message =&amp;gt; proc {_(&amp;#39;Gadget requires at least one archive &amp;#39;)}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I&amp;#8217;ve created a monkey patch for that, which you can put into a Rails initilizer, e.g. &lt;code&gt;config/initializers/gettext_hacks.rb&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module ActiveRecord
  class Errors

     # allow a proc as a user defined message
     def add(attribute, message = nil, options = {})
       message = options[:default].call if options[:default].is_a?(Proc)
       message ||= :invalid
       message = generate_message(attribute, message, options) if message.is_a?(Symbol)
       @errors[attribute.to_s] ||= []
       @errors[attribute.to_s] &amp;lt;&amp;lt; message
     end

  end
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now I&amp;#8217;m working on a more advanced version, which will solve the problem of attribute name making troubles as validation message prefix. More on that - tomorrow.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>New blog - blogging like a geek</title>
   <link href="http://blog.geekq.net/2009/04/07/blogging-like-a-geek"/>
   <updated>2009-04-07T00:00:00+02:00</updated>
   <id>http://blog.geekq.net/2009/04/07/blogging-like-a-geek</id>
   <content type="html">&lt;h1 id='new_blog__blogging_like_a_geek'&gt;New blog - blogging like a geek&lt;/h1&gt;

&lt;p&gt;Now I am blogging like a geek - with the article content residing in text files managed by &lt;a href='http://git-scm.com/'&gt;git&lt;/a&gt; revision control software and HTML generation powered by a &lt;a href='http://github.com/geekq/jekyll/tree'&gt;ruby software&lt;/a&gt;. My new blog is movabletype free.&lt;/p&gt;

&lt;p&gt;&lt;a href='http://github.com/geekq/jekyll/tree'&gt;jekyll&lt;/a&gt; was originally written by &lt;a href='http://tom.preston-werner.com/'&gt;Tom Preston-Werner&lt;/a&gt;, a founder of &lt;a href='http://github.com'&gt;github.com&lt;/a&gt;. I did some adjustments to the markdown handling so it better suites my &lt;a href='http://en.wikipedia.org/wiki/Don&amp;apos;t_repeat_yourself'&gt;DRY&lt;/a&gt; attitude. I think, I&amp;#8217;ll further expand the software and probably merge it with &lt;a href='http://github.com/geekq/git-wiki/tree'&gt;git-wiki&lt;/a&gt; so it eventually leeds to a &lt;a href='http://en.wikipedia.org/wiki/Bliki'&gt;bliki&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Web design of the new site is currently more a kind of scientific style - without any bells and whistles, but I am going to change this soon.&lt;/p&gt;

&lt;p&gt;The new blog is available at &lt;a href='http://blog.geekq.net/'&gt;http://blog.geekq.net/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The old one is still available at &lt;a href='http://www.innoq.com/blog/vd/'&gt;http://www.innoq.com/blog/vd/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Things to do:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;copy the posts from the old blog&lt;/li&gt;

&lt;li&gt;enable comments&lt;/li&gt;
&lt;/ul&gt;</content>
 </entry>
 
 <entry>
   <title>impress!ve - presentation for geeks</title>
   <link href="http://blog.geekq.net/2009/03/29/impressive-presentation-for-geeks"/>
   <updated>2009-03-29T00:00:00+01:00</updated>
   <id>http://blog.geekq.net/2009/03/29/impressive-presentation-for-geeks</id>
   <content type="html">&lt;h1 id='impressve__presentation_for_geeks'&gt;impress!ve - presentation for geeks&lt;/h1&gt;

&lt;p&gt;Recently I&amp;#8217;ve discovered a great presentation tool chain for real geeks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;beamer latex class to describe the slides in latex&lt;/li&gt;

&lt;li&gt;vim for editing&lt;/li&gt;

&lt;li&gt;&lt;a href='http://impressive.sourceforge.net/'&gt;impressive&lt;/a&gt; (former keyJnote) by &lt;a href='http://keyj.s2000.ws/'&gt;keyJ&lt;/a&gt; for displaying&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are two points for improvement in this chain though.&lt;/p&gt;

&lt;h2 id='markdown_plus_latex'&gt;Markdown plus latex&lt;/h2&gt;

&lt;p&gt;A pure latex file demands too much markup for my taste, exactly as an html file requires too much angle brackets. I am going to use something like markdown with latex fragments the same way you can use html in markdown as a fallback for the case you need something special.&lt;/p&gt;

&lt;h2 id='motivation_for_the_impressive_fork'&gt;Motivation for the &lt;em&gt;impressive&lt;/em&gt; fork&lt;/h2&gt;

&lt;p&gt;Macs have the perfect presentation software: while an external beamer shows the slide you are talking about, your notebook shows&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the current slide&lt;/li&gt;

&lt;li&gt;the next slide&lt;/li&gt;

&lt;li&gt;time elapsed for the current slide&lt;/li&gt;

&lt;li&gt;total elapsed time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is &lt;strong&gt;exactly&lt;/strong&gt; what I need and what &lt;em&gt;impressive&lt;/em&gt; is unfortunately missing - the dual monitor (dual head) feature.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;impressive&lt;/em&gt; is implemented in python and the source code seems to be a perfect foundation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the code looks clean&lt;/li&gt;

&lt;li&gt;it is a bit C style, not object oriented - a lot of global, not (deeper) structured variables, but it is OK&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So keep an eye on &lt;a href='http://github.com/geekq/impressive'&gt;http://github.com/geekq/impressive&lt;/a&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Translator hints with gettext</title>
   <link href="http://blog.geekq.net/2009/02/27/translator-hints-gettext"/>
   <updated>2009-02-27T00:00:00+01:00</updated>
   <id>http://blog.geekq.net/2009/02/27/translator-hints-gettext</id>
   <content type="html">&lt;h1 id='translator_hints_with_gettext'&gt;Translator hints with gettext&lt;/h1&gt;

&lt;p&gt;In our current project we use gettext not only for translating, but also for localizing date and time formats.&lt;/p&gt;

&lt;p&gt;One day I&amp;#8217;ve found out, that the user registration date looks strange if I switch the language to italian. The cause was, that the translator translated &lt;code&gt;%Y-%m-%d&lt;/code&gt; to &lt;code&gt;%A-%m-%g&lt;/code&gt;. It is logical, because &amp;#8220;year&amp;#8221; is &amp;#8220;anno&amp;#8221; in italian, so he translated &lt;code&gt;%Y&lt;/code&gt; to &lt;code&gt;%A&lt;/code&gt;. The result was&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;quot;Friday-02-09&amp;quot;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;neither italian nor a correct full date.&lt;/p&gt;

&lt;p&gt;My first idea was to embed the translation hints into the msgid itself. But the problem is, if there is no translation for particular language, the msgid is used as date format, so we can not do that.&lt;/p&gt;

&lt;p&gt;I&amp;#8217;ve found out that the GNU gettext provides a special feature for such comments given by the programmer and directed at the translator; these comments are called extracted comments and inserted by &lt;code&gt;xgettext&lt;/code&gt; (for C programs) into the po file and appear as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#. extracted-comments
msgid untranslated-string
msgstr translated-string&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So the prefix is a hash sign followed by a dot. There is a &lt;a href='http://www.gnu.org/software/gettext/manual/gettext.html#Names'&gt;bigger example&lt;/a&gt; on the GNU web site.&lt;/p&gt;

&lt;p&gt;Unfortunatly this feature is not implemented in &lt;a href='http://rubyforge.org/projects/gettext/'&gt;ruby-gettext&lt;/a&gt; yet. I&amp;#8217;ll probably implement it some day. Then the following code will generate appropriate comments in the po file.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def format_date(user, t)
  # TRANSLATORS: please adjust the order of the date parts and the separator
  # %Y - Year with century
  # %m - Month of the year (01..12)
  # %d - Day of the month (01..31)
  # Example: %d.%m.%Y generates 23.02.1976
  user.timezone.utc_to_local(t.utc).strftime(_(&amp;#39;%Y-%m-%d&amp;#39;))
end&lt;/code&gt;&lt;/pre&gt;</content>
 </entry>
 
 <entry>
   <title>`acts_as_state_machine` successor `workflow`</title>
   <link href="http://blog.geekq.net/2009/02/25/acts_as_state_machine-successor-workflow"/>
   <updated>2009-02-25T00:00:00+01:00</updated>
   <id>http://blog.geekq.net/2009/02/25/acts_as_state_machine-successor-workflow</id>
   <content type="html">&lt;h1 id='_successor_'&gt;&lt;code&gt;acts_as_state_machine&lt;/code&gt; successor &lt;code&gt;workflow&lt;/code&gt;&lt;/h1&gt;

&lt;p&gt;While &lt;a href='http://agilewebdevelopment.com/plugins/acts_as_state_machine'&gt;acts_as_state_machine&lt;/a&gt; was not maintained for some years (the &lt;a href='http://github.com/rubyist/aasm/tree/master'&gt;revived&lt;/a&gt; version can be now found on github), a couple of successor libraries sprang up. My favourite was &lt;a href='http://github.com/ryan-allen/workflow/tree/master'&gt;workflow&lt;/a&gt; by Ryan Allen.&lt;/p&gt;

&lt;p&gt;Unfortunately, after we had adopted it in our current project, we&amp;#8217;ve found some undeterministic behaviour in the ActiveRecord integration area. So I&amp;#8217;ve streamlined the integration code by:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;strictly separating the implementation between the state machine/workflow specification (states, transitions) and the model entity state&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;putting all the model enhancements into modules and initiating the admixing the state machine functionality in the &lt;code&gt;self.included&lt;/code&gt; method. The modules are::&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;WorkflowClassMethods&lt;/li&gt;

&lt;li&gt;WorkflowInstanceMethods&lt;/li&gt;

&lt;li&gt;ActiveRecordInstanceMethods&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;But you do not need to know all that implementation details to be able to use the workflow library. Just &lt;a href='http://github.com/geekq/workflow/tree/master'&gt;grab my version from github&lt;/a&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Rails internationalization - locale-selector released</title>
   <link href="http://blog.geekq.net/2009/01/02/rails-internationalization-locale_selector-1.93.0-release"/>
   <updated>2009-01-02T00:00:00+01:00</updated>
   <id>http://blog.geekq.net/2009/01/02/rails-internationalization-locale_selector-1.93.0-release</id>
   <content type="html">&lt;h1 id='rails_internationalization__localeselector_released'&gt;Rails internationalization - locale-selector released&lt;/h1&gt;

&lt;p&gt;Just released the first public version of the &lt;code&gt;locale_selector&lt;/code&gt;. locale_selector is an internationalization library and provides a wrapper around the &lt;a href='http://www.yotabanana.com/hiki/ruby-gettext.html?ruby-gettext'&gt;ruby-gettext&lt;/a&gt; gem.&lt;/p&gt;

&lt;p&gt;You can install it simply by&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gem install locale_selector&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;or see the &lt;a href='http://locale-selector.rubyforge.org/'&gt;complete installation instructions&lt;/a&gt;&lt;/p&gt;
&lt;a href='/images/2009/duesseldorf_at_night/02012009354.jpg'&gt;
  &lt;img class='floatright' src='/images/2009/duesseldorf_at_night/02012009354_small.jpg' alt='Duesseldorf at night' /&gt;
&lt;/a&gt;
&lt;p&gt;Features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Offers a convinient way to specify the list of locales supported by your application.&lt;/li&gt;

&lt;li&gt;Provides a html UI control for locale selection.&lt;/li&gt;

&lt;li&gt;Maintains the user preference in a cookie and provides callback for saving the preference e.g. in a database.&lt;/li&gt;

&lt;li&gt;Offers rake tasks for maintaining the translations. The suggested translation maintenance workflow is really enterprise proven. E.g. updating single po-files according to the real world responsibility distribution.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Provides some fixes and improvements for the ruby-gettext library:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;better parsing for ActiveRecord models in some edge cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;namespaced classes&lt;/li&gt;

&lt;li&gt;multiple model classes per file&lt;/li&gt;

&lt;li&gt;and most important - fix for models, that are loaded in environment.rb due to e.g. referencing by a observer&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;html escaping built into the underscore &lt;code&gt;_()&lt;/code&gt; method&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;better customization for ActiveRecord validation methods - you can now avoid including the field name if you wish&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The fixes will be eventually merged into and released with the next releases of ruby-gettext. But for the impatient - just install the locale_selector with the monkey patches.&lt;/p&gt;

&lt;p&gt;You can also browse the &lt;a href='http://github.com/geekq/locale_selector/tree/master'&gt;source code&lt;/a&gt;.&lt;/p&gt;</content>
 </entry>
 
 
</feed>