<commit_before>class ObservationField < ActiveRecord::Base
  belongs_to :user
  has_many :observation_field_values, :dependent => :destroy
  has_many :observations, :through => :observation_field_values
  has_many :project_observation_fields, :dependent => :destroy
  has_many :comments, :as => :parent, :dependent => :destroy
  has_subscribers :to => {
    :comments => {:notification => "activity", :include_owner => true}
  }
  
  validates_uniqueness_of :name
  validates_presence_of :name
  validates_length_of :allowed_values, :maximum => 512, :allow_blank => true
  
  before_validation :strip_tags
  before_validation :strip_name
  before_validation :strip_description
  before_validation :strip_allowed_values
  validate :allowed_values_has_pipes
  
  # TYPES = %w(text numeric date time datetime location)
  TYPES = %w(text numeric date time taxon)
  
  def strip_name
    self.name = name.strip unless name.blank?
    true
  end
  
  def strip_description
    self.description = description.strip unless description.blank?
    true
  end
  
  def strip_allowed_values
    return true if allowed_values.blank?
    self.allowed_values = allowed_values.split('|').map{|v| v.strip}.join('|')
    true
  end
  
  # usually we would sanitize on the front, but we use these values in json a <commit_msg>Added some validations for observation fields.<commit_after>class ObservationField < ActiveRecord::Base
  belongs_to :user
  has_many :observation_field_values, :dependent => :destroy
  has_many :observations, :through => :observation_field_values
  has_many :project_observation_fields, :dependent => :destroy
  has_many :comments, :as => :parent, :dependent => :destroy
  has_subscribers :to => {
    :comments => {:notification => "activity", :include_owner => true}
  }
  
  validates_uniqueness_of :name
  validates_presence_of :name
  validates_length_of :allowed_values, :maximum => 512, :allow_blank => true
  validates_length_of :name, :maximum => 255, :allow_blank => true
  validates_length_of :description, :maximum => 255, :allow_blank => true
  
  before_validation :strip_tags
  before_validation :strip_name
  before_validation :strip_description
  before_validation :strip_allowed_values
  validate :allowed_values_has_pipes
  
  # TYPES = %w(text numeric date time datetime location)
  TYPES = %w(text numeric date time taxon)
  
  def strip_name
    self.name = name.strip unless name.blank?
    true
  end
  
  def strip_description
    self.description = description.strip unless description.blank?
    true
  end
  
  def strip_allowed_values
    return true if allowed_values.blank?
    self.allowed_values = allowed_values.split('|').map{|v| v.strip}.join('|')
    true
  end
  
  # usually we would sanitize on the front, but we use these values in json a 
{'fraction_non_alphanumeric': 0.08859397417503587, 'fraction_numerical': 0.00430416068866571, 'mean_word_length': 4.522772277227722, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '1760315', 'n_tokens_mistral': 870, 'n_tokens_neox': 851, 'n_words': 227}