diff options
Diffstat (limited to 'app/models/customer_relations')
-rw-r--r-- | app/models/customer_relations/contact.rb | 33 | ||||
-rw-r--r-- | app/models/customer_relations/organization.rb | 10 |
2 files changed, 36 insertions, 7 deletions
diff --git a/app/models/customer_relations/contact.rb b/app/models/customer_relations/contact.rb new file mode 100644 index 00000000000..aaa7e2ae175 --- /dev/null +++ b/app/models/customer_relations/contact.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +class CustomerRelations::Contact < ApplicationRecord + include StripAttribute + + self.table_name = "customer_relations_contacts" + + belongs_to :group, -> { where(type: 'Group') }, foreign_key: 'group_id' + belongs_to :organization, optional: true + + 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 diff --git a/app/models/customer_relations/organization.rb b/app/models/customer_relations/organization.rb index caf1cd68cc5..a18d3ab8148 100644 --- a/app/models/customer_relations/organization.rb +++ b/app/models/customer_relations/organization.rb @@ -1,11 +1,13 @@ # frozen_string_literal: true class CustomerRelations::Organization < ApplicationRecord + include StripAttribute + self.table_name = "customer_relations_organizations" belongs_to :group, -> { where(type: 'Group') }, foreign_key: 'group_id' - before_validation :strip_whitespace! + strip_attributes! :name enum state: { inactive: 0, @@ -22,10 +24,4 @@ class CustomerRelations::Organization < ApplicationRecord where(group: group_id) .where('LOWER(name) = LOWER(?)', name) end - - private - - def strip_whitespace! - name&.strip! - end end |