Apply Style/ClassVars rubocop rule

Class variables in Ruby are not the same as instance variables of a
class. They're particularly tricky when it comes to inheritance and
modules.

In the case of the Measurable module, for example, using a class
variable will make all classes including the Measurable module share
the same value. However, that's not what we want; we want the variable
to be different for every class. And that's accomplished using a class
instance variable.

Although in this case it would probably be better to remove the caching
variable. I don't think these methods are called more than once during a
request, and even if they did it's highly unlikely the would become a
bottleneck.
This commit is contained in:
Javi Martín
2019-10-26 00:33:57 +02:00
parent dfe9ab2c69
commit e3bfcbcd25
4 changed files with 8 additions and 5 deletions

View File

@@ -376,6 +376,9 @@ Style/BlockDelimiters:
Style/ClassCheck: Style/ClassCheck:
Enabled: true Enabled: true
Style/ClassVars:
Enabled: true
Style/Not: Style/Not:
Enabled: true Enabled: true

View File

@@ -3,11 +3,11 @@ module Measurable
class_methods do class_methods do
def title_max_length def title_max_length
@@title_max_length ||= (columns.find { |c| c.name == "title" }.limit rescue nil) || 80 @title_max_length ||= (columns.find { |c| c.name == "title" }.limit rescue nil) || 80
end end
def responsible_name_max_length def responsible_name_max_length
@@responsible_name_max_length ||= (columns.find { |c| c.name == "responsible_name" }.limit rescue nil) || 60 @responsible_name_max_length ||= (columns.find { |c| c.name == "responsible_name" }.limit rescue nil) || 60
end end
def question_max_length def question_max_length

View File

@@ -42,11 +42,11 @@ class Organization < ApplicationRecord
end end
def self.name_max_length def self.name_max_length
@@name_max_length ||= columns.find { |c| c.name == "name" }.limit || 60 @name_max_length ||= columns.find { |c| c.name == "name" }.limit || 60
end end
def self.responsible_name_max_length def self.responsible_name_max_length
@@responsible_name_max_length ||= columns.find { |c| c.name == "responsible_name" }.limit || 60 @responsible_name_max_length ||= columns.find { |c| c.name == "responsible_name" }.limit || 60
end end
private private

View File

@@ -313,7 +313,7 @@ class User < ApplicationRecord
end end
def self.username_max_length def self.username_max_length
@@username_max_length ||= columns.find { |c| c.name == "username" }.limit || 60 @username_max_length ||= columns.find { |c| c.name == "username" }.limit || 60
end end
def self.minimum_required_age def self.minimum_required_age