<commit_before># Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/

class EmailAddress < ApplicationModel
  include LatestChangeObserved

  has_many        :groups,   after_add: :cache_update, after_remove: :cache_update
  belongs_to      :channel
  validates       :realname, presence: true
  validates       :email,    presence: true

  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
  after_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 { |email_address|

      # set to active if channel exists
      if email_address.channel_id && Channel.find_by(id: email_address.channel_id)
        if !email_address.active
          email_address.save
        end
        next
      end

      # set in inactive if channel not longer exists
      next if !email_address.active
      email_address.save
    }
  end

  private

  # 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

    # set in inactive if channel not longer exists
    self.channel_id = nil
    self.active = false
    true
  end

  # delete group.email_address_id reference if email address get's deleted
  def delete_group_reference
    Group.where(email_address_id: id).each { |group|
      group.email_address_id = nil
    }
  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
    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
    group.save
  end

end
<commit_msg>Improved error handling.<commit_after># Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/

class EmailAddress < ApplicationModel
  include LatestChangeObserved

  has_many        :groups,   after_add: :cache_update, after_remove: :cache_update
  belongs_to      :channel
  validates       :realname, presence: true
  validates       :email,    presence: true

  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
  after_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 { |email_address|

      # set to active if channel exists
      if email_address.channel_id && Channel.find_by(id: email_address.channel_id)
        if !email_address.active
          email_address.save!
        end
        next
      end

      # set in inactive if channel not longer exists
      next if !email_address.active
      email_address.save!
    }
  end

  private

  # 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

    # set in inactive if channel not longer exists
    self.channel_id = nil
    self.active = false
    true
  end

  # delete group.email_address_id reference if email address get's deleted
  def delete_group_reference
    Group.where(email_address_id: id).each { |group|
      group.email_address_id = nil
    }
  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
    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
    group.save!
  end

end

{'fraction_non_alphanumeric': 0.054015969938938466, 'fraction_numerical': 0.004227336777829967, 'mean_word_length': 3.216831683168317, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7629782', 'n_tokens_mistral': 1308, 'n_tokens_neox': 1258, 'n_words': 356}