summaryrefslogtreecommitdiff
path: root/app/models/namespace.rb
diff options
context:
space:
mode:
authorDouwe Maan <douwe@gitlab.com>2015-03-24 14:53:30 +0100
committerDouwe Maan <douwe@gitlab.com>2015-03-27 11:09:29 +0100
commit3f7531d6f2974ea75ab8e67bc93049f674ddb672 (patch)
tree5340386a05e4970e7064392741c18f053157fccc /app/models/namespace.rb
parent28592ae46767443dc0f3723bd4f05f360bab8f41 (diff)
downloadgitlab-ce-3f7531d6f2974ea75ab8e67bc93049f674ddb672.tar.gz
Move User.cleanup_username to Namespace.cleanup_path.
Diffstat (limited to 'app/models/namespace.rb')
-rw-r--r--app/models/namespace.rb43
1 files changed, 31 insertions, 12 deletions
diff --git a/app/models/namespace.rb b/app/models/namespace.rb
index 35280889a86..0c0252ed8ee 100644
--- a/app/models/namespace.rb
+++ b/app/models/namespace.rb
@@ -44,21 +44,40 @@ class Namespace < ActiveRecord::Base
scope :root, -> { where('type IS NULL') }
- def self.by_path(path)
- where('lower(path) = :value', value: path.downcase).first
- end
+ class << self
+ def by_path(path)
+ where('lower(path) = :value', value: path.downcase).first
+ end
- # Case insensetive search for namespace by path or name
- def self.find_by_path_or_name(path)
- find_by("lower(path) = :path OR lower(name) = :path", path: path.downcase)
- end
+ # Case insensetive search for namespace by path or name
+ def find_by_path_or_name(path)
+ find_by("lower(path) = :path OR lower(name) = :path", path: path.downcase)
+ end
- def self.search(query)
- where("name LIKE :query OR path LIKE :query", query: "%#{query}%")
- end
+ def search(query)
+ where("name LIKE :query OR path LIKE :query", query: "%#{query}%")
+ end
- def self.global_id
- 'GLN'
+ def global_id
+ 'GLN'
+ end
+
+ def clean_path(path)
+ path.gsub!(/@.*\z/, "")
+ path.gsub!(/\.git\z/, "")
+ path.gsub!(/\A-/, "")
+ path.gsub!(/\z./, "")
+ path.gsub!(/[^a-zA-Z0-9_\-\.]/, "")
+
+ counter = 0
+ base = path
+ while Namespace.by_path(path).present?
+ counter += 1
+ path = "#{base}#{counter}"
+ end
+
+ path
+ end
end
def to_param