Monkeybars - Little bug

Posted by Moser on 10 Sep 2009

For a little side project I am currently evaluating different ways of GUI programming using Ruby. Monkeybars is one of the most interesting candidates. There is a really good article about it, if you want to learn more. Version 1.0.4 has a little, but annoying bug: If you generate a new application skeleton it won’t compile/run:

manifest.rb:32:in `require': no such file to load -- monkeybars (LoadError)
    from manifest.rb:32
    from manifest.rb:21:in `require'
    from main.rb:21

To fix it, you got to change line 21 of manifest.rb:

add_to_classpath '../lib/java/monkeybars-1.0.2.jar'
#to
add_to_classpath '../lib/java/monkeybars-1.0.4.jar'

ActiveRecord: write_attribute and UTC conversion

Posted by Moser on 23 Jun 2009

A little gotcha with custom setters for datetime attributes is, that when setting an attribute through ‘write_attribute’ it is not converted to UTC (or whatever else your default time zone is). This problem can easily be reproduced:

./script/generate model Thing a:datetime b:datetime

My model:

class Thing < ActiveRecord::Base
  def b=(d)
    write_attribute(:b, d)
  end
end

My tests:

require 'test_helper'

class ThingTest < ActiveSupport::TestCase
  test "a and b" do
    d = DateTime.now
    t = Thing.new
    t.a = d
    t.b = d
    t.save
    assert_equal t.a, t.b
    t.reload
    assert_equal t.a, t.b
  end
end

The second assertion will fail. Is this intended or a bug? Quick fix:

  def b=(d)
    d = d.utc
    write_attribute(:b, d)
  end

jQuery, jRails and the Accept header

Posted by Moser on 18 Jun 2009

I included jQuery by installing the jRails plugin, which seems nice because you can continue using rails’ ajax helpers. But when I tried to implement a more special functionality by using the $.ajax function it proved impossible to set the Accept header for my request. Neither using the dataType option nor by setting it directly in a beforeSend function. The Accept header always read:

text/javascript, text/html, application/xml, text/xml, */*

I was about to uninstall and hate jQuery for the rest of my life. By coincidence I took a look at jrails.js which is a part of the jRails plugin:

    (function($)
    {
      $().ajaxSend(
        function(a,xhr,s){
          xhr.setRequestHeader("Accept","text/javascript, text/html, application/xml, text/xml, */*")
        }
      )
    }
    )(jQuery); 
    [...]

WTF? Is that documented anywhere? This hard coded shit stuff breaks rails’ respond_to functionality, doesn’t it?t To be fair, it works if you use extensions to determine what datatype you expect. (Like /things/1.js) But I don’t do that when I build a custom ajax request where I can set the Accept header directly.

Update: Some research on the topic “accept header vs. extension” showed that in terms of cross browser compatibility I should favor the extension approach. And I’m not surprised it’s Microsoft’s fault :-)

ActiveRecord: Overwrite attribute setter

Posted by Moser on 17 Jun 2009

It’s somehow trivial, but I found out about ‘write_attribute’ only just: (another case of rtfm)

class Thing < ActiveRecord::Base
  def name=(str)
    str.upcase! unless str.nil?
    write_attribute(:name, str)
  end
end

Update: Be careful if you use this with Datetime attributes.

Multi-line comments in ruby

Posted by Moser on 07 Apr 2009

I keep forgetting how to do them, so I post myself a little reminder here.

Just put

=begin
...
=end

around the stuff to be commented out. There must not be whitespaces before these keywords.

dm-optlock now on github

Posted by Moser on 03 Mar 2009

dm-optlock is on github now. It can still be installed as gem:

gem install moser-dm-optlock

If you haven’t done before, you need to add github to your gem sources:

gem sources -a http://gems.github.com