Here is a consolidated list of Naming Conventions in Ruby on Rails, that I have collected from the internet. If anything is missing from the list, please mention it in the comments section below :
| Entity |
Description |
| Class and Module |
- Classes and module names starts with an uppercase letter
- Multiple words are named using MixedCase
eg.: module Encryption, class MixedCase
|
| Instance Methods |
-
Method name should start with a lowercase letter
-
can have lower case letters, digits and underscores
eg.: paint(...), close_the_door(...)
|
| Local Variables |
- All Lowercase letters
- Use underscores instead of camelBack for multiple words
eg.: total, student_name
|
| Instance Variables |
- Begins with '@' sign and then followed by lowercase letters
- Use underscores for multiple words
eg.: @total, @student_name
|
| Global Variables |
-
Starts with a dollar ($) sign followed by other characters
eg.: $global_variable
|
| Class Variables |
-
Class variable names start with a double "at" sign (@@)
-
may be followed by digits, underscores and lower case letters
eg.: @@color
|
| Constants |
-
Constant names start with an uppercase letter followed by other characters.
-
Constant objects are by convention named using all uppercase letters and underscores between words
eg.: THIS_IS_A_CONSTANT
|
| Model |
- Follows the convention of Class names of unbroken MixedCase letters
- Always Singular format of the corresponding Table Name in database
- file name for the Models should always be in lowercase. For Multiple words use '_'
eg.: Order (table name: orders | file name: /app/models/order.rb )
|
| Controller |
- Singularized Table name with the suffix 'Controller'
- file name for the Controller should be in lowercase. Use '_' for multiple words format.
eg.: OrdersController (file: /app/controllers/order_controller.rb)
|
| View |
- File name should strictly have the correspodning action name from the controller
- File name should be in lowercase with underscore for multiple words format
eg.: app/views/order/index.html.erb
|
| Files & Diretories |
- Files and Directories are named using lowercase letters and '_' for multiple words formats
- Proper naming and pluralization is mandatory for the conventional functioning of Rails
eg.: app/helpers/orders_helper.rb, app/models/order.rb
|
| Database Tables |
- Table names have all lowercase letters and underscores between words, also all table names need to be plural
e.g. invoice_items, orders
|
| Primary Key |
- The primary key of a table is assumed to be named id.
|
| Foreign Key |
- The foreign key is named with the singular version of the target table name with _id appended to it
e.g. order_id in the items table where we have items linked to the orders table.
|
| Many to Many Link Tables |
- Tables used to join two tables in a many to many relationship is named using the table names they link, with the table names in alphabetical order
eg.: items_orders
|
| Timestamp Columns |
- You can get ActiveRecord to automatically update the create and update times of records in a database table. To do this create two specially named columns created_at and updated_at to your table,
i.e. t.datetime :created_at and t.datetime :updated_at.
- If you only want to store the date rather than a date and time, use :created_on and :updated_on.
|
No comments:
Post a Comment