class FatFreeCRM::MailProcessor::CommentReplies

Constants

ENTITY_SHORTCUTS

Subject line of email can contain full entity, or shortcuts e.g. [contact:1234] OR [co:1234]

Public Class Methods

new() click to toggle source
Calls superclass method FatFreeCRM::MailProcessor::Base::new
# File lib/fat_free_crm/mail_processor/comment_replies.rb, line 25
def initialize
  @settings = Setting.email_comment_replies.dup
  super
end

Private Instance Methods

create_comment(email, entity_name, entity_id) click to toggle source

Creates a new comment on an entity

# File lib/fat_free_crm/mail_processor/comment_replies.rb, line 58
def create_comment(email, entity_name, entity_id)
  # Find entity from subject params
  if (entity = entity_name.capitalize.constantize.find_by_id(entity_id))
    # Create comment if sender has permissions for entity
    if sender_has_permissions_for?(entity)
      parsed_reply = EmailReplyParser.parse_reply(plain_text_body(email))
      Comment.create user:        @sender,
                     commentable: entity,
                     comment:     parsed_reply
    end
  end
end
process(_uid, email) click to toggle source

Email processing pipeline

# File lib/fat_free_crm/mail_processor/comment_replies.rb, line 34
def process(_uid, email)
  with_subject_line(email) do |entity_name, entity_id|
    create_comment email, entity_name, entity_id
  end
end
with_subject_line(email) { |entity_name, entity_id| ... } click to toggle source

Checks the email to detect [entity:id] in the subject.

# File lib/fat_free_crm/mail_processor/comment_replies.rb, line 42
def with_subject_line(email)
  if /\[([^:]*):([^\]]*)\]/ =~ email.subject
    entity_name = Regexp.last_match[1]
    entity_id = Regexp.last_match[2]
    # Check that entity is a known model
    if ENTITY_SHORTCUTS.values.include?(entity_name)
      yield entity_name, entity_id
    # Check if entity is a 2 letter 'shortcut'
    elsif expanded_entity = ENTITY_SHORTCUTS[entity_name]
      yield expanded_entity, entity_id
    end
  end
end