email_address.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class EmailAddress < ApplicationModel
  3. has_many :groups, after_add: :cache_update, after_remove: :cache_update
  4. belongs_to :channel
  5. validates :realname, presence: true
  6. validates :email, presence: true
  7. before_create :check_if_channel_exists_set_inactive
  8. before_update :check_if_channel_exists_set_inactive
  9. after_destroy :delete_group_reference
  10. latest_change_support
  11. =begin
  12. check and if channel not exists reset configured channels for email addresses
  13. EmailAddress.channel_cleanup
  14. =end
  15. def self.channel_cleanup
  16. EmailAddress.all.each {|email_address|
  17. # set to active if channel exists
  18. if email_address.channel_id && Channel.find_by(id: email_address.channel_id)
  19. if !email_address.active
  20. email_address.save
  21. end
  22. next
  23. end
  24. # set in inactive if channel not longer exists
  25. next if !email_address.active
  26. email_address.save
  27. }
  28. end
  29. private
  30. # set email address to inactive/active if channel exists or not
  31. def check_if_channel_exists_set_inactive
  32. # set to active if channel exists
  33. if channel_id && Channel.find_by(id: channel_id)
  34. self.active = true
  35. return true
  36. end
  37. # set in inactive if channel not longer exists
  38. self.channel_id = nil
  39. self.active = false
  40. true
  41. end
  42. # delete group.email_address_id reference if email address get's deleted
  43. def delete_group_reference
  44. Group.where(email_address_id: id).each { |group|
  45. group.email_address_id = nil
  46. }
  47. end
  48. end