Class: WcoEmail::EmailFilterCondition

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

Constant Summary collapse

FIELD_BODY =
'body'
FIELD_BODY_PLAIN =
'body-plain'
FIELD_FROM =
'from'
FIELD_LEADSET =
'from-leadset'
FIELD_SUBJECT =
'subject'
FIELD_TAGGED =

either lead or leadset, actually

'leadset-tagged'
FIELD_NOT_TAGGED =

either lead or leadset, actually

'leadset-not-tagged'
FIELD_TO_OR_CC =
'to-or-cc'
FIELD_OPTS =
[
  FIELD_BODY, FIELD_BODY_PLAIN,
  FIELD_FROM,
  FIELD_LEADSET,
  FIELD_SUBJECT,
  FIELD_TAGGED,
  FIELD_NOT_TAGGED,
  FIELD_TO_OR_CC,
]
OPERATOR_EQUALS =
'eq-i'
OPERATOR_HAS_TAG =
'has-tag'
OPERATOR_NOT_HAS_TAG =
'not-has-tag'
OPERATOR_REGEX =
'regex'
OPERATOR_MATCH =
'match-i'
OPERATOR__ID =
'_id'
OPERATOR_SLUG =
'slug'
OPERATOR_OPTS =
[ OPERATOR__ID,
OPERATOR_EQUALS,
OPERATOR_HAS_TAG,
OPERATOR_MATCH,
OPERATOR_NOT_HAS_TAG,
OPERATOR_REGEX,
OPERATOR_SLUG, ]

Instance Method Summary collapse

Instance Method Details

#apply(lead:, message:) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/models/wco_email/email_filter_condition.rb', line 58

def apply lead:, message:
  cond = self
  reason = nil
  case cond.field

  ## from match-i <value>
  when WcoEmail::EmailFilterCondition::FIELD_FROM
    if cond.operator == WcoEmail::EmailFilterCondition::OPERATOR_MATCH
      if message.from.downcase.include?( value.downcase )
        reason = "#{email_skip_filter ? 'skip_' : ''}condition from match-i `#{value}`"
      end
    end

  when WcoEmail::EmailFilterCondition::FIELD_LEADSET
    if cond.operator == WcoEmail::EmailFilterCondition::OPERATOR__ID
      test_leadset = Wco::Leadset.find( value ) rescue nil
      if test_leadset
        if lead.leadset == test_leadset
          reason = "#{email_skip_filter ? 'skip_' : ''}condition leadset _id `#{value}`"
        end
      end
    end

  when WcoEmail::EmailFilterCondition::FIELD_TAGGED
    ## cond.operator should eql OPERATOR__ID but I don't check b/c if its slug, it should work also.
    tag   = Wco::Tag.find( cond.value ) rescue nil
    tag ||= Wco::Tag.where( slug: cond.value ).first
    if tag
      if lead.leadset.tags.include?( tag ) ||
         lead.tags.include?( tag )

        reason = "#{email_skip_filter ? 'skip_' : ''}condition TAGGED `#{tag.slug}`"

      end
    end

  when WcoEmail::EmailFilterCondition::FIELD_NOT_TAGGED
    ## cond.operator should eql OPERATOR__ID but I don't check b/c if its slug, it should work also.
    tag   = Wco::Tag.find( cond.value ) rescue nil
    tag ||= Wco::Tag.where( slug: cond.value ).first
    if tag
      if !lead.leadset.tags.include?( tag ) &&
         !lead.tags.include?( tag )

        reason = "#{email_skip_filter ? 'skip_' : ''}condition NOT_TAGGED `#{tag.slug}`"

      end
    end

  end ## end case

  return reason
end

#strip_valueObject



51
52
53
# File 'app/models/wco_email/email_filter_condition.rb', line 51

def strip_value
  self.value = value.strip
end

#to_sObject



113
114
115
# File 'app/models/wco_email/email_filter_condition.rb', line 113

def to_s
  "<EF#{email_skip_filter ? 'Skip' : ''}Condition #{field} #{operator} `#{value}` />"
end

#to_s_full(indent: 0) ⇒ Object



116
117
118
# File 'app/models/wco_email/email_filter_condition.rb', line 116

def to_s_full indent: 0
  "#{" " * indent }<EF#{email_skip_filter ? 'Skip' : ''}Condition #{field} #{operator} `#{value}` #{comment} />\n"
end