<commit_before>class Product::ClassifiedAdvertisement::Vacancy < ::Vacancy
  attr_accessible :task_id, :task
  
  after_initialize :set_defaults

=begin
  has_many :candidatures, -> do
    order(%Q{
      CASE 
        WHEN candidatures.state='new' THEN 1 
        WHEN candidatures.state='accepted' THEN 2 
        WHEN candidatures.state='denied' THEN 3 
        ELSE 4 
      END
    })
  end
=end

  validates :task, presence: true
  
  def task
    @task ||= task_class.where(id: task_id).first
  end
  
  def task=(value)
    self.task_id = value.try(:id).try(:to_s)
    @task = value
  end
  
  def task_class
    if product.present?
      "#{product.class.name}::Task".constantize rescue Task
    else
      Task
    end
  end
  
  def product
    @product ||= project.product if project
  end
  
  #def new_or_accepted_candidature
  #  candidatures.where(state: ['new', 'accepted']).first
  #end
  <commit_msg>Implements candidatures_attributes.<commit_after>class Product::ClassifiedAdvertisement::Vacancy < ::Vacancy
  has_many :candidatures, dependent: :destroy, class_name: 'Product::ClassifiedAdvertisement::Candidature', inverse_of: :vacancy
  
  attr_accessible :task_id, :task
  
  after_initialize :set_defaults

=begin
  has_many :candidatures, -> do
    order(%Q{
      CASE 
        WHEN candidatures.state='new' THEN 1 
        WHEN candidatures.state='accepted' THEN 2 
        WHEN candidatures.state='denied' THEN 3 
        ELSE 4 
      END
    })
  end
=end

  validates :task, presence: true
  
  def candidatures_attributes=(attributes)
    self.candidatures ||= []
    
    attributes.each do |index, candidature_attributes|
      destroy = candidature_attributes.delete :_destroy
      
      if candidature_attributes[:id].present? && destroy.to_i == 1
        begin; candidature_class.destroy(candidature_attributes[:id]); rescue ActiveRecord::RecordNotFound; end
        @candidatures.select!{|v| v.id != candidature_attributes[:id]}
        next
      end
      
      candidature_attributes[:vacancy] = self
      candidature = nil
      
      if candidature_attributes[:id].present?
        begin; candidature = candidature_class.find(candidature_attributes[:id]); rescue ActiveRecord::RecordNotFound; end
        
        candidature_attributes.delete(:id)
        
        candidature.attributes = candidature_attributes if candidature.present?
      else
        candidature = candidature_class.new(candidature_attributes)
      end
      
      next unless candidature.present?
      
      if candidature_attributes[:id].present?
        self.candidatures.each_with_index do |current_candidature, candidatures_index|
          if current_candidature.id.to_s == candidature_attributes[:id]
            self.candidatures[candidatures_index] = candidature
          end
        end
      else
        self.candidatures << candidature
      end
    end
  end
  
  def task
    @task ||= task_class.where(id: task_id).first
  end
  
  def task=(value)
    self.task_id = value.try(:id).try(:to_s)
    @task = value
  end
  
  def task_class
    if product.present?
      "#{product.class.name}::Task".constantize rescue Task
    else
      Task
    end
  end
  
  def candidature_class
    if product.present?
      "#{product.class.name}::Candidature".constantize rescue Candidature
    else
      Candidature
    end
  end
  
  def product
    @product ||= project.product if project
  end
  
  #def new_or_accepted_candidature
  #  candidatures.where(state: ['new', 'accepted']).first
  #end
  
{'fraction_non_alphanumeric': 0.09332579185520362, 'fraction_numerical': 0.0025452488687782806, 'mean_word_length': 2.836225596529284, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1786795', 'n_tokens_mistral': 1128, 'n_tokens_neox': 1094, 'n_words': 231}