Skip to content

Commit c1894f6

Browse files
committed
Add section for hash syntax of enums
Closes #144 Ref: rubocop/rubocop-rails#78
1 parent c0edf66 commit c1894f6

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

README.adoc

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,26 @@ class Transaction < ActiveRecord::Base
385385
end
386386
----
387387

388+
=== Enums [[enums]]
389+
390+
Prefer using the hash syntax for `enum`. Array makes the database values implicit
391+
& any insertion/removal/rearrangement of values in the middle will most probably
392+
lead to broken code.
393+
394+
[source,ruby]
395+
----
396+
class Transaction < ActiveRecord::Base
397+
# bad - implicit values - ordering matters
398+
enum type: %i[credit debit]
399+
400+
# good - explicit values - ordering does not matter
401+
enum type: {
402+
credit: 0,
403+
debit: 1
404+
}
405+
end
406+
----
407+
388408
=== Macro Style Methods [[macro-style-methods]]
389409

390410
Group macro-style methods (`has_many`, `validates`, etc) in the beginning of the class definition.
@@ -403,7 +423,7 @@ class User < ActiveRecord::Base
403423
404424
attr_accessible :login, :first_name, :last_name, :email, :password
405425
406-
# Rails 4+ enums after attr macros, prefer the hash syntax
426+
# Rails 4+ enums after attr macros
407427
enum gender: { female: 0, male: 1 }
408428
409429
# followed by association macros

0 commit comments

Comments
 (0)