model_probe icon indicating copy to clipboard operation
model_probe copied to clipboard

Tweaking print_model to be more similar to Rubocop?

Open nickjj opened this issue 2 years ago • 1 comments

Rubocop's suggested ordering is:

class User < ApplicationRecord
  # keep the default scope first (if any)
  default_scope { where(active: true) }

  # constants come up next
  COLORS = %w(red green blue)

  # afterwards we put attr related macros
  attr_accessor :formatted_date_of_birth

  attr_accessible :login, :first_name, :last_name, :email, :password

  # Rails 4+ enums after attr macros
  enum role: { user: 0, moderator: 1, admin: 2 }

  # followed by association macros
  belongs_to :country

  has_many :authentications, dependent: :destroy

  # and validation macros
  validates :email, presence: true
  validates :username, presence: true
  validates :username, uniqueness: { case_sensitive: false }
  validates :username, format: { with: /\A[A-Za-z][A-Za-z0-9._-]{2,19}\z/ }
  validates :password, format: { with: /\A\S{8,128}\z/, allow_nil: true }

  # next we have callbacks
  before_save :cook
  before_save :update_username_lower

  # other macros (like devise's) should be placed after the callbacks

  ...
end

What do you think about these adjustments?

class User < ApplicationRecord
  # extends ...................................................................

  # includes ..................................................................

  # constants .................................................................

  # attrs .....................................................................

  # enums .....................................................................

  # associations ..............................................................

  # validations ...............................................................

  # callbacks .................................................................

  # scopes ....................................................................

  # additional config (accepts_nested_attribute_for, etc.) ....................

  # class methods .............................................................
  class << self
  end

  # public instance methods ...................................................

  # protected instance methods ................................................

  # private instance methods ..................................................
end

Proposed changes:

  • Decrease ... to max out at 79 chars
  • Added attrs
  • Added enums
  • Moved class methods above instance methods
  • Renamed relationships to associations
  • Removed (...) help for callbacks and scopes
    • Thought process: These would end up being removed all the time, but having it kept for additional config seems handy

Let me know and I'll open a PR if you're interested.

nickjj avatar Jun 19 '23 17:06 nickjj

I like this. Feel free to create a PR.

hopsoft avatar Dec 30 '23 17:12 hopsoft