Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enums in Rails: uppercase or lowercase?

With most mentions of JAVA enums on the internet, it is everywhere mentioned that enums should be all uppercase (ex: ACTIVE).

Like here: Coding Conventions - Naming Enums

But when it comes to Rails, in all there examples and docs they use lowercase enum value (ex: 'active'), like here: http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html

which makes sense since rails also provides instance methods by the name of these enums (eg: obj.active?). Is this the only reason why enums in Rails are used as lowercase, or is there more to it? Also we differ from the convention when we use enums as lowercase, should this be the case? or shall we use uppercase enums in Rails as well?

So for example I have a status enum in my model, which can either be active, draft or inactive as per convention, should it be:

enum status: {active: 1, draft: 2, inactive: 3}

or:

enum status: {ACTIVE: 1, DRAFT: 2, INACTIVE: 3}

Which one and why?

like image 976
Sambhav Sharma Avatar asked Oct 20 '16 10:10

Sambhav Sharma


People also ask

Should enum be uppercase or lowercase?

Because they are constants, the names of an enum type's fields are in uppercase letters. You should use enum types any time you need to represent a fixed set of constants.

Can enum values be lowercase?

In Java, it's considered good practice to define enum values with uppercase letters, as they are constants. However, we may want to support lowercase letters in the request URL.

Should enums be camel case?

Enums are types, so they should be named using UpperCamelCase like classes. The enum values are constants, so they should be named using lowerCamelCase like constants, or ALL_CAPS if your code uses that legacy naming style.

How does enum work in Rails?

An enum is an attribute where the values map to integers in the database and can be queried by name. Enums also give us the ability to change the state of the data very quickly. This makes it easy to use enums in Rails and saves a lot of time by providing dynamic methods.


2 Answers

In Rails enums should be snake_case.

Why? Because enums are used to construct method names. And method names in Ruby should be snake_case according to the community convention. Using ALLCAPS or CamelCase can lead to bugs and confusion as Ruby treats such identifiers as constants.

ActiveRecord::Enum is not comparable to a language level enumeration construct such as in Java.

Declare an enum attribute where the values map to integers in the database, but can be queried by name.
http://api.rubyonrails.org/classes/ActiveRecord/Enum.html

The keys mappings in an ActiveRecord::Enum are not constants. Rather it's just a list which is used with Ruby style metaprogramming that adds methods to make bitmask columns easier (and more fun) to work with.

In your example its actually comparable to:

enum status: [ :ACTIVE, :DRAFT, :INACTIVE ] 

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.
https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

Enums in Java are basically glorified switch statements where the class constants denote the possible values.

like image 68
max Avatar answered Sep 21 '22 18:09

max


which makes sense since rails also provides instance methods by the name of these enums (eg: obj.active?). Is this the only reason why enums in RAILS are used as lowercase

I think it's the main reason, yes. It's highly unidiomatic in ruby to have methods like obj.Active? and obj.ACTIVE?. One doesn't need any more reason. :)

like image 32
Sergio Tulentsev Avatar answered Sep 22 '22 18:09

Sergio Tulentsev