Posted September 16, 2008
Viviti uses a lot of single table inheritance. Because of the particular details of our project, we've implemented a few quick conventions and extensions to Rails' STI goodness. One of my favorites, extracted from a module we use in several different places:
class MySupertype::Base < ActiveRecord::Base
def self.new(atts = nil)
atts ||= {}
if atts[:type] and klass = "MySupertype::#{atts[:type].classify}".constantize and klass != self and klass.ancestors.include?(self)
return klass.new( atts )
end
return super
end
end
This code allows you to call MySupertype::Base.new with :type => 'MySubclass' and it will Just Work™.
Posted September 16, 2008
If you've ever tried to attach an onchange event handler to a <select> tag, you've definitely noticed that different browsers handle them in annoyingly different ways. This afternoon I whipped up a snippet of jquery code that makes them behave like they ought:
$.event.special.change = {
setup: function() {
if(this.tagName.toLowerCase() != 'select' || $.browser.safari) return false;
$(this).data('oldVal', $(this).val());
$(this).bind("click keydown", $.event.special.change.handler);
return true;
},
teardown: function() {
$(this).unbind("click keydown", $.event.special.change.handler);
},
handler: function(event) {
var args = arguments;
setTimeout($.bind_function(function() {
if($(this).val() != $(this).data('oldVal')) {
$(this).data('oldVal', $(this).val());
args[0].type = 'change';
return $.event.handle.apply(this, args);
} else {
return true;
}
}, this), 1);
return true;
}
};