<commit_before>class Article < ApplicationRecord
  belongs_to :user, optional: true
  belongs_to :category
  belongs_to :country
  has_many :validations

  has_many :article_sources
  has_many :sources
  has_many :sources, through: :article_sources, :dependent => :destroy

  accepts_nested_attributes_for :sources, :reject_if=> lambda { |article| article[:name].blank? }

  validates :title, :content, :category, :country, presence: true
  before_save :make_title_case

  # --This custom setter method is called whenever an Article is initialized with a sources field.
  # --virtuals

  def self.most_validated_article
    most_validated_by_quantity =0
    most_validated_article=""
    self.all.each do |article|
      article.validations.each do |validation|
        if validation.quantity > most_validated_by_quantity
            most_validated_by_quantity = validation.quantity
            most_validated_article =  Article.find(validation.article_id)
        end
      end
    end
    if !most_validated_article.empty?
      most_validated_article.title
    else
      " There are currently no articles to validate! "
    end
  end

  def sources_attributes=(sources_attributes)
     sources_attributes.values.each do |sources_attribute|
       source = Source.find_or_create_by(sources_attribute)<commit_msg>reject_if not working<commit_after>class Article < ApplicationRecord
  belongs_to :user, optional: true
  belongs_to :category
  belongs_to :country
  has_many :validations

  has_many :article_sources
  has_many :sources
  has_many :sources, through: :article_sources, :dependent => :destroy

  accepts_nested_attributes_for :sources, :allow_destroy=> true

  validates :title, :content, :category, :sources, :country, presence: true
  before_save :make_title_case

  validate :destroy_attribute

  def destroy_attribute
   self.sources.each do |article|
      if article[:name].blank?
        article[:name].delete
      end
    end
  end
  # --This custom setter method is called whenever an Article is initialized with a sources field.
  # --virtuals

  def self.most_validated_article
    most_validated_by_quantity =0
    most_validated_article=""
    self.all.each do |article|
      article.validations.each do |validation|
        if validation.quantity > most_validated_by_quantity
            most_validated_by_quantity = validation.quantity
            most_validated_article =  Article.find(validation.article_id)
        end
      end
    end
    if !most_validated_article.empty?
      most_validated_article.title
    else
      " There are currently no articles to validate! "
    end
  end

  def sources_attributes=(sources_attributes)
     sources_attributes.values.each do |sources_attribute|
       source = Source.find_or_create_by(sources_attribute)
{'fraction_non_alphanumeric': 0.06850789096126256, 'fraction_numerical': 0.0007173601147776184, 'mean_word_length': 3.7756849315068495, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '3585255', 'n_tokens_mistral': 807, 'n_tokens_neox': 790, 'n_words': 203}