Class: WcoEmail::EmailFilter

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Paranoia, Mongoid::Timestamps
Defined in:
app/models/wco_email/email_filter.rb

Overview

2023-03-04 vp When I receive one.

Constant Summary collapse

PAGE_PARAM_NAME =
:filters_page
KIND_AUTORESPOND_TMPL =
'autorespond-template'
KIND_AUTORESPOND_EACT =
'autorespond-email-action'
KIND_REMOVE_TAG =
'remove-tag'
KIND_ADD_TAG =
'add-tag'
KIND_DESTROY_SCHS =
'destroy-schs'
KIND_AUTORESPOND =
Deprecated.
'autorespond'
KIND_DELETE =

@deprecated, DO NOT USE!

'delete'
KIND_SKIP_INBOX =

@deprecated, use add-tag

'skip-inbox'
KINDS =

@deprecated, use remove-tag

[ nil, KIND_AUTORESPOND_TMPL, KIND_AUTORESPOND_EACT, KIND_ADD_TAG, KIND_REMOVE_TAG, KIND_DESTROY_SCHS]

Instance Method Summary collapse

Instance Method Details

#conditionsObject

‘and’ - all conditions must match, for filter to match



34
# File 'app/models/wco_email/email_filter.rb', line 34

has_many :conditions,      class_name: '::WcoEmail::EmailFilterCondition', inverse_of: :email_filter

#conversationsObject

@TODO: change to has_and_belongs_to_many, test-driven.



72
# File 'app/models/wco_email/email_filter.rb', line 72

has_many :conversations, class_name: '::WcoEmail::Conversation', inverse_of: :filter

#skip_conditionsObject

‘and’ - all conditions must match, for filter to match



38
# File 'app/models/wco_email/email_filter.rb', line 38

has_many :skip_conditions, class_name: '::WcoEmail::EmailFilterCondition', inverse_of: :email_skip_filter

#to_sObject



74
75
76
# File 'app/models/wco_email/email_filter.rb', line 74

def to_s
  "EmailFilter: #{from_regex} #{from_exact} #{conditions.map { |c| c.to_s }.join }"
end

#to_s_fullObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/wco_email/email_filter.rb', line 77

def to_s_full
  # inn = ""
  # inn = "#{inn}#{conditions.map { |c| c.to_s_full }.join }" if conditions.present?
  # inn = "#{inn}#{skip_conditions.map { |c| c.to_s_full }.join }" if skip_conditions.present?
  out =<<-AOL
<EmailFilter #{from_regex} #{from_exact}>
#{conditions.map { |c| c.to_s_full( indent: 2) }.join }
#{skip_conditions.map { |c| c.to_s_full( indent: 2) }.join }
#{actions.map { |c| c.to_s_full( indent: 2) }.join }
</EmailFilter>"
AOL
  while out.match(/\n\n/) do
    out = out.gsub(/\n\n/, "\n")
  end
  out
end

#to_xmlObject



94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/wco_email/email_filter.rb', line 94

def to_xml
  attrs = ''
  children = ''
  if from_regex || from_exact
    attrs = "#{attrs} from=#{from_regex}#{from_exact}"
  end
  if conditions.present?
    children = "#{children}#{conditions.map { |c| c.to_s }.join('') }"
  end
  return "<EF #{attrs}>#{children}</EF>\n"
end

#validate_actionsObject

validate :validate_actions



27
28
29
30
31
# File 'app/models/wco_email/email_filter.rb', line 27

def validate_actions
  if actions.length == 0
    errors.add(:actions, 'must be present')
  end
end

#validate_conditionsObject

validate :validate_conditions



42
43
44
45
46
# File 'app/models/wco_email/email_filter.rb', line 42

def validate_conditions
  if conditions.length + skip_conditions.length == 0
    errors.add(:conditions, 'Either conditions or skip conditions must be present.')
  end
end