My name is MatthewRudy, and I'm a Rails Developer. I am.... MATTHEW RUDY ON RAILS!!!


Cleaning Up Your Models Folder

I love really complex applications.
It feels safe and warm knowing that your code relies on 20 database tables, and maybe some of those have 5 inherited types.

So you end up with a models directory that looks like this;

app/
models/
carnivore.rb
carrot.rb
cauliflower.rb
meat.rb
person.rb
sausage.rb
smelly_sausage.rb
steak.rb
vegetarian.rb
vegetarian_sausage.rb
vegetable.rb

PROPER MESSY! (and that's only from 3 tables - meats, vegetables, people)
So we want to impose a structure on it,
we want;
app/
models/
meat/
sausages/
sausage.rb
smelly_sausage.rb
vegetarian_sausage.rb

meat.rb
steak.rb

people/
carnivore.rb
person.rb
vegetarian.rb

vegetables/
carrot.rb
cauliflower.rb
vegetable.rb

Without doing any work you could make this happen;
Rails expects the model People::Person to live in /people/person.rb,
so if you want to write "People::Person.find(...." every time, then feel free to do so.

The quick, and clean solution is just to add the following lines to environment.rb.
Rails::Initializer.run do |config|
%W( meat meat/sausages people vegetables ).each do |extra_path|
config.load_paths << "#{RAILS_ROOT}/app/models/#{extra_path}"
end
end

And, actually,
that's all it takes!!!

Easy-peasy, SCORE!!!!

You can do whatever you want to tidy up yours tests and fixtures, you just have to add a couple of "/.." to the top of any tests you've moved
require File.dirname(__FILE__) + '/../test_helper'
for anything in /test/unit,
require File.dirname(__FILE__) + '/../../test_helper'
for anything in /test/unit/stuff,
and so on....