Class: WcoEmail::EmailFilter

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

Constant Summary collapse

PAGE_PARAM_NAME =
:filters_page
FIELD_OPTS =

‘lead_tag_id’

[ 'subject', 'from', 'to', 'to_and_cc', 'body', ]
MATCHTYPE_OPTS =
[ 'regex', 'exact_insensitive' ]
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_OAT =
'office-action'
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_OAT, 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



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

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

#conversationsObject

@TODO: change to has_and_belongs_to_many, test-driven.



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

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

#office_action_templateObject

no habtm please has_and_belongs_to_many :action_tmpls, class_name: ‘::Wco::OfficeActionTemplate’



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

belongs_to :office_action_template, class_name: '::Wco::OfficeActionTemplate', optional: true

#office_email_filtersObject

‘wco_email_email_filters’



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

store_in collection: 'office_email_filters'

#skip_conditionsObject

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



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

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

#to_sObject



79
80
81
# File 'app/models/wco_email/email_filter.rb', line 79

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

#to_s_fullObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/wco_email/email_filter.rb', line 82

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



99
100
101
102
103
104
105
106
107
108
109
# File 'app/models/wco_email/email_filter.rb', line 99

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



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

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

#validate_conditionsObject

validate :validate_conditions



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

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