Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How add new folder with class in rails app?

I have 2 questions in rails app context:

I have some classes which aren't "modele", but require in my sytem, so I want separe them

1) How can I add "class' folder in app/? (if I create it and put classes, their are no included)

2) how can I put folder "model" in "app/class" folder (same thing here, the model are not included if I move it)

thx.

like image 804
Matrix Avatar asked Oct 16 '14 14:10

Matrix


2 Answers

It´s kind of unclear what you are asking.

But if you want to autoload additional directories you can do it by placing something like this in config/application.rb

config.autoload_paths << Rails.root.join('app/class')

But please don´t call your directory class, use something descriptive instead.

By convention code that does not fit inside models, controllers, views, helpers or concerns and placed in the lib directory at the project root.


Edit:

You can load subdirectories by using a glob:

config.autoload_paths << Rails.root.join('app/classes/**/')
like image 76
max Avatar answered Oct 26 '22 23:10

max


For quite some time Rails has autoloaded all paths under /app, as mentioned here

You may have run into a problem when using a "app/class" directory since "class" is a reserved word and "Class" is a class in Ruby.

There is a problem with your example:

exemple: "app/classes/effects/attribute.rb" with "class Effect::Attribute"

Notice that in the file path "effects" has an "s" at the end, whereas your module name does not "Effect::Atttribute". Those should match. Either both end with "s" or not, and when they do match Rails autoloading should work.

You should remove any of the other suggestions about appending to config.autoload_paths.

like image 31
Wizard of Ogz Avatar answered Oct 26 '22 23:10

Wizard of Ogz