<commit_before>class TemplateDivision < ActiveRecord::Base
  belongs_to :exam_template
  belongs_to :criteria_assignment_files_join, dependent: :destroy

  accepts_nested_attributes_for :criteria_assignment_files_join, allow_destroy: true

  validates :start, numericality: { greater_than_or_equal_to: 1,
                                    less_than_or_equal_to: :end,
                                    only_integer: true }
  validates :end, numericality: { only_integer: true }
  validate :end_should_be_less_than_or_equal_to_num_pages
  validates :label, uniqueness: true, allow_blank: false

  after_save :set_defaults_for_associated_criteria_assignment_files_join # when template division is create or updated

  def hash
    [self.start, self.end, self.label].hash
  end

  def end_should_be_less_than_or_equal_to_num_pages
    errors.add(:end, "should be less than or equal to num_pages") unless self.end <= self.exam_template.num_pages
  end

  def set_defaults_for_associated_criteria_assignment_files_join
    if criteria_assignment_files_join.nil?
      assignment_file = AssignmentFile.find_or_initialize_by(
        filename: "#{exam_template.name}-#{label}.pdf",
        assignment_id: exam_template.assignment.id
      )
      criterion = FlexibleCriterion.find_or_initialize_by(
        name: label,
        assignment_id: exam_template.assignment.id
      )
      if criterion.new_record?
        criterion.update(max_mark: 1.0)
      end
      criteria_assignment_files_join_object = CriteriaAssignmentFilesJoin.new(
        assignment_file: assignment_file,
        criterion: criterion
      )
      self.update(criteria_assignment_files_join: criteria_assignment_files_join_object)
    else
      criteria_assignment_files_join.assignment_file.filename = "#{exam_template.name}-#{label}.pdf"
      criteria_assignment_files_join.criterion.name = label
      criteria_assignment_files_join.save!
    end
  end
end
<commit_msg>think it should be 'create' instead of 'new' for CriteriaAssignmentFilesJoin<commit_after>class TemplateDivision < ActiveRecord::Base
  belongs_to :exam_template
  belongs_to :criteria_assignment_files_join, dependent: :destroy

  accepts_nested_attributes_for :criteria_assignment_files_join, allow_destroy: true

  validates :start, numericality: { greater_than_or_equal_to: 1,
                                    less_than_or_equal_to: :end,
                                    only_integer: true }
  validates :end, numericality: { only_integer: true }
  validate :end_should_be_less_than_or_equal_to_num_pages
  validates :label, uniqueness: true, allow_blank: false

  after_save :set_defaults_for_associated_criteria_assignment_files_join # when template division is created or updated

  def hash
    [self.start, self.end, self.label].hash
  end

  def end_should_be_less_than_or_equal_to_num_pages
    errors.add(:end, "should be less than or equal to num_pages") unless self.end <= self.exam_template.num_pages
  end

  def set_defaults_for_associated_criteria_assignment_files_join
    if criteria_assignment_files_join.nil?
      assignment_file = AssignmentFile.find_or_initialize_by(
        filename: "#{exam_template.name}-#{label}.pdf",
        assignment_id: exam_template.assignment.id
      )
      criterion = FlexibleCriterion.find_or_initialize_by(
        name: label,
        assignment_id: exam_template.assignment.id
      )
      if criterion.new_record?
        criterion.update(max_mark: 1.0)
      end
      criteria_assignment_files_join_object = CriteriaAssignmentFilesJoin.create(
        assignment_file: assignment_file,
        criterion: criterion
      )
      self.update(criteria_assignment_files_join: criteria_assignment_files_join_object)
    else
      criteria_assignment_files_join.assignment_file.filename = "#{exam_template.name}-#{label}.pdf"
      criteria_assignment_files_join.criterion.name = label
      criteria_assignment_files_join.save!
    end
  end
end

{'fraction_non_alphanumeric': 0.06510219530658592, 'fraction_numerical': 0.001514004542013626, 'mean_word_length': 3.857843137254902, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5605527', 'n_tokens_mistral': 1227, 'n_tokens_neox': 1215, 'n_words': 215}