I was doing some research on a future product I plan to develop and found that I extremely dislike creating fixtures for nested_set models. You know, I’m pretty much lazy. I don’t want to calculate lft and rgt. I thought about what can I do to simplify my life.
Attention: may be I was blind again and missed some already made tool. Anyway, I’ve done something, and I would like to share it.
I’ve created Fixturease gem. It is not published at Rubyforge yet, so you can download it here
Now, you can run it as:
$ RAILS_ROOT=/path/to/app RAILS_ENV=development FIXTURES_ROOT=path/to/fixtures fixturease.rb
- if you’re in a root of your rails app, you can omit RAILS_ROOT
- if you want test mode, you can omit RAILS_ENV
- FIXTURES_ROOT is relative path to fixtures. By default, it is spec/fixtures (yes, we use RSpec)
What it does? It creates a special console for you. You just create your objects as you want, and when ready, just exit from it:
$ fixturease.rb
Loading Fixturease console for test mode.
>> Clause.create :document => Document.create
=> #<Clause:0x130e368 @document=#<Document:0x203f5dc @new_record_before_save=true, @errors=#<ActiveRecord::Errors:0x20100e8 @base=#<Document:0x203f5dc ...>, errors{}, attributes{"id"=>31}, new_recordfalse, errors#<ActiveRecord::Errors:0x1301a8c @base=#<Clause:0x130e368 ...>, errors{}, attributes{"created_on"=>Wed Jan 17 20:39:52 0200 2007, "document_id"=>31, "updated_on"=>Wed Jan 17 20:39:52 0200 2007, "lft"=>1, "id"=>25, "content"=>nil, "rgt"=>2, "parent_id"=>nil}, new_recordfalse
>>
Saving fixtures:
Document [./spec/fixtures/documents.yml]: 31
Clause [./spec/fixtures/clauses.yml]: 25
It will append your fixtures files with newly created objects. That’s it. Easy? I assume yes! Useful? I don’t know, but I like it!
P.S. The code inside is awful. I know. It was a quick hack, really.
Update: better version here





Wow! I’m definitely going to use this!