Commit ab9d02365181df373beffbe7732b36b9b3f7a307

Authored by Dmitriy Zaporozhets
1 parent f17ddeb3

Create dir with namespace. Create namespace with user

app/models/namespace.rb
... ... @@ -10,6 +10,8 @@ class Namespace < ActiveRecord::Base
10 10  
11 11 delegate :name, to: :owner, allow_nil: true, prefix: true
12 12  
  13 + after_save :ensure_dir_exist
  14 +
13 15 scope :root, where('type IS NULL')
14 16  
15 17 def self.search query
... ... @@ -23,4 +25,9 @@ class Namespace < ActiveRecord::Base
23 25 def human_name
24 26 owner_name
25 27 end
  28 +
  29 + def ensure_dir_exist
  30 + namespace_dir_path = File.join(Gitlab.config.git_base_path, code)
  31 + Dir.mkdir(namespace_dir_path) unless File.exists?(namespace_dir_path)
  32 + end
26 33 end
... ...
app/models/project.rb
... ... @@ -67,6 +67,7 @@ class Project < ActiveRecord::Base
67 67 message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }
68 68 validates :issues_enabled, :wall_enabled, :merge_requests_enabled,
69 69 :wiki_enabled, inclusion: { in: [true, false] }
  70 +
70 71 validate :check_limit, :repo_name
71 72  
72 73 # Scopes
... ... @@ -89,6 +90,12 @@ class Project < ActiveRecord::Base
89 90 project = Project.new params
90 91  
91 92 Project.transaction do
  93 +
  94 + # Build gitlab-hq code from GitLab HQ name
  95 + #
  96 + slug = project.name.dup.parameterize
  97 + project.code = project.path = slug
  98 +
92 99 project.owner = user
93 100 project.namespace_id = namespace_id
94 101 project.save!
... ...
app/models/user.rb
... ... @@ -63,11 +63,14 @@ class User < ActiveRecord::Base
63 63 validates :bio, length: { within: 0..255 }
64 64 validates :extern_uid, :allow_blank => true, :uniqueness => {:scope => :provider}
65 65 validates :projects_limit, presence: true, numericality: {greater_than_or_equal_to: 0}
  66 + validates :username, presence: true
66 67  
67 68 before_validation :generate_password, on: :create
68 69 before_save :ensure_authentication_token
69 70 alias_attribute :private_token, :authentication_token
70 71  
  72 + delegate :code, to: :namespace, allow_nil: true, prefix: true
  73 +
71 74 # Scopes
72 75 scope :not_in_project, ->(project) { where("id not in (:ids)", ids: project.users.map(&:id) ) }
73 76 scope :admins, where(admin: true)
... ... @@ -122,4 +125,8 @@ class User < ActiveRecord::Base
122 125 namespaces = namespaces + Group.all if admin
123 126 namespaces
124 127 end
  128 +
  129 + def several_namespaces?
  130 + namespaces.size > 1
  131 + end
125 132 end
... ...
app/observers/user_observer.rb
1 1 class UserObserver < ActiveRecord::Observer
2 2 def after_create(user)
  3 + user.create_namespace(code: user.username, name: user.name)
  4 +
3 5 log_info("User \"#{user.name}\" (#{user.email}) was created")
4 6  
5 7 Notify.new_user_email(user.id, user.password).deliver
... ... @@ -10,7 +12,7 @@ class UserObserver &lt; ActiveRecord::Observer
10 12 end
11 13  
12 14 def after_save user
13   - if user.username_changed?
  15 + if user.username_changed? and user.namespace
14 16 user.namespace.update_attributes(code: user.username)
15 17 end
16 18 end
... ...
app/views/admin/users/_form.html.haml
... ... @@ -16,6 +16,11 @@
16 16 = f.text_field :name
17 17 %span.help-inline * required
18 18 .clearfix
  19 + = f.label :username
  20 + .input
  21 + = f.text_field :username
  22 + %span.help-inline * required
  23 + .clearfix
19 24 = f.label :email
20 25 .input
21 26 = f.text_field :email
... ... @@ -26,11 +31,11 @@
26 31 = f.label :force_random_password do
27 32 %span Generate random password
28 33 .input= f.check_box :force_random_password, {}, true, nil
29   -
  34 +
30 35 %div.password-fields
31 36 .clearfix
32 37 = f.label :password
33   - .input= f.password_field :password, disabled: f.object.force_random_password
  38 + .input= f.password_field :password, disabled: f.object.force_random_password
34 39 .clearfix
35 40 = f.label :password_confirmation
36 41 .input= f.password_field :password_confirmation, disabled: f.object.force_random_password
... ...
app/views/projects/_new_form.html.haml
... ... @@ -9,27 +9,12 @@
9 9 = f.text_field :name, placeholder: "Example Project", class: "xxlarge"
10 10 = f.submit 'Create project', class: "btn primary project-submit"
11 11  
12   - %hr
13   - %div.adv_settings
14   - %h6 Advanced settings:
15   - - if current_user.namespaces.size > 1
16   - .clearfix
17   - = f.label :namespace_id do
18   - Namespace
19   - .input
20   - = f.select :namespace_id, namespaces_options, {}, {class: 'chosen'}
21   - .clearfix
22   - = f.label :path do
23   - Git Clone
24   - .input
25   - .input-prepend
26   - %span.add-on= Gitlab.config.ssh_path
27   - = f.text_field :path, placeholder: "example_project", disabled: !@project.new_record?
28   - %span.add-on= ".git"
  12 + - if current_user.several_namespaces?
29 13 .clearfix
30   - = f.label :code do
31   - URL
  14 + = f.label :namespace_id do
  15 + %span.cgray Namespace
32 16 .input
33   - .input-prepend
34   - %span.add-on= web_app_url
35   - = f.text_field :code, placeholder: "example"
  17 + = f.select :namespace_id, namespaces_options, {}, {class: 'chosen'}
  18 + %hr
  19 + %p.padded
  20 + All created project are private. You choose who can see project and commit to repository.
... ...