class MailHist < ApplicationRecord belongs_to :mail_content belongs_to :mail_type belongs_to :m_odr_rep belongs_to :m_odr belongs_to :lang_site belongs_to :p_customer belongs_to :element, :polymorphic => true acts_as_sorting :fields => { :created_at => {:name => "Date", :reorder => true}, :from_email => {:name => "Email exp.", :reorder => true}, :to_email => {:name => "Email dest.", :reorder => true}, :subject => {:name => "Sujet.", :reorder => true}, :element => {:name => "Elément"}, :mail_type => {:name => "Elément"}, :p_customer => {:name => "Client"}, :actions => "Actions" } def self.generate_mail(lang_slug, mail_type, email, options = {}) #(lang_slug, mail_type, @m_odr_rep.particulars.first.email, {:arguments => {:numero_avoir => ""}, :m_odr => @m_odr}) @options = options @lang_site = LangSite.find_by_slug(lang_slug) @mail_type = mail_type @mail_content = @mail_type.mail_contents.where(:lang_site_id => @lang_site.id).first @p_customer = options[:p_customer] @m_odr = options[:m_odr] @m_odr_rep = options[:m_odr_rep] @file_tunel = options[:file_tunel] @arguments = @options[:arguments] arguments = @arguments s = @mail_content.subject.to_s arguments.each_pair do |key, value| s = s.gsub(/\[#{key.to_s}\]/, value.to_s) end @subject = s r = @mail_content.message arguments.each_pair do |key, value| r = r.gsub(/\[#{key.to_s}\]/, value.to_s) end @body = r if @options[:from] @from = @options[:from] elsif @m_odr and @m_odr.email_from? @from = @m_odr.email_from elsif @file_tunel and @file_tunel.from_email? @from = @file_tunel.from_email end @to_email = email mail_history = MailHist.create(:lang_site => @lang_site, :to_email => @to_email, :from_email => @from, :element => options[:element], :subject => @subject, :body => @body, :m_odr => @m_odr, :m_odr_rep => @m_odr_rep, :p_customer => @p_customer, :mail_content => @mail_content, :mail_type => @mail_type) mail = GeneralMails.general2(@to_email, @subject, @body, mail_options = {:m_odr => @m_odr, :file_tunel => @file_tunel}) mail_history.subject_send = @subject mail_history.body_send = mail.body.encoded mail.deliver mail_history.save 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