Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subfolders in lib

I have a module called user_searches. It performs some searches that aren't core to the user model, thus, why I'm putting the responsibility somewhere else. I want to organize all my models like this that perform non-core user functions in a lib subfolder called user. Right now to include the module's methods in the User model I have to put...

require 'user/user_searches'

class User < ActiveRecord::Base

  include UserSearches

end

...I don't need the require if the file is directly in the lib folder, but do if it's in the subfolder. What do I have to do so I don't need the require?

like image 575
keruilin Avatar asked Feb 12 '11 23:02

keruilin


People also ask

What are the subfolders?

Definition of subfolder : an organizational folder on a computer that is located within another folder …

What is a folder with subfolders called?

Directory: A named group of files (a folder); a directory that contains more folders (subdirectories) are called the "parent" and the folder within it is referred to as the "child" of that directory..

What is a subfolder example?

A subfolder is housed on the same server and any of its link juice goes back to the domain. An example of a subfolder web address is https://www.impactplus.com/blog. With this structure, all of the pages associated with IMPACT's blog feed the authority of the main domain.

What is parent folder and subfolders?

A folder that contains subfolders is referred to as a parent folder. Of the list of top-level folders, only Contracts is a parent folder, as indicated by the little arrow to the left of the folder name.


1 Answers

You could put the necessary require lines into lib/user.rb that way, all requirements are loaded recursively on application launch.

Alternatively, you could put something like this into an initializer:

# put into config/initializers/load_lib.rb
Dir["#{RAILS_ROOT}/lib/**/*.rb"].each { |f| require(f) }

It will require all ruby files in your lib folder. You just have to make sure if this is really what you want :)

like image 100
Holger Just Avatar answered Oct 13 '22 23:10

Holger Just