class FileTunel < ApplicationRecord
  belongs_to :p_customer
  belongs_to :admin
  
  
  has_many :file_tunel_sends, :dependent => :destroy
  accepts_nested_attributes_for :file_tunel_sends, :allow_destroy => true
  
  has_many :file_tunel_files, :dependent => :destroy
  accepts_nested_attributes_for :file_tunel_files, :allow_destroy => true
  
  
  
  has_many :mail_types, :dependent => :destroy
  accepts_nested_attributes_for :mail_types, :allow_destroy => true
  
  
  
  
  QI_DYNAMICS = %w()

  eval(QI_DYNAMICS_CORE)
    
  def self.qi_table_order
    {
      :created_at => {:name => "Date", :reorder => true},
      :name => {:name => "Nom", :reorder => true},
      
      :slug => {:name => "Permalien", :reorder => true},
      
      :start_at => {:name => "Date de début", :reorder => true},
      :end_at => {:name => "Date de fin", :reorder => true},
    
      :actions => "Actions",
     
    }
    
  end
  
  def self.valid_sort
    r = []
    self.qi_table_order.each do |key, value|
      if value.instance_of? Hash
        if value[:reorder] and value[:reorder] == true
          r << key.to_s if value[:reorder]
        end
      end
    end
    return r
    
    
  end
  
  
  
  before_create { generate_token() }
  
  def generate_token()
    begin
      self[:token] = SecureRandom.urlsafe_base64
    end while FileTunel.exists?(:token => self[:token])
  end
  
end
