<commit_before>class Node < ActiveRecord::Base
  MODEL_NODE_TYPE = 1
  PREVIEW_NODE_TYPE = 2
  DOCUMENT_NODE_TYPE = 3
  IMAGE_NODE_TYPE = 4
  DATA_NODE_TYPE = 5

  acts_as_tree :order => "name"

  belongs_to :node_type
  belongs_to :group

  belongs_to :visibility, :class_name => "PermissionSetting", :foreign_key => :visibility_id
  belongs_to :changeability, :class_name => "PermissionSetting", :foreign_key => :changeability_id

  has_many :node_versions, :order => 'updated_at DESC'
  has_many :postings, :order => 'updated_at'

  has_many :tagged_nodes
  has_many :tags, :through => :tagged_nodes

  has_many :email_recommendations
  has_many :recommendations

  has_many :spam_warnings

  validates_presence_of :name, :node_type_id, :visibility_id, :changeability_id
  validates_numericality_of :node_type_id, :visibility_id, :changeability_id

  # Convenience named scopes for grabbing certain types of children
  named_scope :models, :conditions => ['node_type_id = ? ', MODEL_NODE_TYPE]
  named_scope :non_models, :conditions => ['node_type_id <> ? ', MODEL_NODE_TYPE]
  named_scope :previews, :conditions => ['node_type_id = ? ', PREVIEW_NODE_TYPE]
  named_scope :documents, :conditions => ['node_type_id = ? ', DOCUMENT_NODE_TYPE]
  named_scope :images, :conditions => ['node_type_id = ? ', IMAGE_NODE_TYPE]
  named_scope :data, :conditions => ['node_type_id = ? ', DATA_NODE_TYPE]

  # ------------------------------------------------------------
  # Grab children of various sorts
  # ------------------------------------------------------------

  def person
    self.node_versions.sort { |n| n.created_at}.last.person
  end

  def children_of_id(id)
    self.children.select { |v| v.node_type_id == id}
  end

  def models
    self.children_of_id(MODEL_NODE_TYPE)
  end

  def non_models
    self.children.select { |v| v.node_type_id != MODEL_NODE_TYPE}
  end

  def previews
    self.children_of_id(PREVIEW_NODE_TYPE)
  end

  def latest_preview
    self.children.last.node_versions.sort { |n| n.created_at}.last.contents
  end

  def documents
    self.children_of_id(DOCUMENT_NODE_TYPE)
  end

  def images
    self.children_of_id(IMAGE_NODE_TYPE)
  end

  def data
    self.children_of_id(DATA_NODE_TYPE)
  end

  def files
    self.non_models
  end

  # ------------------------------------------------------------
  # Info from the latest version
  # ------------------------------------------------------------

  def description<commit_msg>Got uploading of preview images to work again<commit_after>class Node < ActiveRecord::Base
  MODEL_NODE_TYPE = 1
  PREVIEW_NODE_TYPE = 2
  DOCUMENT_NODE_TYPE = 3
  IMAGE_NODE_TYPE = 4
  DATA_NODE_TYPE = 5

  acts_as_tree :order => "name"

  belongs_to :node_type
  belongs_to :group

  belongs_to :visibility, :class_name => "PermissionSetting", :foreign_key => :visibility_id
  belongs_to :changeability, :class_name => "PermissionSetting", :foreign_key => :changeability_id

  has_many :node_versions, :order => 'updated_at DESC'
  has_many :postings, :order => 'updated_at'

  has_many :tagged_nodes
  has_many :tags, :through => :tagged_nodes

  has_many :email_recommendations
  has_many :recommendations

  has_many :spam_warnings

  validates_presence_of :name, :node_type_id, :visibility_id, :changeability_id
  validates_numericality_of :node_type_id, :visibility_id, :changeability_id

  # Convenience named scopes for grabbing certain types of children
  named_scope :models, :conditions => ['node_type_id = ? ', MODEL_NODE_TYPE]
  named_scope :non_models, :conditions => ['node_type_id <> ? ', MODEL_NODE_TYPE]
  named_scope :previews, :conditions => ['node_type_id = ? ', PREVIEW_NODE_TYPE]
  named_scope :documents, :conditions => ['node_type_id = ? ', DOCUMENT_NODE_TYPE]
  named_scope :images, :conditions => ['node_type_id = ? ', IMAGE_NODE_TYPE]
  named_scope :data, :conditions => ['node_type_id = ? ', DATA_NODE_TYPE]

  # ------------------------------------------------------------
  # Grab children of various sorts
  # ------------------------------------------------------------

  def person
    self.node_versions.sort { |n| n.created_at}.last.person
  end

  def children_of_type(id)
    self.children.select { |v| v.node_type_id == id}
  end

  def models
    self.children_of_type(MODEL_NODE_TYPE)
  end

  def non_models
    self.children.select { |v| v.node_type_id != MODEL_NODE_TYPE}
  end

  def previews
    self.children_of_type(PREVIEW_NODE_TYPE)
  end

  def latest_preview
    self.previews.sort_by { |p| p.created_at}.last.contents
  end

  def documents
    self.children_of_type(DOCUMENT_NODE_TYPE)
  end

  def images
    self.children_of_type(IMAGE_NODE_TYPE)
  end

  def data
    self.children_of_type(DATA_NODE_TYPE)
  end

  def files
    self.non_models
  end

  # ------------------------------------------------------------
  # Info from the latest version
  # ------------------------------------------------------------

  def description
{'fraction_non_alphanumeric': 0.18280871670702178, 'fraction_numerical': 0.0020177562550443904, 'mean_word_length': 2.8013803680981595, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, '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': '2711678', 'n_tokens_mistral': 1659, 'n_tokens_neox': 1564, 'n_words': 332}