Class: AbAdmin::Mailers::MailAttachHelper::MailAttacher

Inherits:
Object
  • Object
show all
Defined in:
lib/ab_admin/mailers/mail_attach_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ MailAttacher


15
16
17
18
19
# File 'lib/ab_admin/mailers/mail_attach_helper.rb', line 15

def initialize(text)
  @text = text.gsub(/(\n|\t|\r)/, ' ').gsub(/>\s*</, '><').squeeze(' ')
  @images = []
  parse_images
end

Instance Attribute Details

#imagesObject

Returns the value of attribute images


13
14
15
# File 'lib/ab_admin/mailers/mail_attach_helper.rb', line 13

def images
  @images
end

#textObject

Returns the value of attribute text


13
14
15
# File 'lib/ab_admin/mailers/mail_attach_helper.rb', line 13

def text
  @text
end

Instance Method Details

#parse_imagesObject


21
22
23
24
25
26
27
28
# File 'lib/ab_admin/mailers/mail_attach_helper.rb', line 21

def parse_images
  raw_res = @text.scan(/<img[^>]*?src="(\/[^"]*)/).flatten
  raw_res.map do |r|
    r_clear = r.gsub(/\?[^?]*$/, '')
    @images << [r, Rails.root.join('public', r_clear.slice(1..-1)).to_s, File.basename(r_clear)]
  end
  @images
end

#replace_attaches(attaches) ⇒ Object


30
31
32
33
34
35
# File 'lib/ab_admin/mailers/mail_attach_helper.rb', line 30

def replace_attaches(attaches)
  @images.each do |image|
    @text = @text.gsub(image[0], attaches[image[2]].try(:url))
  end
  @text
end