Enable Layout/ClassStructure rule
Description of the proposal
Related to #69 (closed).
Enable the Layout/ClassStructure
rule.
It checks if the code style follows the ExpectedOrder;
- Module inclusion (include, prepend, extend)
- Constants
- Associations (has_one, has_many)
- Public attribute macros (attr_accessor, attr_writer, attr_reader)
- Other macros (validates, validate)
- Public class methods
- Initializer
- Public instance methods
- Protected attribute macros (attr_accessor, attr_writer, attr_reader)
- Protected instance methods
- Private attribute macros (attr_accessor, attr_writer, attr_reader)
- Private instance methods
Example;
# bad
# Expect extend be before constant
class Person < ApplicationRecord
has_many :orders
ANSWER = 42
extend SomeModule
include AnotherModule
end
# good
class Person
# extend and include go first
extend SomeModule
include AnotherModule
# inner classes
CustomError = Class.new(StandardError)
# constants are next
SOME_CONSTANT = 20
# afterwards we have public attribute macros
attr_reader :name
# followed by other macros (if any)
validates :name
# then we have public delegate macros
delegate :to_s, to: :name
# public class methods are next in line
def self.some_method
end
# initialization goes between class methods and instance methods
def initialize
end
# followed by other public instance methods
def some_method
end
# protected attribute macros and methods go next
protected
attr_reader :protected_name
def some_protected_method
end
# private attribute macros, delegate macros and methods
# are grouped near the end
private
attr_reader :private_name
delegate :some_private_delegate, to: :name
def some_private_method
end
end
Check-list
-
Mention this proposal in the relevant Slack channels (e.g. #development
,#backend
,#frontend
) -
If there is a choice to make between two potential styles, set up an emoji vote in the MR: - CHOICE_A:
🅰 - CHOICE_B:
🅱 - Vote yourself for both choices so that people know these are the choices
- CHOICE_A:
-
The MR doesn't have significant objections, and is getting a majority of 👍 vs👎 (remember that we don't need to reach a consensus) -
(If applicable) One style is getting a majority of vote (compared to the other choice) -
(If applicable) Update the MR with the chosen style -
Follow the review process as usual
/cc @gitlab-org/maintainers/rails-backend