<commit_before># frozen_string_literal: true

class Form < ApplicationRecord
  extend FriendlyId
  friendly_id :form_name, use: :slugged
  has_many :fields, dependent: :destroy
  has_many :drafts, class_name: 'FormDraft',
                    foreign_key: :form_id,
                    dependent: :destroy
  accepts_nested_attributes_for :fields

  validates :name, presence: true, uniqueness: true
  default_scope { order :name }

  def create_draft(user)
    return draft_belonging_to(user) if draft_belonging_to?(user)
    draft_attributes = attributes.symbolize_keys
                                 .except(:id)
                                 .merge user: user, form: self
    draft = FormDraft.create draft_attributes.except(:slug)
    fields.each do |field|
      new_field = field.dup
      new_field.assign_attributes form: nil, form_draft: draft
      new_field.save
    end
    draft
  end

  def draft_belonging_to(user)<commit_msg>rename this method since it sometimes just finds something<commit_after># frozen_string_literal: true

class Form < ApplicationRecord
  extend FriendlyId
  friendly_id :form_name, use: :slugged
  has_many :fields, dependent: :destroy
  has_many :drafts, class_name: 'FormDraft',
                    foreign_key: :form_id,
                    dependent: :destroy
  accepts_nested_attributes_for :fields

  validates :name, presence: true, uniqueness: true
  default_scope { order :name }

  def find_or_create_draft(user)
    return draft_belonging_to(user) if draft_belonging_to?(user)
    draft_attributes = attributes.symbolize_keys
                                 .except(:id)
                                 .merge user: user, form: self
    draft = FormDraft.create draft_attributes.except(:slug)
    fields.each do |field|
      new_field = field.dup
      new_field.assign_attributes form: nil, form_draft: draft
      new_field.save
    end
    draft
  end

  def draft_belonging_to(user)
{'fraction_non_alphanumeric': 0.07102418939783839, 'fraction_numerical': 0.0, 'mean_word_length': 2.760154738878143, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2711675', 'n_tokens_mistral': 571, 'n_tokens_neox': 566, 'n_words': 143}