2021-08-20 05:09:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class CustomerRelations::Contact < ApplicationRecord
|
|
|
|
include StripAttribute
|
|
|
|
|
|
|
|
self.table_name = "customer_relations_contacts"
|
|
|
|
|
2021-09-22 08:12:04 -04:00
|
|
|
belongs_to :group, -> { where(type: Group.sti_name) }, foreign_key: 'group_id'
|
2021-08-20 05:09:16 -04:00
|
|
|
belongs_to :organization, optional: true
|
2021-10-19 11:12:08 -04:00
|
|
|
has_many :issue_contacts, inverse_of: :contact
|
|
|
|
has_many :issues, through: :issue_contacts, inverse_of: :customer_relations_contacts
|
2021-08-20 05:09:16 -04:00
|
|
|
|
|
|
|
strip_attributes! :phone, :first_name, :last_name
|
|
|
|
|
|
|
|
enum state: {
|
|
|
|
inactive: 0,
|
|
|
|
active: 1
|
|
|
|
}
|
|
|
|
|
|
|
|
validates :group, presence: true
|
|
|
|
validates :phone, length: { maximum: 32 }
|
|
|
|
validates :first_name, presence: true, length: { maximum: 255 }
|
|
|
|
validates :last_name, presence: true, length: { maximum: 255 }
|
|
|
|
validates :email, length: { maximum: 255 }
|
|
|
|
validates :description, length: { maximum: 1024 }
|
|
|
|
validate :validate_email_format
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def validate_email_format
|
|
|
|
return unless email
|
|
|
|
|
|
|
|
self.errors.add(:email, I18n.t(:invalid, scope: 'valid_email.validations.email')) unless ValidateEmail.valid?(self.email)
|
|
|
|
end
|
|
|
|
end
|