Pierre Michard
Pierre Michard

Reputation: 1391

Where do I put my class files which are neither model, view nor controller?

I have a rails application. For one of my models, I created a specific class which will probably never be used anywhere else than in this specific model.

Where should I place the file corresponding to this class and its corresponding test file?

I currently placed it in /lib but since it will never be used elsewhere, I don't think that this is the right place for it.

Upvotes: 0

Views: 109

Answers (2)

Chetan
Chetan

Reputation: 140

As @jstim says there is no specific way.

But you can use the presenters or decorates for this

In app directory create a folder as presenters or decorators.

Create a file some_file.eb In this you can create the class and use it in any controller you want.

app/presenters/some_file.eb

class ClassName
  def your_method
  end
end

Upvotes: 0

jstim
jstim

Reputation: 2432

I don't think there's a categorically correct answer to this question, because the class can be put in a few different locations that all make it available to the program and it's more a matter of organizational preference.

tl;dr lib is just fine.

Options:

  1. models directory
    • If the class is related to the program domain, it may make sense to put it here. You could even namespace it and put it in a directory to get it out of the main dir and organize it relative to its function.
    • app/models/user/password.rb for User::Password
  2. lib directory
    • If the class is orthogonal to the program domain, lib might be the right place. Domain specific stuff could also go here if it's clogging your models dir.
    • lib/soap_client.rb
  3. in the file for the model that uses it
    • Probably not the best option, since it hides the classes existence (if you're looking at the directory structure)
    • For something like a custom Exception class or the like perhaps?

Upvotes: 3

Related Questions