trabajo-afectivo/app/models/email_address.rb

89 lines
2.4 KiB
Ruby
Raw Normal View History

2016-10-19 03:11:36 +00:00
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
class EmailAddress < ApplicationModel
include ChecksLatestChangeObserved
has_many :groups, after_add: :cache_update, after_remove: :cache_update
belongs_to :channel
validates :realname, presence: true
validates :email, presence: true
before_validation :check_email
2015-09-11 13:44:05 +00:00
before_create :check_if_channel_exists_set_inactive
before_update :check_if_channel_exists_set_inactive
after_create :update_email_address_id
after_update :update_email_address_id
before_destroy :delete_group_reference
=begin
check and if channel not exists reset configured channels for email addresses
EmailAddress.channel_cleanup
=end
def self.channel_cleanup
EmailAddress.all.each do |email_address|
2015-09-11 13:44:05 +00:00
# set to active if channel exists
if email_address.channel_id && Channel.find_by(id: email_address.channel_id)
if !email_address.active
2017-05-02 12:11:59 +00:00
email_address.save!
end
next
end
2015-09-11 13:44:05 +00:00
# set in inactive if channel not longer exists
next if !email_address.active
2017-05-02 12:11:59 +00:00
email_address.save!
end
end
private
def check_email
return true if Setting.get('import_mode')
return true if email.blank?
self.email = email.downcase.strip
raise Exceptions::UnprocessableEntity, 'Invalid email' if email !~ /@/
2017-11-23 08:09:44 +00:00
raise Exceptions::UnprocessableEntity, 'Invalid email' if email.match?(/\s/)
true
end
2015-09-11 13:44:05 +00:00
# set email address to inactive/active if channel exists or not
def check_if_channel_exists_set_inactive
# set to active if channel exists
if channel_id && Channel.find_by(id: channel_id)
self.active = true
return true
end
2015-09-11 13:44:05 +00:00
# set in inactive if channel not longer exists
self.channel_id = nil
self.active = false
true
end
2015-09-11 13:44:05 +00:00
# delete group.email_address_id reference if email address get's deleted
def delete_group_reference
Group.where(email_address_id: id).each do |group|
group.update!(email_address_id: nil)
end
2015-09-11 13:44:05 +00:00
end
# keep email email address is of inital group filled
def update_email_address_id
not_configured = Group.where(email_address_id: nil).count
total = Group.count
2016-07-26 22:02:28 +00:00
return if not_configured.zero?
return if total != 1
group = Group.find_by(email_address_id: nil)
group.email_address_id = id
group.updated_by_id = updated_by_id
2017-05-02 12:11:59 +00:00
group.save!
end
end