<commit_before>class Publication
  include Mongoid::Document
  include Mongoid::Timestamps

  field :panopticon_id,   :type => Integer

  field :name,            :type => String
  field :slug,            :type => String
  field :tags,            :type => String
  field :audiences,       :type => Array

  field :has_drafts,      :type => Boolean
  field :has_fact_checking, :type => Boolean
  field :has_published,   :type => Boolean
  field :has_reviewables, :type => Boolean
  field :archived,        :type => Boolean
  field :lined_up,        :type => Boolean

  field :section,         :type => String
  field :department,      :type => String
  field :related_items,   :type => String

  field :rejected_count,  :type => Integer, default: 0
  field :edition_rejected_count,  :type => Integer, default: 0

  embeds_many :publishings

  scope :in_draft,         where(has_drafts: true).order_by(:edition_rejected_count => :desc).order_by(:name => :desc)
  scope :lined_up,         where(lined_up: true)
  scope :fact_checking,    where(has_fact_checking: true)
  scope :published,        where(has_published: true)
  scope :review_requested, where(has_reviewables: true)
  scope :archive,          where(archived: true)
  scope :assigned_to,      lambda{ |user|
    expr = if user
      %{assignment && assignment.recipient_id == "#{user.id}"}
    else
      %{!assignment}
    end
    where(%{
      function(){
        var last = function(a){ return a && a[a.length - 1]; }
        var edition = last(this.editions);
        if (!edition) { return false; }
        var assignment = last((edition.actions || []).filter(function(a){
          return a.request_type == "#{Action::ASSIGNED}";
        }));
        return #{expr};
      }
    })
  }

  after_initialize :create_first_edition

  before_save :calculate_statuses, :denormalise_metadata, :mark_as_started

  # validates_presence_of :panopticon_id

  accepts_nested_attributes_for :editions, :reject_if => proc { |a| a['title'].blank? }

  def self.import panopticon_id, importing_user
    uri = "#{Plek.current.find("arbiter")}/artefacts/#{panopticon_id}.js"
    data = open(uri).read
    json = JSON.parse data
    publication = Publication.where(slug: json['slug']).first
    if publication.present?
      return publication if publication.panopticon_id
      publication.panopticon_id = json['id']
      publication.save!
      return publication
    end

    kind = json['kind']
    publication = importing_user.create_publication kind.to_sym, :panopticon_id => json['id'], :name => json['name']
    publication.save!
    publication
  end

  def self.find_and_identify_edition(slug, edition)
    publication = where(slug: slug).first
    return nil if publication.nil?
    if edition.present?
      # This is used for previewing yet-to-be-published editions.
      # At some point this should require special authentication.
      if edition == "latest"
        publication.editions.order_by(:created_at => :desc).first
      else
        publication.editions.select { |e| e.version_number.to_i == edition.to_i }.first
      end
    else
      publication.published_edition
    end
  end

  def panopticon_uri
    Plek.current.find("arbiter") + '/artefacts/' + (panopticon_id || slug).to_s<commit_msg>Tidy away the filter generation.<commit_after>class Publication
  include Mongoid::Document
  include Mongoid::Timestamps

  field :panopticon_id,   :type => Integer

  field :name,            :type => String
  field :slug,            :type => String
  field :tags,            :type => String
  field :audiences,       :type => Array

  field :has_drafts,      :type => Boolean
  field :has_fact_checking, :type => Boolean
  field :has_published,   :type => Boolean
  field :has_reviewables, :type => Boolean
  field :archived,        :type => Boolean
  field :lined_up,        :type => Boolean

  field :section,         :type => String
  field :department,      :type => String
  field :related_items,   :type => String

  field :rejected_count,  :type => Integer, default: 0
  field :edition_rejected_count,  :type => Integer, default: 0

  embeds_many :publishings

  scope :in_draft,         where(has_drafts: true).order_by(:edition_rejected_count => :desc).order_by(:name => :desc)
  scope :lined_up,         where(lined_up: true)
  scope :fact_checking,    where(has_fact_checking: true)
  scope :published,        where(has_published: true)
  scope :review_requested, where(has_reviewables: true)
  scope :archive,          where(archived: true)
  scope :assigned_to,      lambda{ |user| assignment_filter(user) }

  after_initialize :create_first_edition

  before_save :calculate_statuses, :denormalise_metadata, :mark_as_started

  # validates_presence_of :panopticon_id

  accepts_nested_attributes_for :editions, :reject_if => proc { |a| a['title'].blank? }

  def self.import panopticon_id, importing_user
    uri = "#{Plek.current.find("arbiter")}/artefacts/#{panopticon_id}.js"
    data = open(uri).read
    json = JSON.parse data
    publication = Publication.where(slug: json['slug']).first
    if publication.present?
      return publication if publication.panopticon_id
      publication.panopticon_id = json['id']
      publication.save!
      return publication
    end

    kind = json['kind']
    publication = importing_user.create_publication kind.to_sym, :panopticon_id => json['id'], :name => json['name']
    publication.save!
    publication
  end

  def self.find_and_identify_edition(slug, edition)
    publication = where(slug: slug).first
    return nil if publication.nil?
    if edition.present?
      # This is used for previewing yet-to-be-published editions.
      # At some point this should require special authentication.
      if edition == "latest"
        publication.editions.order_by(:created_at => :desc).first
      else
        publication.editions.select { |e| e.version_number.to_i == edition.to_i }.first
      end
    else
      publication.published_edition
    end
  end

  def self.assignment_filter(user)
    expr = if user
      %{assignment && assignment.recipient_id == "#{user.id}"}
    else
      %{!assignment}
    end
    where(%{
      function(){
        var last = function(a){ return a && a[a.length - 1]; }
        var edition = last(this.editions);
        if (!edition) { return false; }
        var assignment = last((edition.actions || []).filter(function(a){
          return a.request_type == "#{Action::ASSIGNED}";
        }));
        return #{expr};
      }
    })
  end

  def panopticon_uri
    Plek.current.find("arbiter") + '/artefacts/' + (panopticon_id || slug).to_s
{'fraction_non_alphanumeric': 0.11716744677638533, 'fraction_numerical': 0.0009059338668277216, 'mean_word_length': 3.1142857142857143, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 47, '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': '2099993', 'n_tokens_mistral': 2086, 'n_tokens_neox': 2056, 'n_words': 489}