Showing posts with label rails. Show all posts
Showing posts with label rails. Show all posts

June 29, 2011

Jasmine Guard for CoffeeScript rocks on Rails 3

CoffeeScript sure is interesting and I had a very positive experience trying it with Jasmine BDD testing kit. With Guard autocompiling the .js files, it becomes a charm on Rails 3.

Jasmine BDD in CoffeeScript on Vimeo.

This setup guide from pivotallabs.com coupled with this updated gist you will have the infrastructure set up in no time. Any change to the CoffeeScript source or spec files should trigger the Guard to recompile the JavaScript.

To compile "bare" js see this tip to add :bare => true to your Guardfile.

Start guard and jasmine servers and browse to localhost:8888 to run the test suite.
$ guard
$ rake jasmine

Here is a good example of writing a CoffeeScript spec. More spec examples will eventually sprout up, undeniably. And naturally, the Jasmine wiki is a good source of information.

Here's one example: timer.coffee
class Timer
  constructor: (@timer_json) ->
    @start_date = new Date(@timer_json.start_date)
    @end_date = new Date(@timer_json.end_date)
    [@start_hours, @start_minutes] = @timer_json.start_at.split(":")
    [@end_hours, @end_minutes] = @timer_json.end_at.split(":")
And the corresponding test: timer_spec.coffee
describe 'timer-parser', ->

  timer_data = {
    start_date: "2011/06/20", end_date: "2011/06/25",
    start_at: "12:00", end_at: "04:04"
  }

  it 'should parse properly', ->
    timer_json = JSON.parse JSON.stringify timer_data
    timer = new Timer(timer_json)
    expect(timer.start_date).toEqual new Date "2011/06/20"
    expect(timer.end_date).toEqual new Date "2011/06/25"
    expect(timer.start_hours).toEqual "12"
    expect(timer.start_minutes).toEqual "00"
    expect(timer.end_hours).toEqual "04"
    expect(timer.end_minutes).toEqual "04"

May 12, 2010

Gource animation of Rails-portlet development

Gource is a fabulous program that can visualize common version control repository history. This is the development of Rails-portlet for the last few years, in a few seconds.

I had to write a small Python script that parses logs from Git submodules. The script is at Gist and may be freely used.

December 30, 2008

ARTS

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

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