From e548d148596bbb758ea89eca5b49229538060664 Mon Sep 17 00:00:00 2001 From: kevin Date: Mon, 8 Jul 2019 16:49:07 +0530 Subject: [PATCH] Add section for hash syntax of enums Closes https://github.com/rubocop-hq/rails-style-guide/issues/144 Ref: https://github.com/rubocop-hq/rubocop-rails/issues/78 --- README.adoc | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/README.adoc b/README.adoc index c9080ff..d43c87c 100644 --- a/README.adoc +++ b/README.adoc @@ -385,6 +385,26 @@ class Transaction < ActiveRecord::Base end ---- +=== Enums [[enums]] + +Prefer using the hash syntax for `enum`. Array makes the database values implicit +& any insertion/removal/rearrangement of values in the middle will most probably +lead to broken code. + +[source,ruby] +---- +class Transaction < ActiveRecord::Base + # bad - implicit values - ordering matters + enum type: %i[credit debit] + + # good - explicit values - ordering does not matter + enum type: { + credit: 0, + debit: 1 + } +end +---- + === Macro Style Methods [[macro-style-methods]] Group macro-style methods (`has_many`, `validates`, etc) in the beginning of the class definition. @@ -403,7 +423,7 @@ class User < ActiveRecord::Base attr_accessible :login, :first_name, :last_name, :email, :password - # Rails 4+ enums after attr macros, prefer the hash syntax + # Rails 4+ enums after attr macros enum gender: { female: 0, male: 1 } # followed by association macros