class MailType < ApplicationRecord
  has_many :mail_contents
  belongs_to :mail_type_reference, :class_name => "MailType"

  
  accepts_nested_attributes_for :mail_contents



  acts_as_sorting :fields => {
      
      :slug => {:name => "Réf", :reorder => true},
      :default_title => {:name => "Sujet", :reorder => true},
      :default_message => {:name => "Contenu", :reorder => true},
      
      :actions => "Actions",
    }
 

  def self.find_or_create(slug)
    if mail_type = self.where(:slug => slug).first
      return mail_type
    else
      mail_type = MailType.create(:slug => slug)
      LangSite.all.each do |ls|
        mail_type.mail_contents.create(:lang_site => ls, :enabled => true, :content_type => "text")
      end
      return mail_type

    end
  end


end
