trees in sql – no more nested sets?

Posted November 17, 2006 by hildolfur
Categories: SQL

I looked for sql trees on the net, and it seems there is another method called ‘nested intervals with farey fractions’. This is really heavy math stuff, but looks interesting. Obviously it gets around the problem that on average half of the total nodes of an SQL nested set table must be updated if you do a structural change (insert, move, delete…) – which results in very bad performance for large trees.

Recommended reading:

Nested intervals with Farey fractions

Static hierarchies and binary fractions in PostgreSQL 

Modeling trees with nested sets and nested intervals 

mission of the day: javascript windows

Posted November 17, 2006 by hildolfur
Categories: Ruby/Rails

Need to get on these for my ActiveAcl plugin. Fortunately there is this “prototype window” thing which makes creating these a no brainer:

http://prototype-window.xilinus.com/

SQL Design Patterns

Posted November 16, 2006 by hildolfur
Categories: SQL

Oh man, I desperatly need this book:

http://www.rampant-books.com/book_2006_1_sql_coding_styles.htm

Too bad it will be published first in October 2007… *grr* It’s so hard to get valuable information on professional SQL usage. 😦

has_many_polymorphs enhanced!

Posted November 6, 2006 by hildolfur
Categories: Ruby/Rails

http://blog.evanweaver.com/articles/2006/06/02/has_many_polymorphs

Evan really did a great job, adding model namespaces, table names and double polymorphism. Thank you so much! And thanks for the testing credits, though you really improved that as well 🙂

rails engines testing with rcov

Posted November 1, 2006 by hildolfur
Categories: Ruby/Rails

Ever wanted to do coverage tests on an engine? Well, it really hurts that using the default test runner like “rake test:engines:my_engine:rcov” (that is added by the rcov helper plugin for rails) will run the tests, but ignore everything below the “vendor” folder. Unfortunately this includes the engine files as well. And at the time I tried it last, this exclusion was hard coded into rcov.

Figured out how to do it anyway – add this to your engine’s tasks (using gacl_engine from my engine…)


require 'rake/clean'

# output directory - removed with "rake clobber"
CLOBBER.include("coverage")
  
# RCOV command, run as though from the commandline.  Amend as required or perhaps move to config/environment.rb?
RCOV = "rcov"

desc "generate a coverage report in coverage/gacl_engine"
namespace :gacl_engine do
  task :test_coverage do
    sh "cd vendor/plugins/gacl_engine;#{RCOV} --rails -T -Ilib:test --output ../../../coverage/gacl_engine test/all_tests.rb"
  end
end

Then create test runners in your engine test directory.

all_tests.rb:

# Combine unit and functional tests in one run
%w( unit_tests functional_tests ).each do |f|
  require File.dirname(__FILE__) + "/#{f}.rb"
end

unit_tests.rb:

# Run all unit tests
Dir[File.dirname(__FILE__) + "/unit/**/*_test.rb"].each do |f|
  require f
end

functional_tests.rb and so on are accordingly created. Use the rake task and you’ve got coverage tests on engines…

character encoding in forms

Posted October 30, 2006 by hildolfur
Categories: html/css

Things that give a headache in IE and Firefox – the character encoding when sending form data doesn’t always seem right. Fortunately there is a workaround:

http://www.crazysquirrel.com/computing/general/form-encoding.jspx

breakpointer: where am I?

Posted October 29, 2006 by hildolfur
Categories: Ruby/Rails

I heard there is a gem around to give you a stack trace in breakpointer, but you can quickly get it without as well. The trick is to raise an exception and immediately catch it, printing it’s stack trace. Blatantly stolen from my Java experience 🙂

begin; raise; rescue Exception => e; puts e.backtrace; end

class reloading in ruby

Posted October 29, 2006 by hildolfur
Categories: Ruby/Rails

Ever wanted to undefine a class in ruby and reload it afterwards? Needed this as I wanted to restart a test suite with the models using table names – tests for the has_many_polymorphs plugin. It’s a dirty hack and doesn’t work with instantiated fixtures, but at least it makes shure the same tests run on models with table_name set.

To “reload a class” you unset the class constant in the main object name space and then reload the class source file. In my case I wanted the table name to be set before the association creation macros (has_many, belongs_to etc.) are called, so they use the correct table name.

This works by unsetting the class constant, then recreating the class manually by setting the class name constant to the newly created class, setting the table name and afterwards loading the model file, enhancing the class we created before with the table name already set.

class PolymorphTableTest < PolymorphTest
    
  fixtures :test_petfoods, :test_cats, :test_dogs, :test_wild_boars  
  CLASSES = [:Cat, :Dog, :EatersParent, :WildBoar, :Kitten, :Tabby, :Petfood]
        
  def setup
    names = CLASSES
    # unset classes
    Object.class_eval do
      names.each do |clazz|
        remove_const clazz.to_s if const_defined? clazz.to_s
      end
    end
    
    # create new and set table names
    [:Dog, :Cat, :WildBoar, :EatersParent, :Petfood].each do |name|
      Object.class_eval do
        clazz = const_set(name.to_s, Class.new(ActiveRecord::Base))
        clazz.set_table_name "test_#{name.to_s.pluralize.underscore}"
      end
    end
    
    # reload model files
    names.each do |clazz|
      load "#{clazz.to_s.underscore}.rb"
    end
    super
  end
  
  def teardown
    super
    names = CLASSES
    
    # unset model classes
    Object.class_eval do
      names.each do |clazz|
        remove_const clazz.to_s if const_defined? clazz.to_s
      end
    end
    
    # reload model files
    names.each do |clazz|
      load "#{clazz.to_s.underscore}.rb"
    end

  end
end

has_many_polymorphs on the way…

Posted October 27, 2006 by hildolfur
Categories: Ruby/Rails

http://blog.evanweaver.com/articles/2006/06/02/has_many_polymorphs

it’s an absolut “must have” if you plan to do anything with polymorphic associations in rails. Just have a look at it.

I tried to integrate it today with my gacl system – only to find out it doesn’t support models inside modules. Too bad!!! 😦

I refactored my code to make it work *ouch*, but – much better!! – evan was very kind and told me he would make his plugin support modules if I write up some test cases.

Hurray!!! 🙂

object oriented design patterns

Posted October 26, 2006 by hildolfur
Categories: Ruby/Rails

While searching for docs on singleton classes, I stumbled on some ruby features and design patterns I did’t know – at least the delegator pattern was unknown to me…

http://www.rubycentral.com/book/lib_patterns.html