class MailHistory < ActiveRecord::Base belongs_to :element, :polymorphic => true belongs_to :mail_type belongs_to :mail_content belongs_to :p_customer belongs_to :lang_site def self.generateMail(slug, to, options = {}) options[:lang_slug] = options[:lang_slug] || "fr" options[:mail_options] = options[:mail_options] || {} @lang_site = LangSite.find_by_slug(options[:lang_slug]) @mail_content = MailContent.find_key(@lang_site, slug) @mail_type = @mail_content.mail_type @lang_slug = options[:lang_slug] @options = options[:mail_options] @email = to arguments = options[:mail_options][:arguments] || {} s = @mail_content.subject.to_s arguments.each_pair do |key, value| s = s.gsub(/\[#{key.to_s}\]/, value.to_s) end @subject = DEFAULT_SUBJECT_EMAIL.to_s+s r = @mail_content.message arguments.each_pair do |key, value| r = r.gsub(/\[#{key.to_s}\]/, value.to_s) end @content = r mail_history = MailHistory.create(:lang_site => @lang_site, :email => @email, :element => options[:element], :subject => @subject, :content => @content, :p_customer => options[:p_customer], :mail_content => @mail_content, :mail_type => @mail_type) return mail_history end def deliver_mail mail = GeneralMails.general(self.lang_site.slug, self.mail_type.slug, self.email , self.subject, self.content) self.subject_cache = self.subject self.content_cache = mail.body.encoded mail.deliver self.sended = true self.sended_at = Time.now self.save end end