views:

348

answers:

2

I have a rails app moving along fairly well, but the fact that I'm doing this myself means that some poor sod is eventually going to see this and say, "What the hell were you thinking? Why did you put this here?!?!"

Where is that poor, sorry soul going to expect to see a series of classes that aren't used by anything but a single model class? Obviously, I could chuck it in the_model.rb along with class TheModel, but this may expand beyond the planned two classes...

I thought about lib, but it doesn't need to clutter everyone's view of the world....

Thank you.

My predecessor thanks you.

+4  A: 

Leave them in the_model.rb until you need them in more than one place. If you refactor needlessly, you're not doing the simplest thing that could possibly work. You aren't gonna need it.

At that point, the general pattern is to create a directory for "concerns". See this weblog post by Jamis Buck or this one by Peter Marklund for more information.

Jim Puls
A: 

In general: follow the Rails naming conventions when translating class names into filesystem locations. (that is: keep the class FooHelper::Bar in foo_helper/bar.rb)

You can make exceptions for small helper classes that are only used once and keep them in the same file as your model, but those should be exceptions. (but the converse is also true, don't create one-line thousands of single line files)

Use modules and class namespaces to your advantage. If you have a helper class that is only used by (and dependent on) your model, put them into the namespace of the model class:

class TheModel::HelperClass
end

the location in the file system would be app/models/the_model/helper_class.rb

And something that is not dependent on your model can probably still be namespaced

module Bar
  class Foo
  end
end

living in bar/foo.rb, of course

You should probably not be afraid to put things that are not models into lib -- that's what this directory is for

I'd say concerns, while useful, are not really the right way to go because that is a way to split a single class into multiple files and you don't seem to be doing that.

levinalex