<commit_before>class RepositoryFile < FakeRecord
  class PathValidator < ActiveModel::Validator
    def validate(record)
      if record.repository.points_through_file?(record.target_path)
        record.errors[:target_directory] = "Error! This path points to or through a file."
      end
    end
  end

  # basic repository file usage
  attr_reader :repository, :file, :user
  attr_accessor :index
  delegate :name, :path, :size, :mime_type, :mime_category,
    :oid, :file?, :dir?, :type, to: :file

  # only for new/edit
  attr_reader :message, :temp_file, :target_directory, :target_filename
  validates :message, :temp_file, presence: true
  validates_with PathValidator, :if => :temp_file_exists?

  def self.find_with_path(opts)
    new(opts)
  rescue GitRepository::PathNotFoundError
    nil
  end

  def self.find_with_path!(opts)
    self.find_with_path(opts) ||
      raise(GitRepository::PathNotFoundError,
        [opts[:repository_id], opts[:ref], opts[:path]].compact.join('/'))
  end

  def self.find_with_basepath(opts)
    repository = Repository.find_by_path(opts[:repository_id])
    oid        = compute_ref(repository, opts[:ref])[:oid]
    dir_path   = opts[:path].split('/')[0..-2].join('/')

    entries = repository.git.folder_contents(oid, dir_path).reduce([]) do |es, entry|
      if entry.path.start_with?(opts[:path]) && entry.file?
        es << new(repository_id: repository.to_param, path: entry.path)
      end
    end
  end

  def initialize(*args, &block)
    opts = args.shift.symbolize_keys
    @user = opts[:user]
    if self.class.manipulating_file?(opts)
      initialize_for_create_and_update(opts)
    elsif opts[:git_file] && opts[:repository]
      initialize_for_already_loaded_file(opts)
    else
      initialize_for_read(opts)
    end
  end

  def save!
    raise RecordNotSavedError unless valid?
    repository.save_file(temp_file.path, target_path, message, user)
  end

  def ontologies(child_name=nil)
    @ontologies ||= if file?
      ontos = repository.ontologies.find_with_path(path).parents_first
      ontos.map!{ |o| o.children.where(name: child_name) }.flatten! if child_name
      ontos<commit_msg>use with_path instead of find_with_path<commit_after>class RepositoryFile < FakeRecord
  class PathValidator < ActiveModel::Validator
    def validate(record)
      if record.repository.points_through_file?(record.target_path)
        record.errors[:target_directory] = "Error! This path points to or through a file."
      end
    end
  end

  # basic repository file usage
  attr_reader :repository, :file, :user
  attr_accessor :index
  delegate :name, :path, :size, :mime_type, :mime_category,
    :oid, :file?, :dir?, :type, to: :file

  # only for new/edit
  attr_reader :message, :temp_file, :target_directory, :target_filename
  validates :message, :temp_file, presence: true
  validates_with PathValidator, :if => :temp_file_exists?

  def self.find_with_path(opts)
    new(opts)
  rescue GitRepository::PathNotFoundError
    nil
  end

  def self.find_with_path!(opts)
    self.find_with_path(opts) ||
      raise(GitRepository::PathNotFoundError,
        [opts[:repository_id], opts[:ref], opts[:path]].compact.join('/'))
  end

  def self.find_with_basepath(opts)
    repository = Repository.find_by_path(opts[:repository_id])
    oid        = compute_ref(repository, opts[:ref])[:oid]
    dir_path   = opts[:path].split('/')[0..-2].join('/')

    entries = repository.git.folder_contents(oid, dir_path).reduce([]) do |es, entry|
      if entry.path.start_with?(opts[:path]) && entry.file?
        es << new(repository_id: repository.to_param, path: entry.path)
      end
    end
  end

  def initialize(*args, &block)
    opts = args.shift.symbolize_keys
    @user = opts[:user]
    if self.class.manipulating_file?(opts)
      initialize_for_create_and_update(opts)
    elsif opts[:git_file] && opts[:repository]
      initialize_for_already_loaded_file(opts)
    else
      initialize_for_read(opts)
    end
  end

  def save!
    raise RecordNotSavedError unless valid?
    repository.save_file(temp_file.path, target_path, message, user)
  end

  def ontologies(child_name=nil)
    @ontologies ||= if file?
      ontos = repository.ontologies.with_path(path).parents_first
      ontos.map!{ |o| o.children.where(name: child_name) }.flatten! if child_name
      ontos
{'fraction_non_alphanumeric': 0.11875719217491369, 'fraction_numerical': 0.0009205983889528193, 'mean_word_length': 3.9330306469920546, 'pattern_counts': {'":': 0, '<': 11, '<?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': '2948328', 'n_tokens_mistral': 1425, 'n_tokens_neox': 1402, 'n_words': 297}