namespace.rb 1.49 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1 2 3 4 5 6 7 8 9 10 11 12 13
# == Schema Information
#
# Table name: namespaces
#
#  id         :integer          not null, primary key
#  name       :string(255)      not null
#  path       :string(255)      not null
#  owner_id   :integer          not null
#  created_at :datetime         not null
#  updated_at :datetime         not null
#  type       :string(255)
#

14
class Namespace < ActiveRecord::Base
15
  attr_accessible :name, :path
16

17
  has_many :projects, dependent: :destroy
18 19 20
  belongs_to :owner, class_name: "User"

  validates :name, presence: true, uniqueness: true
21 22 23
  validates :path, uniqueness: true, presence: true, length: { within: 1..255 },
            format: { with: /\A[a-zA-Z][a-zA-Z0-9_\-\.]*\z/,
                      message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }
24 25 26 27
  validates :owner, presence: true

  delegate :name, to: :owner, allow_nil: true, prefix: true

28 29
  after_create :ensure_dir_exist
  after_update :move_dir
30

31 32
  scope :root, where('type IS NULL')

33
  def self.search query
34
    where("name LIKE :query OR path LIKE :query", query: "%#{query}%")
35 36 37
  end

  def to_param
38
    path
39
  end
40 41 42 43

  def human_name
    owner_name
  end
44 45

  def ensure_dir_exist
46
    namespace_dir_path = File.join(Gitlab.config.git_base_path, path)
47
    Dir.mkdir(namespace_dir_path, 0770) unless File.exists?(namespace_dir_path)
48
  end
49 50 51 52 53 54

  def move_dir
    old_path = File.join(Gitlab.config.git_base_path, path_was)
    new_path = File.join(Gitlab.config.git_base_path, path)
    system("mv #{old_path} #{new_path}")
  end
55
end