RSS RSS feed | Atom Atom feed
Popular Articles: Tom Riddle's Magical Diary | AJAX Lego Robot | AJAX CAPTCHA | SQL Multisets

Single Table Polymorphic Inheritance with ActiveRecords

Ruby on Rails warning: toplevel constant XX referenced by YY

Implementing a single table polymorphic inheritance in rails is dead easy. Simply do
class Fruit < ActiveRecord::Migration def self.up create_table :persons do |t| t.column :type :string t.column :shape, :string t.timestamps end end def self.down drop_table :fruits end end
or if you a migrating...
class Fruit < ActiveRecord::Migration def self.up add_column :fruits, :type, :string end def self.down remove_column :fruits, :type end end
Now, all you have to do is to create your sub-classes
class Fruit < ActiveRecord::Base def calc_volume ... end end class Apple < Fruit def calc_volume ... end end
Don't forget to put Apple in a separate file by itself apple.rb or else ruby will spit out an unconditional warning if you reference Apple in your code with Fruit::Apple
warning: toplevel constant Fruit referenced by Fruit::Apple

Read more...

Tags :
slashdot digg del.icio.us technorati [more]

GreenMail v1.3 Released

The Birth of Mr GreenMail JBoss Service

This release contains minor changes to GreenMail core, most notable the introduction of slf4j for logging.

New in this release is a JBoss service for GreenMail. The service runs GreenMail as a lightweight mail server sandbox, nicely suited for developers.

Check out the project page http://www.icegreen.com/greenmail for the latest documentation!

Note about the released files (goto download page):

  • greenmail-1.3-bundle.jar : mvn upload bundle
  • greenmail-1.3.jar : contains all greenmail core classes
  • greenmail-1.3-src.zip : contains sources, javadoc, required libs, docs
  • greenmail-1.3.zip : like above, but no sources
  • greenmail-jboss-service-1.3.sar : deployable JBoss service archive


Changes:

  • Added logging via slf4j, replacing System.out.println
  • Minor improvements:
    • c.i.g.AbstractServer exposes ServerSetup
    • c.i.g.util.GreenMailUtil exposes sendTextEmail(...)
  • New JBoss service wrapper (see the project page)

Credits:
Marcel May added the JBoss service and contributed the logging changes. Thanks!

Upcoming changes:
For 1.4 we plan to replace the current Foedus smtp and pop implementation with http://subetha.tigris.org

slashdot digg del.icio.us technorati [more]

Internet Explorer 6 & 7 document.domain bug

Disclaimer: Im not sure this a bug discovery...couldn't find anything that resembled it after a couple of minutes of googling, or a super silly security restriction.

I've been coding some javascript ajax code that inserts an iframe, and into the created iframe, inserts a form and submits the form to a (cross domain) server. This is an old cross-domain ajax technique by now, but while implementing it I came across this ridiculously annoying bug in IE. The line in red below will result in an "Access Denied" exception being thrown...again, only in IE. Tried this with IE6 under WinXP Media Center Edition SP2, IE7 under WinXP Home Edition SP2, and IE7 Windows Vista Home. Needless to say...it works just fine in FireFox.

<script type="text/javascript"> document.domain = 'waelchatila.com'; var new_html = '<iframe id="frm" name="frm" src="http://waelchatila.com"></iframe>'; document.body.innerHTML+=new_html; try { var iframe_doc = document.getElementById("frm").contentWindow.document; } catch (e) { alert(e); } </script>
Try it by clicking the button below.

This page should be served off http://waelchatila.com. As you can see, the document.domain is explicitly set to the domain the page was served from. The iframe src is also from the same server. I've tried it with a whole variety of variations, none of the demonstrated here but tested for.

  • iframe's document.domain = 'waelchatila.com';
  • iframe src is empty
  • iframe src is "javascript:false"
There should be no problem accessing the iframe's content in either case.

Now, if the document.domain = 'waelchatila.com'; line is removed, it works. In order for this to work, you'll need to reload this page if you clicked the previous button since document.domain has now already been assigned.

The same results present itself if the iframe is created with document.createElement('iframe') and document.appendChild(...)

I've yet to find a workaround for this issue. I'll be positing another entry if I found out how. Please let me know if you know of one!

slashdot digg del.icio.us technorati [more]

RXTX on Gumstix

so binaries

Since it could be tricky to get the RXTX sources compiling properly for the gumstix platforms I thought I'd spare anyone the pain by providing my binaries.

However, since you're here, I would advise against using java on the gum. Sure, the programs themselves are a breeze to develop, but getting jam and classpath to compile and run properly can very well take a considerable amount of time. Just think of the reason why you are reading this. Chances are you probably have troubles getting the RXTX sources to compile or run smoothly. Just do yourself a favor, write it in small fast c/c++ or something.
slashdot digg del.icio.us technorati [more]

Groovy vs BeanShell

Quick speed performance test

Below is a quick and dirty test to compare the speed performance between bean shell and groovy. Bean shell is an order of magnitude faster. If you don't need groovy closure and other nice groovy features...just use bean shell. It's only one small jar away.
public static void main(String[] args) throws EvalError { StringBuilder script = new StringBuilder(); for (int i=0;i<900;i++) { script.append("res"); script.append(i); script.append("="); script.append(i+"+"+i+"*"+i); script.append(";\n"); } GroovyShell groovyShell = new GroovyShell(); Interpreter bsh = new Interpreter(); long t = System.currentTimeMillis(); groovyShell.evaluate(script.toString()); System.out.println(System.currentTimeMillis() - t); t = System.currentTimeMillis(); for (int i=0;i<1000;i++) { int j = i+i*i; } System.out.println(System.currentTimeMillis() - t); t = System.currentTimeMillis(); bsh.eval(script.toString()); System.out.println(System.currentTimeMillis() - t); }
slashdot digg del.icio.us technorati [more]