1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
lpr-partynest/app/models/contact_network.rb

45 lines
855 B
Ruby
Raw Normal View History

2019-08-04 17:05:57 -04:00
# frozen_string_literal: true
class ContactNetwork < ApplicationRecord
CODENAME_RE = /\A[a-z][a-z0-9]*(_[a-z0-9]+)*\z/.freeze
2019-08-04 17:05:57 -04:00
#############
# Callbacks #
#############
before_validation :turn_blanks_into_nils
before_validation :strip_extra_spaces
###############
# Validations #
###############
validates :codename,
2019-08-04 17:05:57 -04:00
presence: true,
length: { in: 3..36 },
format: CODENAME_RE,
2019-08-04 17:05:57 -04:00
uniqueness: { case_sensitive: false }
validates :name, allow_nil: true, length: { in: 1..255 }
2019-08-04 17:05:57 -04:00
2019-08-17 20:03:18 -04:00
###########
# Methods #
###########
def to_param
codename
end
2019-08-04 17:05:57 -04:00
private
def turn_blanks_into_nils
self.codename = nil if codename.blank?
self.name = nil if name.blank?
2019-08-04 17:05:57 -04:00
end
def strip_extra_spaces
self.codename = codename&.strip
self.name = name&.strip
2019-08-04 17:05:57 -04:00
end
end