December 30, 2008

ARTS

ARTS has branched, as the original author has not updated the project. The new branch at github.

October 15, 2008

jdbcpostgres-adapter and the numeric datatype

Vanilla Ruby with the postgres-pr adapter stores the 'numeric' datatype to BigDecimal. Unlike expected, the column has to explicitly define 'precision' and 'scale' when used in JRuby with the JDBC adapter, otherwise the scale is assumed to be 0 and all decimals are dropped. I have a column 'weight' in a table, in the original migration:

t.column :weight, :decimal, :null => false

This was corrected with the migration:
def self.up
  change_column :weights, :weight, :decimal, :null => false, :precision => 4, :scale => 1
end

def self.down
  change_column :weights, :weight, :decimal, :null => false
end

September 13, 2008

RJS testing

Last week I spent two very full days in creating an RJS-enhanced admin GUI on Rails. As RJS is extremely brittle ( it takes some time to tie the Ajax and css together and still it's easy to break an aspect of functionality without realizing it ), testing is important to later add new functionality, and a necessity for another developer to write new code without regressions. In the GUI there is a list of users and a list of groups. On the page there is an XHR call to the AjaxController, which responds by updating a div on the page.
def select_group
raise unless request.xhr?
render :update do |page|
  page.replace_html :zcore_inspector,   :partial => 'admin/ajax/group'
  page.replace_html :zcore_group_users, :partial => 'admin/ajax/groups_users'
end
end
Kevin Clark has written an RJS testing plugin (ARTS) and the guide gives a good head start. As RJS returns a chunk of JSON-encoded JavaScript, it's simple to regexp for an item that is expected to be rendered from the partial. As can be seen below, this is a powerful testing utility.
xhr :get, :select_group, :groupid => group.id, :uid => @admin.id
  assert_response :success
  assert_not_nil assigns(:admin)
  assert_not_nil assigns(:company)
  assert_equal assigns(:company),@company
  assert_not_nil assigns(:group)
  assert_equal assigns(:group),group

  # group.name should be in the returned JavaScript
  assert_rjs :replace_html, 'zcore_group_users', /#{to_json(group.name)}/

  # all groups users should be listed, as they are draggable the id is fixed.
  group.users.each do |u|
    assert_rjs :replace_html, 'zcore_group_users', /id=\\\"uid_#{u.id}\\\"/
  end

  # closer inspection of the group
  assert_rjs :replace_html, 'zcore_inspector', /span id=\\\"groupid.*>#{group.id}<\/span>/

  # if the group does not have forums, offer a link to create them
  unless group.has_zcategories?
    assert_rjs :replace_html, 'zcore_inspector', /create_message_board/

  else
    root = group.private_councelling_category
    assert_rjs :replace_html, 'zcore_inspector', /#{to_json(root.description)}/

    # groups members have their private category, assert their names are mentioned
    root.subcategories.each do |c|
      assert_rjs :replace_html, 'zcore_inspector', /#{to_json(c.user.fullname)}/
    end
  end
The assertion doesn't match if the string has non-ascii characters that are encoded by the String.to_json() method. I got around this by creating a json wrapper method to_json(), with the 'falling matchsticks' and whatnot so the string properly evalutes in the regular expression.
def to_json(s)
s.to_json.gsub(/^\"|\"$/,'').gsub(/\\/,'\\\\\\\\').gsub(/\(/,'\(').gsub(/\)/,'\)')
end

July 7, 2008

Localizing dates with Gettext in Rails

I came into a problem where I needed to localize Time and Date in my Rails app. I found Samuel Lown's gettext patch which worked really well for my needs. However, my application's primary language is not English, for practical reasons, I translated the strings to the same locale as my app. After putting this into my lib/ directory, I can use these methods to localize Date and Time:
Time.now.strftime('%c')
=> Maa Heinä 7 21:21:52 2008

Time.now.to_localised_formatted_s
=> Maa Heinä 07 21:21:52 EEST 2008

GettextDate::Conversions.monthnames(Time.now.month)
=> Heinäkuu

June 27, 2008

First post

Hello world! Watch this space for progress of my current project, which includes using Ruby on Rails application as a JSR286 portlet on the Liferay framework.