Ketan's Musings

Where he blogs about his eclipse musings

Author Archive

Using Models In Rails Migrations

with 2 comments

It is quite tempting to use models in rails migrations.

At the time the migration is expected to run, the model class will have been updated already, so it is hard use that in the migration itself, even though it would be useful.

Consider this example:

class CreateUsers < ActiveRecord::Migration
    def self.up
      t.string :email, :null => false
      t.string :password, :null => false
    end
 
    # create a dummy user - with newer rails versions this should really go in seeds.rb!
    User.create!(:email => 'user@example.com', :password => 'demo')
  end
 
  def self.down
    drop_table :users
  end
end

Doesn’t get simpler than this!

Now lets add another migration:

class AddSSNForUser < ActiveRecord::Migration
  def self.up
    add_column, :users, :ssn, :string, :null => false
  end 
  def self.down
    remove_column, :users, :ssn
  end
end

And a corresponding validation to the User model:

class User < ActiveRecord::Base
  # ...
  validates_presence_of :ssn
  # ...
end

Now if you were to run the database migrations on an empty database, the first migration would fail because of the lack of a SSN. Worse, it is quite possible that the User class is now called something else.

How do you get around this issue:

class CreateUsers < ActiveRecord::Migration
  # define your own User class
  class User < ActiveRecord::Base
    #...
  end
 
  def self.up
    # clear all cache that rails maintains about the User class/table mapping
    User.reset_column_information
    create_table :users do |t|
      # whatever we did in the example above
    end
 
    # this statement will always work no matter what.
    User.create!(:email => 'user@example.com', :password => 'demo')
  end
 
  def self.down
    drop_table :users
  end
end

Sometimes using SQL for performing data migrations could get quite cumbersome and unreadable, and using model objects is the simplest way to run any form of data migration. In such a case, it’s better to copy your model code into the migration.

Written by Ketan

November 20th, 2010 at 9:55 pm

Posted in rails,ruby

Tagged with ,

JRuby and static imports

without comments

The last few weeks have been interesting working with SWT and some newly acquired (J)Ruby chops, to hack together a couple of approaches to build a recorder for SWTBot. Here is one of the interesting hacks which was cobbled together:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
require 'java'
module Recorder
  module SWTInitializer
 
      include_package 'org.eclipse.swt'
      include_package 'org.eclipse.swt.layout'
      include_package 'org.eclipse.swt.widgets'
      include_package 'org.eclipse.swt.custom'
      include_packate  'org.eclipse.swtbot.swt.finder'
 
      # loop through a few known classes to implement static imports
      def method_missing(method, *args, &amp;block)
        klass = [
            org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory,
            org.eclipse.swtbot.swt.finder.utils.SWTUtils].find do |k|
          k.respond_to?(method)
        end
        return klass.send(method, *args, &amp;block) if klass
        super
      end
  end
end

To use this class:

1
2
3
4
5
6
7
8
class Application
  include Recorder::SWTInitializer
  # the with_text invocation falls into the method_missing 
  # which delegates it to WidgetMatcherFactory
  def self.run
    SWTBot.new().widgets(with_text('hello'))
  end
end

Written by Ketan

September 29th, 2010 at 7:57 am

Posted in Uncategorized

Tagged with , , ,

SWTBot Release updated for Helios

with 2 comments

A new release of SWTBot is now available at a the usual location.

This purpose of this release is to work with the helios release and contains a few bug minor fixes.

A few other improvements were done to improve the code coverage for some plugins that were not tested too well.

Forthcoming releases will improve on the code coverage. Well, the irony.

Written by Ketan

July 23rd, 2010 at 9:03 am

Posted in eclipse,Open Source

Tagged with , ,

Code Complexity Visualization for Ruby

with one comment

Only Valid Measure of Code Quality

Only Valid Measure of Code Quality

Image from http://www.osnews.com/story/19266/WTFs_m

WTF implies lack of clarity. Clear code is easier to understand, easier to maintain and easier to extend.

Announcing saikuro_treemap — an easy to setup tool to generate complexity treemaps of ruby code.

See a demo for yourself.

Complexity Visualization of Rake

Written by Ketan

July 20th, 2010 at 8:33 pm

Posted in Uncategorized

Tagged with , ,

An embedded interpreter for eclipse

with 2 comments

As a java developer who is starting to use ruby and javascript for a lot of things lately — there is one thing I miss most. An embedded shell/interpreter for eclipse!

An embedded console or an interpreter is a very powerful tool, it allows you to do some very interesting things with your software as it is running, play around with it, tweak it and anything else you can ever imagine. All of this without the edit-save-compile-relaunch cycle.

Lately I’ve been working on an embedded console for eclipse. The primary motivation was to try out scripting approaches for SWTBot. But I soon realized that I was using it for more than just scripting tests. I was using it to learn how eclipse works, try out different approaches to decide which one is best.

Some of the features include code completion, history lookup. This is possible using jruby’s objectspace and the readline support.

Here’s a small teaser video of what you can do with it an embedded jruby console for eclipse.

Read the rest of this entry »

Written by Ketan

May 28th, 2010 at 12:40 am

Posted in eclipse,Open Source

Tagged with , ,