class Project < ApplicationRecord include Auditable has_many :user_projects, dependent: :destroy has_many :users, through: :user_projects has_many :organization_projects, dependent: :destroy has_many :organizations, through: :organization_projects has_many :opportunities, dependent: :restrict_with_exception has_many :sales_goals, dependent: :nullify has_many :mail_templates, dependent: :destroy has_many :mailings, dependent: :destroy validates :name, :code, presence: true validates :code, uniqueness: { case_sensitive: false }, format: { with: /\A[a-z0-9_-]+\z/, message: "usa solo lettere minuscole, numeri, - e _" } before_validation :normalize_code scope :active, -> { where(active: true) } scope :ordered, -> { order(:position, :name) } def to_s name end private def normalize_code self.code = code.to_s.strip.downcase.parameterize(separator: "_") end end