<commit_before>class Assessment < ActiveRecord::Base
  acts_as_paranoid
  acts_as_duplicable
  #as is for belong_to association
  acts_as_superclass as: :as_assessment

  delegate :full_title, to: :as_assessment

  include Rails.application.routes.url_helpers

  default_scope { order("assessments.open_at") }

  attr_accessible :exp, :bonus_exp
  attr_accessible :title, :description
  attr_accessible :published, :comment_per_qn
  attr_accessible :open_at, :close_at, :bonus_cutoff_at
  attr_accessible :tab_id, :display_mode_id, :dependent_on_attributes
  attr_accessible :dependent_on, :dependent_on_ids, :dependent_id

  validates_presence_of :title


  include HasRequirement
  include ActivityObject

  scope :closed, -> { where("close_at < ?", Time.now) }
  scope :still_open, -> { where("close_at >= ? ", Time.now) }
  scope :opened, -> { where("open_at <= ? ", Time.now) }
  scope :future, -> { where("open_at > ? ", Time.now) }
  scope :published, -> { where(published: true) }
  scope :mission, -> { where(as_assessment_type: "Assessment::Mission") }
  scope :training, -> { where(as_assessment_type: "Assessment::Training") }

  belongs_to  :tab
  belongs_to  :course
  belongs_to  :creator, class_name: "User"
  belongs_to  :display_mode, class_name: "AssignmentDisplayMode", foreign_key: "display_mode_id"

  has_and_belongs_to_many :dependent_on,
                          class_name: "Assessment",
                          foreign_key: :id,
                          association_foreign_key: :dependent_id,
                          join_table: :assessment_dependency

  has_and_belongs_to_many :required_for,
                          class_name: "Assessment",
                          foreign_key: :dependent_id,
                          association_foreign_key: :id,
                          join_table: :assessment_dependency

  accepts_nested_attributes_for :dependent_on, allow_destroy: true

  has_many  :as_asm_reqs, class_name: "AsmReq", as: :asm, dependent: :destroy
  has_many  :as_requirements, through: :as_asm_reqs, source: :as_requirements

  has_many  :question_assessments
  has_many  :questions, through: :question_assessments do
    def coding
      where(as_question_type: Assessment::CodingQuestion)
    end

    def mcq
      where(as_question_type: Assessment::McqQuestion)
    end

    #TODO
    def before(question, pos = 0)
      if question.persisted?
        before_pos(proxy_association.owner.question_assessments.where(question_id: question.id).first.position)
      else
        before_pos(pos)
      end
    end

    def before_pos(position)
      where('position < ?', position)
    end
  end
<commit_msg>Delete question_assessments when assessment is destroyed<commit_after>class Assessment < ActiveRecord::Base
  acts_as_paranoid
  acts_as_duplicable
  #as is for belong_to association
  acts_as_superclass as: :as_assessment

  delegate :full_title, to: :as_assessment

  include Rails.application.routes.url_helpers

  default_scope { order("assessments.open_at") }

  attr_accessible :exp, :bonus_exp
  attr_accessible :title, :description
  attr_accessible :published, :comment_per_qn
  attr_accessible :open_at, :close_at, :bonus_cutoff_at
  attr_accessible :tab_id, :display_mode_id, :dependent_on_attributes
  attr_accessible :dependent_on, :dependent_on_ids, :dependent_id

  validates_presence_of :title


  include HasRequirement
  include ActivityObject

  scope :closed, -> { where("close_at < ?", Time.now) }
  scope :still_open, -> { where("close_at >= ? ", Time.now) }
  scope :opened, -> { where("open_at <= ? ", Time.now) }
  scope :future, -> { where("open_at > ? ", Time.now) }
  scope :published, -> { where(published: true) }
  scope :mission, -> { where(as_assessment_type: "Assessment::Mission") }
  scope :training, -> { where(as_assessment_type: "Assessment::Training") }

  belongs_to  :tab
  belongs_to  :course
  belongs_to  :creator, class_name: "User"
  belongs_to  :display_mode, class_name: "AssignmentDisplayMode", foreign_key: "display_mode_id"

  has_and_belongs_to_many :dependent_on,
                          class_name: "Assessment",
                          foreign_key: :id,
                          association_foreign_key: :dependent_id,
                          join_table: :assessment_dependency

  has_and_belongs_to_many :required_for,
                          class_name: "Assessment",
                          foreign_key: :dependent_id,
                          association_foreign_key: :id,
                          join_table: :assessment_dependency

  accepts_nested_attributes_for :dependent_on, allow_destroy: true

  has_many  :as_asm_reqs, class_name: "AsmReq", as: :asm, dependent: :destroy
  has_many  :as_requirements, through: :as_asm_reqs, source: :as_requirements

  has_many  :question_assessments, dependent: :destroy
  has_many  :questions, through: :question_assessments do
    def coding
      where(as_question_type: Assessment::CodingQuestion)
    end

    def mcq
      where(as_question_type: Assessment::McqQuestion)
    end

    #TODO
    def before(question, pos = 0)
      if question.persisted?
        before_pos(proxy_association.owner.question_assessments.where(question_id: question.id).first.position)
      else
        before_pos(pos)
      end
    end

    def before_pos(position)
      where('position < ?', position)
    end
  end

{'fraction_non_alphanumeric': 0.09345968041620216, 'fraction_numerical': 0.0003716090672612412, 'mean_word_length': 3.3481421647819065, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, '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': '4992872', 'n_tokens_mistral': 1743, 'n_tokens_neox': 1690, 'n_words': 335}