Sha256: af4d182fddf4ecc1194bdbf7b2f40b18ec7ecb1d3c9845570d2265b29244e4ad

Contents?: true

Size: 863 Bytes

Versions: 3

Compression:

Stored size: 863 Bytes

Contents

class MailEngine::MailTemplateFile < ActiveRecord::Base
  mount_uploader :file, MailEngine::TemplateFileUploader

  validates :file, :presence => true

  belongs_to :mail_template
  delegate :url, :to => :file
  after_save :replace_url_in_mail_template

  def image?
    return false if attributes["file"].blank?
    File.basename(attributes["file"]) =~ /\.(j(e)?pg)|(png)|(gif)$/i
  end

  def replace_url_in_mail_template
    if self.file_changed? and self.file_was.present?
      self.mail_template.update_attribute :body,
        MailEngine::HtmlDocumentAssetsReplacer.replace_resource_in_html(
          self.mail_template.body,
          self.file_was,
          File.basename(self.file.url),
          :filename
        )
    end
  end

  def clone *args
    file_clone = super *args
    file_clone.file = File.open(self.file.path)
    file_clone
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mail_engine-0.1.2 app/models/mail_engine/mail_template_file.rb
mail_engine-0.1.1 app/models/mail_engine/mail_template_file.rb
mail_engine-0.1.0 app/models/mail_engine/mail_template_file.rb