class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
  
  
  QI_DYNAMICS_CORE = '
  
  #START QI_DYNAMICS_CORE
  
  QI_DYNAMICS.each do |s|
    define_method("#{s}") {
      return eval("self.ac_#{s} == nil ? self.ca_#{s} : self.ac_#{s}")
    }
  end

  def qi_dynamics_cache
    puts "qi_dynamics_cache"
    
    if self.methods.include?(:to_no_archive)
      self.to_no_archive.each do |noc|
        eval("self.ac_#{noc} = nil")
      end
      
      
    end
    
    
    QI_DYNAMICS.each do |s|
      eval("self.cc_#{s} = self.#{s}")
    end
  end
  
  after_validation :qi_dynamics_cache
  
  def archive_now(skip_personalised_archive = nil)
    self.qi_dynamics_cache
    self.archived = true
    
    QI_DYNAMICS.each do |s|
      eval("self.ac_#{s} = self.cc_#{s} ")
    end
    
    self.personalised_archive if self.methods.include?(:personalised_archive) and !skip_personalised_archive
    self.save
  end
  
  def unarchive_now(skip_personalised_unarchive = nil)
    self.archived = false
    self.qi_dynamics_cache
    
    QI_DYNAMICS.each do |s|
      eval("self.ac_#{s} = nil ")
    end
    
    self.personalised_unarchive if self.methods.include?(:personalised_unarchive) and !skip_personalised_unarchive
    
    self.save
  end
 
  #END QI_DYNAMICS_CORE
  
  '
  
end
