<commit_before>class CommitRequest 
  include ActiveAttr::Model
  include ActiveMessaging::MessageSender
  attr_accessor :user, :command, :repository, :branch,:files,  :records
  attr_writer :commit_message, :files

  validates_presence_of :user, :command, :repository, :branch, :commit_message
  validates_numericality_of :user, :repository
  validates_inclusion_of :command, in: %w( move add remove ), message: "%s is not an acceptable command" 
  validate :existence_of_user, :existence_of_repository, :commit_access , :correct_branch, :path_names

  publishes_to :commit

  #
  # Performs the the action given by @options
  # @move {
  #  command: "move",
  #  user: 1,
  #  repository: 123,
  #  branch: "master",
  #  commit_message: "A commit message",
  #  paths: [{
  #    from: "old/path",
  #    to: "new/path"
  #  }],
  #  records: [{
  #    from: "path/to/file.txt",
  #    to: "path/to/new_file.txt"
  #  }]
  # }
  #
  # @add {
  #  command: "add",
  #  user: 1,
  #  repository: 123,
  #  branch: "master",
  #  commit_message: "A commit message",
  #  files: [{
  #    from: "/full/path/to/file",
  #    to: "path/to/dir"
  #  }]
  # }
  #
  # @remove {
  #   user: 1,
  #   command: "remove",
  #   repository: 123,
  #   branch: "master",
  #   commit_message: "A commit message",
  #   records: [
  #     "path/to/dir1",
  #     "path/to/dir2",
  #   ]
  # }
  #
  # @mkdir {
  #   command: "mkdir",
  #   user: 1,
  #   repository: 123,
  #   branch: "master",
  #   commit_message: "Antagligen inget meddelande tills vidare",
  #   path: "path/to/dir"
  # }

  #
  # @return String Commit message provided by frontend
  #
  def commit_message
    if @commit_message.to_s.length.between?(6, 96)
      @commit_message
    else
      "WebCommit: #{@command}"
    end
  end

  #
  # Ads @options to beanstalkd
  # @return Boolean
  #   false when validation fails or @options has been pushed
  #   true when data has been pushed to beanstalkd
  #
  def save
    return false unless valid?
    if @command == "add"
      @files.each do |file|
        file["from"] = File.join(Rails.root, APP_CONFIG['tmp_upload_directory'], file["id"])
      end
    end

    @options = {
      command: @command, 
      user: @user, 
      repository: @repository, 
      branch: @branch, 
      commit_message: commit_message, 
      files: @files
    }

    @@cache[@options.to_s] ||= publish :commit, @options.merge({
      callback: {
        class: "CommitRequest",
        method: "notify_user"
      }
    }).to_json
  end

  #
  # Notify view about process
  # @options Hash See @remove, @add and @move
  #
  def self.notify_user(options)
    config = APP_CONFIG["faye"]
    token = User.find(options["user"]).token
    SecureFaye::Connect.new.
      message({status: 200, options: options}.to_json).
      token(config["token"]).
      server("http://0.0.0.0:#{config["port"]}/faye").<commit_msg>CommitRequest should pass @records to CRP<commit_after>class CommitRequest 
  include ActiveAttr::Model
  include ActiveMessaging::MessageSender
  attr_accessor :user, :command, :repository, :branch,:files, :records
  attr_writer :commit_message, :files

  validates_presence_of :user, :command, :repository, :branch, :commit_message
  validates_numericality_of :user, :repository
  validates_inclusion_of :command, in: %w( move add remove ), message: "%s is not an acceptable command" 
  validate :existence_of_user, :existence_of_repository, :commit_access , :correct_branch, :path_names

  publishes_to :commit

  #
  # Performs the the action given by @options
  # @move {
  #  command: "move",
  #  user: 1,
  #  repository: 123,
  #  branch: "master",
  #  commit_message: "A commit message",
  #  paths: [{
  #    from: "old/path",
  #    to: "new/path"
  #  }],
  #  records: [{
  #    from: "path/to/file.txt",
  #    to: "path/to/new_file.txt"
  #  }]
  # }
  #
  # @add {
  #  command: "add",
  #  user: 1,
  #  repository: 123,
  #  branch: "master",
  #  commit_message: "A commit message",
  #  files: [{
  #    from: "/full/path/to/file",
  #    to: "path/to/dir"
  #  }]
  # }
  #
  # @remove {
  #   user: 1,
  #   command: "remove",
  #   repository: 123,
  #   branch: "master",
  #   commit_message: "A commit message",
  #   records: [
  #     "path/to/dir1",
  #     "path/to/dir2",
  #   ]
  # }
  #
  # @mkdir {
  #   command: "mkdir",
  #   user: 1,
  #   repository: 123,
  #   branch: "master",
  #   commit_message: "Antagligen inget meddelande tills vidare",
  #   path: "path/to/dir"
  # }

  #
  # @return String Commit message provided by frontend
  #
  def commit_message
    if @commit_message.to_s.length.between?(6, 96)
      @commit_message
    else
      "WebCommit: #{@command}"
    end
  end

  #
  # Ads @options to beanstalkd
  # @return Boolean
  #   false when validation fails or @options has been pushed
  #   true when data has been pushed to beanstalkd
  #
  def save
    return false unless valid?
    if @command == "add"
      @files.each do |file|
        file["from"] = File.join(Rails.root, APP_CONFIG['tmp_upload_directory'], file["id"])
      end
    end

    @options = {
      command: @command, 
      user: @user, 
      repository: @repository, 
      branch: @branch, 
      commit_message: commit_message, 
      files: @files
    }

    if @command == "remove"
      @options.merge!({
        records: @records
      })
    end

    @@cache[@<EMAIL>] ||= publish :commit, @options.merge({
      callback: {
        class: "CommitRequest",
        method: "notify_user"
      }
    }).to_json
  end

  #
  # Notify view about process
  # @options Hash See @remove, @add and @move
  #
  def self.notify_user(options)
    config = APP_CONFIG["faye"]
    token = User.find(options["user"]).token
    SecureFaye::Connect.new.
      message({status: 200, options: options}.to_json).
      token(config["token"]).
      server("http://0.0.0.0:#{config["port"]}/faye").
{'fraction_non_alphanumeric': 0.1449763991908294, 'fraction_numerical': 0.009440323668240054, 'mean_word_length': 2.7197492163009405, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '490677', 'n_tokens_mistral': 2179, 'n_tokens_neox': 2084, 'n_words': 479}