<commit_before># == Schema Information
#
# Table name: options
#
#  id                        :integer          not null, primary key
#  question_id               :integer
#  text                      :text
#  created_at                :datetime
#  updated_at                :datetime
#  next_question             :string(255)
#  number_in_question        :integer
#  deleted_at                :datetime
#  instrument_version_number :integer          default(-1)
#  special                   :boolean          default(FALSE)
#

class Option < ActiveRecord::Base
  include Translatable
  default_scope { order('number_in_question ASC') }
  scope :special_options, -> { where(special: true) }
  belongs_to :question
  delegate :instrument, to: :question, allow_nil: true
  delegate :project, to: :question
  has_many :translations, foreign_key: 'option_id', class_name: 'OptionTranslation', dependent: :destroy
  before_save :update_instrument_version, if: Proc.new { |option| option.changed? }
  before_save :update_option_translation, if: Proc.new { |option| option.text_changed? }
  before_destroy :update_instrument_version
  after_save :record_instrument_version_number
  has_paper_trail
  acts_as_paranoid
  has_many :skips, dependent: :destroy 
  has_one :grid_label

  validates :text, presence: true, allow_blank: false

  amoeba do
    enable
    include_field :translations
    nullify :next_question 
  end

  def to_s
    text
  end

  #Return grid_label text if option has grid_label
  def text
    if grid_label
      grid_label.label
    else
      read_attribute(:text)
    end
  end
  
  def instrument_version
    if instrument && (read_attribute(:instrument_version_number) == -1)
      instrument.current_version_number
    else
      read_attribute(:instrument_version_number)
    end<commit_msg>sanitize option next_question attribute<commit_after># == Schema Information
#
# Table name: options
#
#  id                        :integer          not null, primary key
#  question_id               :integer
#  text                      :text
#  created_at                :datetime
#  updated_at                :datetime
#  next_question             :string(255)
#  number_in_question        :integer
#  deleted_at                :datetime
#  instrument_version_number :integer          default(-1)
#  special                   :boolean          default(FALSE)
#

class Option < ActiveRecord::Base
  include Translatable
  default_scope { order('number_in_question ASC') }
  scope :special_options, -> { where(special: true) }
  belongs_to :question
  delegate :instrument, to: :question, allow_nil: true
  delegate :project, to: :question
  has_many :translations, foreign_key: 'option_id', class_name: 'OptionTranslation', dependent: :destroy
  before_save :update_instrument_version, if: Proc.new { |option| option.changed? }
  before_save :update_option_translation, if: Proc.new { |option| option.text_changed? }
  before_destroy :update_instrument_version
  after_save :record_instrument_version_number
  after_save :sanitize_next_question
  has_paper_trail
  acts_as_paranoid
  has_many :skips, dependent: :destroy 
  has_one :grid_label

  validates :text, presence: true, allow_blank: false

  amoeba do
    enable
    include_field :translations
    nullify :next_question 
  end

  def sanitize_next_question
    unless next_question.blank?
      next_qst = instrument.questions.where(question_identifier: next_question)
      if next_qst.blank?
        update_columns(next_question: nil)
      end
    end
  end

  def to_s
    text
  end

  #Return grid_label text if option has grid_label
  def text
    if grid_label
      grid_label.label
    else
      read_attribute(:text)
    end
  end
  
  def instrument_version
    if instrument && (read_attribute(:instrument_version_number) == -1)
      instrument.current_version_number
    else
      read_attribute(:instrument_version_number)
    end
{'fraction_non_alphanumeric': 0.07126495291422753, 'fraction_numerical': 0.0025451768897938407, 'mean_word_length': 2.937875751503006, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4360390', 'n_tokens_mistral': 1173, 'n_tokens_neox': 1142, 'n_words': 282}