<commit_before>class ContentPlan < ActiveRecord::Base
  include Versioning
  include Organisations

  acts_as_taggable

  QUARTERS = 1..4
  YEARS = Time.now.year..(Time.now + 2.years).year

  has_many :tasks,    -> { order(created_at: :desc) }, dependent: :destroy
  has_many :comments, -> { order(created_at: :desc) }, dependent: :destroy, as: :commentable

  has_many :content_plan_needs

  has_many :content_plan_contents, dependent: :destroy
  has_many :contents, through: :content_plan_contents

  scope :due_date, ->(quarter, year) {
    scope = all
    scope = scope.where(due_quarter: quarter) if quarter.present?
    scope = scope.where(due_year: year) if year.present?
    scope
  }

  scope :contents, -> {
    Content.where id: all.map(&:content_plan_contents).flatten.map(&:content_id).uniq
  }

  validates :title, presence: true
  validates :ref_no, presence: true

  # Caching
  before_save do
    contents.each { |record| record.touch }
  end

  def name
    "#{ref_no} - #{title}"
  end
  alias_method :to_s, :name

  def maslow_need_ids
    content_plan_needs.map(&:need_id)
  end

  def maslow_need_ids=(ids)
    content_plan_needs.destroy_all
    ids.reject(&:blank?).each do |nid|
      ContentPlanNeed.find_or_create_by!(content_plan: self, need_id: nid)
    end<commit_msg>Order contents list on a content plan by the ref_no<commit_after>class ContentPlan < ActiveRecord::Base
  include Versioning
  include Organisations

  acts_as_taggable

  QUARTERS = 1..4
  YEARS = Time.now.year..(Time.now + 2.years).year

  has_many :tasks,    -> { order(created_at: :desc) }, dependent: :destroy
  has_many :comments, -> { order(created_at: :desc) }, dependent: :destroy, as: :commentable

  has_many :content_plan_needs

  has_many :content_plan_contents, dependent: :destroy
  has_many :contents, through: :content_plan_contents

  scope :due_date, ->(quarter, year) {
    scope = all
    scope = scope.where(due_quarter: quarter) if quarter.present?
    scope = scope.where(due_year: year) if year.present?
    scope
  }

  scope :contents, -> {
    Content.where(id: all.map(&:content_plan_contents).flatten.map(&:content_id).uniq).
      order("contents.ref_no")
  }

  validates :title, presence: true
  validates :ref_no, presence: true

  # Caching
  before_save do
    contents.each { |record| record.touch }
  end

  def name
    "#{ref_no} - #{title}"
  end
  alias_method :to_s, :name

  def maslow_need_ids
    content_plan_needs.map(&:need_id)
  end

  def maslow_need_ids=(ids)
    content_plan_needs.destroy_all
    ids.reject(&:blank?).each do |nid|
      ContentPlanNeed.find_or_create_by!(content_plan: self, need_id: nid)
    end
{'fraction_non_alphanumeric': 0.11707317073170732, 'fraction_numerical': 0.00225140712945591, 'mean_word_length': 3.900735294117647, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '1848370', 'n_tokens_mistral': 958, 'n_tokens_neox': 913, 'n_words': 202}