<commit_before>class CertificationCampaign < ActiveRecord::Base
  include Ownership

  attr_accessor :jurisdiction, :version
  attr_writer :limit

  validates :name, uniqueness: true, presence: true
  validates :url, :jurisdiction, presence: true, if: :validate_extra_details?
  validates :limit, numericality: { greater_than: 0 }, allow_blank: true

  has_many :certificate_generators
  belongs_to :user

  attr_accessible :name, :limit, :url, :jurisdiction, :version

  def total_count
    generated_count + duplicate_count
  end

  def generated_count
    current.count
  end

  def published_count
    current.joins(:certificate).merge(Certificate.published).count
  end

  def current
    certificate_generators.where(latest: true)
  end

  def incomplete_count
    current.where(completed: nil).count
  end

  def rerun!
    if url.present?
      factory = CertificateFactory::Factory.new(feed: url)
      factory.each do |item|
        url = factory.get_link(item)
        existing_generator = certificate_generators.select { |g| g.request["documentationUrl"] == url }.first
        generator = CertificateGenerator.create(
          request: existing_generator.try(:request) || build_request(url),
          user: user,
          certification_campaign: self
        )
        generator.sidekiq_delay.generate(existing_generator.survey.access_code, true, existing_generator.try(:dataset))
      end
    else
      certificate_generators.each do |c|
        dataset = Dataset.find(c.dataset.id)
        generator = CertificateGenerator.create(
          request: c.request,
          user: user,
          certification_campaign: self
        )
        jurs = if c.survey
          c.survey.access_code
        else
          jurisdiction
        end
        generator.sidekiq_delay.generate(jurs, true, dataset)
      end
    end
  end

  def build_request(url)
    {
      documentationUrl: url
    }
  end

  def validate_extra_details?
    version.to_i > 1
  end

  def limit
    @limit.to_i unless @limit.blank?
  end

end<commit_msg>Refactor dat<commit_after>class CertificationCampaign < ActiveRecord::Base
  include Ownership

  attr_accessor :jurisdiction, :version
  attr_writer :limit

  validates :name, uniqueness: true, presence: true
  validates :url, :jurisdiction, presence: true, if: :validate_extra_details?
  validates :limit, numericality: { greater_than: 0 }, allow_blank: true

  has_many :certificate_generators
  belongs_to :user

  attr_accessible :name, :limit, :url, :jurisdiction, :version

  def total_count
    generated_count + duplicate_count
  end

  def generated_count
    current.count
  end

  def published_count
    current.joins(:certificate).merge(Certificate.published).count
  end

  def current
    certificate_generators.where(latest: true)
  end

  def incomplete_count
    current.where(completed: nil).count
  end

  def rerun!
    if url.present?
      factory = CertificateFactory::Factory.new(feed: url)
      factory.each do |item|
        url = factory.get_link(item)
        existing_generator = certificate_generators.select { |g| g.request["documentationUrl"] == url }.first
        request = existing_generator.try(:request) || build_request(url)
        run_generator(request, certificate_generators.first.survey.access_code, existing_generator.try(:dataset))
      end
    else
      certificate_generators.each do |c|
        dataset = Dataset.find(c.dataset.id)
        jurs = if c.survey
          c.survey.access_code
        else
          jurisdiction
        end
        run_generator(c.request, jurs, dataset)
      end
    end
  end

  def build_request(url)
    {
      documentationUrl: url
    }
  end

  def validate_extra_details?
    version.to_i > 1
  end

  def limit
    @limit.to_i unless @limit.blank?
  end

  def run_generator(request, access_code, dataset)
    generator = CertificateGenerator.create(
      request: request,
      user: user,
      certification_campaign: self
    )
    generator.sidekiq_delay.generate(access_code, true, dataset)
  end

end
{'fraction_non_alphanumeric': 0.08123456790123457, 'fraction_numerical': 0.0009876543209876543, 'mean_word_length': 3.0836693548387095, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5546546', 'n_tokens_mistral': 1240, 'n_tokens_neox': 1185, 'n_words': 278}