#-- # Copyright (c) 2012+ Damjan Rems # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #++ class DcMailAddress include Mongoid::Document include Mongoid::Timestamps field :email, type: String field :reason, type: String field :dc_user_id, type: BSON::ObjectId field :active, type: Boolean, default: true field :created_by, type: BSON::ObjectId field :updated_by, type: BSON::ObjectId embeds_many :dc_mail_list_members belongs_to :dc_site index( { 'dc_mail_list_members.dc_mail_list_id' => 1 } ) validate :if_email_is_ok =begin def if_email_is_ok if dc_user_id.nil? unless email.to_s =~ /^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/ errors.add(:email, 'is not an email') end end end =end def if_email_is_ok if dc_user_id.nil? and !DcMailAddress::dc_is_email?(email) errors.add(:email, I18n.t('drgcms.not_email')) end end ############################################################################ # Check if string contains valid e-mail address. # # @example usage from program # dc_is_email?(some_email) # eval: name4_id('site','name',@record['site_id']) # # @param [ email ] email address to be checked ############################################################################ def self.dc_is_email?(email) email.to_s =~ /^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/ end end