<commit_before>class RemoteApp < ActiveRecord::Base
  CLIENT_APP_CREATOR  = "g5-client-app-creator"
  CLIENT_HUB_DEPLOYER = "g5-client-hub-deployer"
  CLIENT_HUB          = "g5-client-hub"
  CLIENT_APP_CREATOR_DEPLOYER = "g5-client-app-creator-deployer"
  KINDS           = [CLIENT_APP_CREATOR, CLIENT_HUB_DEPLOYER, CLIENT_HUB, CLIENT_APP_CREATOR_DEPLOYER]
  PREFIXES = {
    CLIENT_HUB_DEPLOYER => "g5-chd-",
    CLIENT_HUB          => "g5-ch-"
  }
  REPOS = {
    CLIENT_APP_CREATOR  => "<EMAIL>:g5search/g5-client-app-creator.git",
    CLIENT_HUB_DEPLOYER => "<EMAIL>:g5search/g5-client-hub-deployer.git",
    CLIENT_HUB          => "<EMAIL>:g5search/g5-client-hub.git"
  }

  attr_accessible :entry_id, :client_uid, :client_name
  attr_accessible :kind, :name, :git_repo, :heroku_app_name

  belongs_to :entry

  # habtm
  has_many :instructions_target_apps, foreign_key: :target_app_id
  has_many :instructions, through: :instructions_target_apps, source: :instruction

  validates :kind, presence: true, inclusion: { in: KINDS }
  validates :client_uid, presence: true, unless: :client_app_creator?
  validates :client_name, presence: true, unless: :client_app_creator?
  validates :name, presence: true, uniqueness: true
  validates :heroku_app_name, presence: true, uniqueness: true
  validates :git_repo, presence: true

  before_validation :assign_missing_attributes
  after_create :create_instruction

  def self.grouped_by_kind_options
    KINDS.map do|kind|
      [kind, RemoteApp.where(kind: kind).map {|app| [app.name, app.id] } ]
    end
  end

  def heroku_repo
    @heroku_repo ||= "<EMAIL>:#{heroku_app_name}.git"
  end

  def heroku_url
    @heroku_url ||= "http://#{heroku_app_name}.herokuapp.com"
  end

  def siblings
    RemoteApp.where("id != ?", id).where(client_uid: client_uid)
  end

  private

  def assign_missing_attributes
    self.git_repo ||= REPOS[kind]
    if client_app_creator?
      self.heroku_app_name ||= CLIENT_APP_CREATOR
    else
      self.heroku_app_name ||= "#{PREFIXES[kind]}#{client_name.parameterize}" if client_name
    end
    self.name ||= heroku_app_name
  end

  def create_instruction
    Instruction.create(
      target_app_kind: client_app_creator.kind,
      target_app_ids: [client_app_creator.id],
      remote_app_id: self.id
    )
  end

  def client_app_creator?
    self.kind == CLIENT_APP_CREATOR
  end

  def client_app_creator
    self.class.client_app_creator<commit_msg>refactors remote_app.rb due to addition of client_app_creator_deployer<commit_after>class RemoteApp < ActiveRecord::Base

  CLIENT_APP_CREATOR          = "g5-client-app-creator"
  CLIENT_APP_CREATOR_DEPLOYER = "g5-client-app-creator-deployer"

  CLIENT_HUB                  = "g5-client-hub"
  CLIENT_HUB_DEPLOYER         = "g5-client-hub-deployer"

  KINDS           = [CLIENT_APP_CREATOR, CLIENT_HUB_DEPLOYER, CLIENT_HUB, CLIENT_APP_CREATOR_DEPLOYER]

  PREFIXES = {
    CLIENT_HUB_DEPLOYER => "g5-chd-",
    CLIENT_HUB          => "g5-ch-"
  }

  REPOS = {
    CLIENT_APP_CREATOR          => "<EMAIL>:g5search/g5-client-app-creator.git",
    CLIENT_APP_CREATOR_DEPLOYER => "<EMAIL>:g5search/g5-client-app-creator-deployer.git",
    CLIENT_HUB_DEPLOYER         => "<EMAIL>:g5search/g5-client-hub-deployer.git",
    CLIENT_HUB                  => "<EMAIL>:g5search/g5-client-hub.git"
  }

  attr_accessible :entry_id, :client_uid, :client_name
  attr_accessible :kind, :name, :git_repo, :heroku_app_name

  belongs_to :entry

  # habtm
  has_many :instructions_target_apps, foreign_key: :target_app_id
  has_many :instructions, through: :instructions_target_apps, source: :instruction

  validates :kind, presence: true, inclusion: { in: KINDS }
  validates :client_uid, presence: true, unless: :non_client_app?
  validates :client_name, presence: true, unless: :non_client_app?
  validates :name, presence: true, uniqueness: true
  validates :heroku_app_name, presence: true, uniqueness: true
  validates :git_repo, presence: true

  before_validation :assign_missing_attributes
  after_create :create_instruction

  def self.grouped_by_kind_options
    KINDS.map do|kind|
      [kind, RemoteApp.where(kind: kind).map {|app| [app.name, app.id] } ]
    end
  end

  def heroku_repo
    @heroku_repo ||= "git@heroku.com:#{heroku_app_name}.git"
  end

  def heroku_url
    @heroku_url ||= "http://#{heroku_app_name}.herokuapp.com"
  end

  def siblings
    RemoteApp.where("id != ?", id).where(client_uid: client_uid)
  end

  private

  def non_client_app?
    self.kind.in? [CLIENT_APP_CREATOR, CLIENT_APP_CREATOR_DEPLOYER]
  end

  def assign_missing_attributes
    assign_git_repo
    assign_heroku_app_name
    assign_name
  end

  def assign_git_repo
    self.git_repo ||= REPOS[kind]
  end

  def assign_heroku_app_name
    if non_client_app?
      self.heroku_app_name ||= kind
    else
      self.heroku_app_name ||= "#{PREFIXES[kind]}#{client_name.parameterize}" if client_name
    end
  end

  def assign_name
    self.name ||= heroku_app_name
  end

  def create_instruction
    Instruction.create(
      target_app_kind: client_app_creator.kind,
      target_app_ids: [client_app_creator.id],
      remote_app_id: self.id
    )
  end

  def client_app_creator
    self.class.client_app_creator
{'fraction_non_alphanumeric': 0.10403963414634146, 'fraction_numerical': 0.004954268292682927, 'mean_word_length': 3.8646895273401296, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '2097845', 'n_tokens_mistral': 2041, 'n_tokens_neox': 1947, 'n_words': 326}