Ruby Constants – Note to Self ::

Well after trying to access a ruby class constant for the zillionth time with Class.CONSTNAME.
( Which btw. very nicely gives the error "Undefined Method Class.CONSTNAME"...)
I've decided to write this note on my blog...

Remember:

print Class::InnerClass::CONSTANT
print Class::InnerClass.method_name

* kick * to my own butt..

Great method to_json

A very nice method to convert a Ruby array to Javascript.

print { 'key1' =>  'value1', 'key2' => [ 1,  2, 3 ] }.to_json
outputs:  { key1: 'value1', key1: [ 1, 2, 3 ] }

Rails: unit test fixture_path NameError.

Last day of my Holiday :(
Trying to write a unit test for my Rails application, I constantly got the error message below:


NameError: undefined local variable or method `fixture_path' for #<LessonTest:0x9a611f8>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/test_process.rb:452:in `method_missing'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:593:in `load_fixtures'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:538:in `setup_with_fixtures'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:575:in `setup'

Very strange: the Unit Test I've written was generated and exactly the same as shown in the example in the "Agile Web Development with Rails 2nd Edition" book.


require File.dirname(__FILE__) + '/../test_helper'
class LessonTest < Test::Unit::TestCase
def test_truth
assert true
end
end

Well I found a solution, but I still haven't found the reason WHY this is happening. You can see this is as a temporary hack.

In your: "/test/test_helper.rb" add the following method:


def fixture_path
File.dirname(__FILE__) + "/fixtures/"
end

Ruby on Rails automatic reload of libraries

Currently I'm busy developing a Rails Application. I'm busy developing some general classes that aren't part of a model/controller or view. I placing these classes in the 'lib' directory.
Things are fine ... well not quite ...

The problem while developing is the libraries aren't reloaded on each request. This is very frustrating when you are trying to write a library.

Some things I found (but don't seam to work):

Rest the application in the console. Well the application reset, but not the libraries.

$ ./script.console
Loading development environment...
&gt;&gt; Dispatcher.reset_application!

I assume the libraries directory is loaded in the ruby booting class loader. ( I'm still thinking Java ). So when Webrick starts these libs are loaded.
The models and controllers etc. are loaded by the rails class loader.

Somehow I need to reload the library in the rails class loader space.... Doesn somebody have a solution!?
Perhaps I should find another directory...

I will keep you informed if I find a solutions.

---

1 august 2007: Well found a solution for the problem, I even wonder if it really was a problem..... (shame on me)
If I place the class "MyOwnClass" in the file "lib/my_own_class.rb" without including the source file. (This isn't required, because Rails automatically includes it) it seems to be refreshed every time I do a request...

Nice.. !

Ajax Table Paginated Sortable and Searchable

Currently I'm working on an application for a client. It's build in Ruby on Rails. After using ActiveScaffold for the startup I've decided to remove this plugin and write my own Ajax table.

I found a nice article / sample about this subject: http://dev.nozav.org/rails_ajax_table.html

The article is nice for a single table, but I need multiple tables, so I need to wrap some stuff in helpers. But that shouldn't be a problem.