From c50452aec6b373441793bccf0babbd5452f99daf Mon Sep 17 00:00:00 2001 From: taitus Date: Tue, 8 Oct 2024 16:47:04 +0200 Subject: [PATCH] Add and apply Rails/EnumHash rubocop rule In rubocop-rails 2.26.0, support was added for Rails 7 syntax in the Rails/EnumHash rule. We took this opportunity to ensure consistency by converting all enums to hash with integer values. This format minimizes the risk of data consistency issues in the database when adding new values. --- .rubocop.yml | 3 +++ app/models/dashboard/action.rb | 2 +- app/models/progress_bar.rb | 2 +- app/models/sdg/phase.rb | 2 +- app/models/tenant.rb | 2 +- app/models/votation_type.rb | 2 +- 6 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 088cfb508..50a37133e 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -388,6 +388,9 @@ Rails/DynamicFindBy: - find_by_slug_or_id! - find_by_manager_login +Rails/EnumHash: + Enabled: true + Rails/EnumSyntax: Enabled: true diff --git a/app/models/dashboard/action.rb b/app/models/dashboard/action.rb index bd8391ecb..ca625a900 100644 --- a/app/models/dashboard/action.rb +++ b/app/models/dashboard/action.rb @@ -9,7 +9,7 @@ class Dashboard::Action < ApplicationRecord class_name: "Dashboard::ExecutedAction" has_many :proposals, through: :executed_actions - enum :action_type, [:proposed_action, :resource] + enum :action_type, { proposed_action: 0, resource: 1 } validates :title, presence: true, allow_blank: false, length: { in: 4..80 } diff --git a/app/models/progress_bar.rb b/app/models/progress_bar.rb index 8852b6549..d79f61b8f 100644 --- a/app/models/progress_bar.rb +++ b/app/models/progress_bar.rb @@ -2,7 +2,7 @@ class ProgressBar < ApplicationRecord self.inheritance_column = nil RANGE = (0..100) - enum :kind, %i[primary secondary] + enum :kind, { primary: 0, secondary: 1 } belongs_to :progressable, polymorphic: true diff --git a/app/models/sdg/phase.rb b/app/models/sdg/phase.rb index 3108c06a8..4a57fb531 100644 --- a/app/models/sdg/phase.rb +++ b/app/models/sdg/phase.rb @@ -1,6 +1,6 @@ class SDG::Phase < ApplicationRecord include Cardable - enum :kind, %w[sensitization planning monitoring] + enum :kind, { sensitization: 0, planning: 1, monitoring: 2 } validates :kind, presence: true, uniqueness: true def self.[](kind) diff --git a/app/models/tenant.rb b/app/models/tenant.rb index f03136b98..6c1bcac80 100644 --- a/app/models/tenant.rb +++ b/app/models/tenant.rb @@ -1,5 +1,5 @@ class Tenant < ApplicationRecord - enum :schema_type, %w[subdomain domain] + enum :schema_type, { subdomain: 0, domain: 1 } validates :schema, presence: true, diff --git a/app/models/votation_type.rb b/app/models/votation_type.rb index de1fac364..71d61c62d 100644 --- a/app/models/votation_type.rb +++ b/app/models/votation_type.rb @@ -3,7 +3,7 @@ class VotationType < ApplicationRecord QUESTIONABLE_TYPES = %w[Poll::Question].freeze - enum :vote_type, %w[unique multiple] + enum :vote_type, { unique: 0, multiple: 1 } validates :questionable, presence: true validates :questionable_type, inclusion: { in: ->(*) { QUESTIONABLE_TYPES }}