In my Rails application i use a generic application.rhtml for all my pages. Amongst others, this code contains some jQuery code to manipulate my page

    var $j = jQuery.noConflict()

    $j(document).ready(function() {

      // remove all alt-tags
      $j("[alt]").removeAttr("alt");

      // validate all forms
      $j("form").validate({
        errorPlacement: function(error, element) {
          error.appendTo( element.parent() );
        }});

    });

The things i do in my document ready callback is simple: remove the alt attribute everywhere, so my tooltips will work on my links containing images, and if there is a form on the page i validate it.

Now for one form, i need to add special code, so that when a user fills something in a field, a select-box looses some options. The ideal place to do this, is in the document ready. But not for all pages. How do i solve this in some elegant way, and not use a different layout for that page.

I choose to use yield and content_for. Like this. Add the following line :

    var $j = jQuery.noConflict()

    $j(document).ready(function() {

      // remove all alt-tags
      $j("[alt]").removeAttr("alt");

      <%= yield :script %>

      // validate all forms
      $j("form").validate({
        errorPlacement: function(error, element) {
          error.appendTo( element.parent() );
        }});

    });

and in my view i write:

<% content_for :script do %>
     var allOptions = $j('#mention_thm_classification option').clone();

     $j('#mention_thm_reference_local').change(function() {
        var filter_txt = '.ref-';
        if (!$j(this).val()) { filter_txt = filter_txt + 'empty'; }
        else                { filter_txt = filter_txt + 'filled'; };
        $j('#mention_thm_classification').html(allOptions.filter(filter_txt));
    });
<%  end %>

So what i actually do is, check whether the value of a field changes. When it contains something a select-list with id mention_thm_classification is changed, and only the options with the correct class are kept.

For completeness, my select is built as follows:

<select name="mention[thm_classification]" id="mention_thm_classification" >
    <option value='1' style='background-color: #b7f9e2' class="ref-empty " >Green</option>
    <option value='2' style='background-color: #ffdebb' class="ref-empty " >Orange</option>
    <option value='3' style='background-color: #ffbbc6' class="ref-empty ref-filled" selected="selected">Red</option></select>

So i have described a nice way to add extra code to my document ready function without needing a special layout-page. And experienced for the umpteenth time how great jQuery is.

I am using the jQuery validate plugin which is awesome for straightforward form validation. What i especially like is that it offers the user a better user experience, because there is no need for the round-trip to the server.

I still need to do the validation on the server-side (rails) as well. I guess there should be a way to extract the validations from the model into the view transparently, but for now i just add a class "required" myself.

Now i did bump into a seemingly difficult problem, when the user wanted my to validate to having either one of both fields filled in. Luckily google came up with a beautiful answer quick enough!

I needed to adapt it somewhat, because my form-styling places each label-field combination inside a div, so i search for the second surrounding ‘div’ (containing the group of fields for which only one field is required).

    jQuery.validator.addMethod('required_group', function(val, el) {
      var $module = $j(el).parent('div').parent('div');
      return $module.find('.required_group:filled').length;
    }, 'Please fill out at least one of these fields');

How i love the jQuery and Rails community. Awesome!

In my Rails application, at a certain stage, it was necessary that my user performs an operation in another web-application (a GIS application) on the same database. I am using the following piece of code to make sure that i open a url in a new window, and that it should get the focus (jump to the front).

  <% javascript_tag  do -%>

    function newWindow(newContent, windowName)
    {
      var new_window;
      var new_window = window.open(newContent, windowName);
      if (window.focus) new_window.focus();
    }

    window.onload=function() {
      newWindow('<%=  link_to_location @mention, true %>', '<%= @gisapp_window_name %>');
      setTimeout(jump_to_select_tab_url, 2000);
    }
  <% end -%>

All is well when the window is first opened, but if the window was already opened, still it fails.
And of course i do all testing against firefox 3.5, although my user wants to use IE instead. So i try in IE and it works. Huh?
So it has something to do with Firefox 3.5.

And behold, as explained here, firefox is so smart it will block all javascript manipulation of windows, which is of course just what we need in this case. So inside firefox, i need to go to Options->Content->Javascript and make sure that i can run javascript that will manipulate opening and closing of windows. Done!

I am using Rails 2.3.4 at the moment, and as is well known, rails uses Prototype as default javascript library. For most purposes this suits me well.

But what i really like about jQuery is the wealth of plugins readily available.

My solution was not something like jRails, but i use them side by side. This might not be the best solution :)

First of, you have to include the correct javascript files.
Then, at the top of my application.rhtml, i write this:

    var $j = jQuery.noConflict()
    $j(document).ready(function() {
        /* insert some jquery magic here */
        ...
    });

This ensures that i can use the jQuery library nicely alongside the Prototype one. Just replace $ by $j for all jQuery calls.

For most jQuery things this is more than enough. But then I needed form validation, and needed to be able to call some jQuery from inside a link_to_function block. It took me a while to find, but actually (isn’t it always) it is dead-simple.

  def add_document_link(divname, parentdoc )
    link_to_function get_icon('adddocument'), { :title => t('adddocument')} do |page|
      new_doc = Document.new
      new_doc.doc_ident_p = parentdoc.doc_ident
      page.select('.add-items-link').each { |b| b.hide }
      page.insert_html :bottom, divname, :partial => 'documents/add_document', :object => new_doc, :locals => { :divname => "add-items-link"}
      page << "var $j = jQuery.noConflict(); $j(\"form\").validate();"
    end
  end

Easy as pie :) I love rails :)

I am developing Ruby on Rails on Windows platforms mostly. But using Rspec and cucumber on windows has a very strange side-effect: all a’s are missing as can be seen from the following screenshot:

rspec-without-a-smaller

Luckily, after some very extensive googling, i found a single blogpost with a fix!

Apparently it has something to do with UTF-8 encoding, and the simple solution is that you need to change the encoding of the current command prompt. This can be achieved via a simple call before you start:

chcp 1252

The aforementioned post then proposes to adjust the cucumber.bat to not have to type this every time. This is all good for cucumber, but not for rspec, and anyhow, every new update of the cucumber i would need to apply this fix again.

I was thinking that it might be possible to set the default codepage, which is 850 on my machine, to 1252 permanently. As this blogpost mentions, there are two different ways to achieve the wanted result.

Change the codepage system-wide

This can be done in the registry.

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage]
OEMCP=1252

But one commenter notes this is not without risk.

Only for command prompt

An alternative way is to put make sure that each time a console is opened (cmd.exe) the codepage is set automatically.

[HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor]
Autorun=chcp 1252

This will only work for console windows in which you run cmd.exe, which is just what i needed.

…or software patent troubles again

The weirdest news i read yesterday: I4i is not out to destroy Microsoft? In the article is described that some small company, i4i, has apparently won a case against Microsoft, claiming the Microsoft Word violates a patent they hold.
More specifically: patent 5787449.

The Texas judge ruled that Microsoft has to pay a fee/fine of 200 million dollar, AND that Word versions 2003, 2007 and future versions can no longer be sold. And the problem only arises when clients or customers use a feature called “custom XML”. What?

Let’s rewind here for a bit. What does that patent actually say? Let me summarize it for you. It describes two things: 1) it describes a system where the content is seperated from the actual representation, and 2) using a dialect of SGML. Now note: this patent was filed in 1994, before XML even existed (only since 1996), and the patent was finally issued in 1998. I tried to read the patent-document as some kind of specification, but i am not even sure what it is actually protecting. An algorithm? The idea? Because the idea of seperating content and representation is a common idiom. We all do it. Websites do it. We use css all the time. We use templates in Word … The idea of using metacodes (like Title, Chapter, …) and then looking up their definition the replace in the actual document (this is the example from the patent).

So it must have something to do with the specific implementation: they use an XML file. Now comes the really scary part. XML is really powerful, and Word is using a set of XML files, packaged in one zipped archive as their file format since Word 2003. This is really awesome. This is more open than any binary format. Now anyone who can read can actually create or process Word-files.

Microsoft calls it Open Office XML or OOXML, but that doesn’t really mean the standard is really open. Only Microsoft can amend the standard, and the other big standard, OpenDocument (ODF), is the fileformat originally used in OpenOffice.org (you see the deliberate confusion in the naming?), is totally different, not supported. A missed opportunity. But that is another discussion all together.

Now, i4i has no problem that Microsoft uses an XML file, just as long as the format is fixed. Meaning: you specify which tags you can read, are defined (for instance using DTD), and those tags are fixed. But Word allows you to add Custom XML, you can add your own fields into any Office document. And i4i claims that is the problem.

So now i am lost. This has nothing to do with seperating content and presentation (that was what the patent was about, right?). It could have something to do with how the extra tags are added, the custom XML. If they would use a lookup-table, to identify the custom tags, then they use a technique that is described in that patent.

But hey, wait, does that mean you can not use custom xml tags anymore? I have used tons and tons of xml, with no predefined DTD, so that the file format is extensible, and users can add their own extensions, and keeping the definition of the tags seperate.

In my personal opinion, and i think i am not alone, Microsoft is falsely convicted. There is no technological ground this patent can hold. This all comes back to the old software patent discussion. What is patentable and what is not. It seems so unnatural to patent software, algorithms, they are the building blocks upon which we learn and build the actual stuff. And the actual stuff (software/websites) is most of the times so general on the one hand, but very specific to the customer, i don’t see how these could be patented. I read that the way a website operates can be patented in the USA.

Back to the patent ‘449. In the press release found on i4i’s website, they claim that Microsoft infringes claims 14,18 and 20 from the patent. Those claims refer to the ability of using a lookup-table, being able to generate it, being able to read and replace the values, and being able to use more than one. That is so general! They could sue anyone who is using xml and some kind of look-up mechanism. I think the patent should just be declared invalid. But i guess that is easier said than done.

Speaking about the seperation between content and representation, speaking of metacodes, reminded me of Donald Knuth, inventor of TeX, and author of the greatest series of books of all time: The Art of Computer Programming. So let me point you to a letter by that same man, claiming mathematical ideas or algorithms should not be patented.

Final thoughts: i4i LP is a company that guards the patents from i4i. So this could be some kind of patent troll? Secondly, they choose Texas, where a large number of these kind of cases have been won, because of their technological inaptitude.

I am looking forward to Microsoft’s next step.

What are your thoughts?

PL/SQL package: lessons learned

Working with Oracle Spatial a lot, I found a great source of information in the Spatial DB Advisor, which besides a lot of interesting articles, also offers his source-code and PL/SQL packages for free. The site is not very user-friendly (trouble of seeing the threes through the wood), as finding the free packages is something that i only succeed in coming from Google :)
A shortcut: you can find them here.

But a word of warning: if you try to install this package in an existing schema, it will delete all indexes and tables. This is something i had to discover the hard way (painful i might add). So i have adapted my version of create_test_data.sql, where at the top of the file all indexes and tables of the user are dropped.

Nevertheless, extremely useful package.

I will list my favourites (for now):

  • isCompound: checks whether a geometry contains any circular/arc elements
  • ConvertGeometry: converts any special elements – circulararcs, rectangles, circles – in a geometry to vertex to vertex linestrings
  • to_2d: converts a sdo_geometry to 2d. Very useful for us, as geoserver can’t draw 2d, and we only use 2,5d anyway. Meaning that we always use a topview, and the z is the elevation level of the ground-level. Or for pipelines: below ground :)
  • sdo_mbr: function to determine the minimum bounding rectangle! i will use this to adapt my script to fill user_sdo_geom_metadata. Cool :)
  • functions to investigate the data of sdo_geometry (number of rings, number of vertexes, number of coordinates, the coordinates itself, …)
  • functions to investigate the meta-data
  • functions to manipulate your sdo-geometry: scale, rotate, affine transformations, move, adding and removing points, …
  • … and lots more …

In short: awesome!!

So after getting a terrifying scare, now rebuilding the databases (and adapted the script: won’t happen to me again). I mailed the original creator to add a note of warning on his site too. Might help other absent-minded developers :)

I also had another smaller problem, because the install-script overruled my ORACLE_HOME and PATH definition, which made sure SQL*PLUS was not found. But that was easily cured. I am very grateful that he puts up this package for free. And i learned another great lesson today: not to get too enthusiastic before i am sure that it all works ;)

I have been trying the Windows 7 beta, and i must admit it is pretty awesome. What is less awesome is the fact that now the RC is released, and i have to reinstall all my Windows 7 machines (three –i admit i was a bit too enthusiastic). And if i do that, once the final version is released, i have to re-install them again. For my standard day to day use not too bad, but not for my development machine.

So i am back to Windows XP now. With a great new look: Zune XP Theme. Nice :)

Working with a lot of open source products lately, so it would be nice to use an open source windows alternative. For the moment it would still be very hard for me to just not work on Windows. I still have to develop for .NET and MicroStation and AutoCAD, all only working on Windows platform.

So I hope that ReactOS will become more mature soon. It is really improving fast lately, but still not completely ready to replace Windows. I will have a test-drive real soon.

What are the other options? Run Linux and do my Windows work inside a VM? Buy a Mac and do the same? Any suggestions?

I am now working in Ruby on Rails for about a month. I have done a few smaller things before, investigating, trying out; but now I am really building a full application site in Rails! It feels great :)

But of course, just starting out in such a new environment, both ruby and rails, I encountered a lot of initial problems.

One of them is showing international, accented characters, like é, è, ê … from the database. Nor Rails nor irb could show the correct characters. I really needed to solve this problem, so this started an investigative journey.

The symptoms

When displaying a table of results from Rails in any browser, all accented characters were shown as white question marks inside a black rhombus (diamond?). When i tested this with irb i also got very weird results.

When using TOAD the results of a query are displayed correctly. But maybe TOAD just does it very clever? If i use the GUI version of SQL*Plus (sqlplusw) the results are also shown correctly. But when i use the console (command-line) version of sqlplus, the characters were also distorted. So maybe the clue could be found there.

Oracle NLS-LANG

No matter what character-set the Oracle database uses to store the data (e.g. UTF-8, UTF-16) the data is always converted to the clients character set. This is truly an amazing feature. This depends on a setting called NLS_LANG. Not sure wat it quite stands for. NLS_LANG is built up as follows:
NLS_LANG=language_territory.charset

The NLS_LANG property is set by default in the registry to the correct Windows codepage.

NLS_LANG="AMERICAN_AMERICA.WE8MSWIN1252"

But why doesn’t it work inside the command line console? Apparently because it used a different codepage. If you type chcp (requesting the codepage) at the DOS prompt, it would normally return 437 (it did on my machine). So you would need to enter


> set NLS_LANG=american_america.US8PC437
> require 'oci8'
> OCI8.new('user','pw','db').exec('select * from emp') do |r| puts r.join('|'); end

in your irb and then all results would be displayed correctly. The crucial line being the correct setting of NLS_LANG.

Wow! I got my results correctly in ruby! Now i was in the assumption that Rails would be a piece of cake, but that was wrong.

Fixing Rails

The easy idea would be to set NLS_LANG in Rails correct, before the oci8-library is required. My first approach was to set the NLS_LANG in the first line of the environment.rb with the following line:

ENV["NLS_LANG"] = "AMERICAN_AMERICA.WE8MSWIN1252"

But this didn’t work. I am using NetBeans 6.5, and it took me a while to realise that if I edited a file to contain special characters (fixed text, e.g. on a menu) it would work. NetBeans (or Rails for that matter) standard works with UTF-8. So all files are encoded in that way.

The easy solution would be to use

ENV["NLS_LANG"] = "AMERICAN_AMERICA.AL32UTF8"

But that didn’t work. I tried the alternative AMERICAN_AMERICA.UTF8 but that didn’t work either. I am just guessing here, but i think somehow the NLS_LANG setting didn’t get picked up in the Rails environment. It kept using the registry setting.

So I tried to turn it around: make Rails use the windows codepage instead of utf-8.

This took several steps:

  • change the default setting inside NetBeans to use the correct code-page instead of UTF-8. This in fact only affects the format of the files that are saved, but it is nevertheless important that all files being served are in the same format. Also convert all previously edited files from UTF-8 to standard ASCII. I used an editor to do that.
  • added the following to application_controller.rb:

    before_filter :headers_iso

    def headers_iso
    # make sure the charset matches the default Oracle NLS setting
    headers["content-type"]= "text/html; charset=windows-1252"
    end

  • added the correct META-tag to application.rhtml in the HEAD-section:

    Note: this meta-tag is not really needed, it has no real effect.

And that finally worked! :)