Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Rails Lib Folder Naming Convention

I seem to be having trouble with the naming conventions of the Lib Folder in Rails, and the error messages provided to me do not help. [For example, I have received a message saying that XXX::YYY::TextBox is expected to be defined xxx/yyy/text_box.rb, even though it clearly was defined there.] I think I'm getting the convention wrong.

Let's say I am working on YourModule::MyModule::MyClass. I clearly get that this file should be located in

lib/your_module/my_module/my_class.rb

But what should the actual file here look like? Which one of these (if either) are correct?

#your_module/my_module/my_class.rb
module YourModule
  module MyModule
    class MyClass
       ...
    end
  end
end

Or

#your_module/my_module/my_class.rb
class MyClass
  ...
end

In other words, do I need to nest the class inside of the module structure or not?

like image 896
Jim Pedid Avatar asked Apr 15 '12 22:04

Jim Pedid


People also ask

What goes in the Rails lib folder?

In Rails's directory structure as far back as I can recall, there's always been a lib folder. This folder is for files that don't belong in the app folder (not controllers, helpers, mailers, models, observers or views), such as modules that are included into other areas of the application.

What belongs in the lib folder?

The lib folder is a library files directory which contains all helpful library files used by the system. In simple terms, these are helpful files which are used by an application or a command or a process for their proper execution. The commands in /bin or /sbin dynamic library files are located just in this directory.

What's the proper convention for naming variables in Ruby?

Variable names in Ruby can be created from alphanumeric characters and the underscore _ character. A variable cannot begin with a number. This makes it easier for the interpreter to distinguish a literal number from a variable. Variable names cannot begin with a capital letter.

What is naming convention in Rails?

Naming conventions in Active Record model Rails is capable of pluralizing (and singularizing) both regular and irregular words. Model class names must use the CamelCase form when composed of two or more words, while the database table names must use the snake_case form.


1 Answers

The lib folder has few conventions, as it is not autoloaded. So, how you organize the files is up to you, but you do have to name the classes correctly. Your first example is correct.

To get the files included you need to specify you want them in your application.rb file, see this example: Best way to load module/class from lib folder in Rails 3?

I would recommend making a folder just called lib/modules, since you probably won't have very many. Name the file my_class.rb. Then in application.rb you need:

config.autoload_paths += %W(#{config.root}/lib/modules)

That should take care of your issue.

like image 172
Andrew Avatar answered Oct 09 '22 15:10

Andrew