Commit c50ec72b52e9ed7270f7c81c2c71fd8e5a28eeb0

Authored by Dmitriy Zaporozhets
1 parent ab9d0236

Deprecate code for Project. Use title and path

app/controllers/admin/projects_controller.rb
... ... @@ -5,7 +5,7 @@ class Admin::ProjectsController < AdminController
5 5 @projects = Project.scoped
6 6 @projects = @projects.where(namespace_id: params[:namespace_id]) if params[:namespace_id].present?
7 7 @projects = @projects.search(params[:name]) if params[:name].present?
8   - @projects = @projects.includes(:namespace).order("namespaces.code, projects.name ASC").page(params[:page]).per(20)
  8 + @projects = @projects.includes(:namespace).order("namespaces.path, projects.name ASC").page(params[:page]).per(20)
9 9 end
10 10  
11 11 def show
... ...
app/controllers/application_controller.rb
... ... @@ -66,7 +66,7 @@ class ApplicationController < ActionController::Base
66 66 id = params[:project_id] || params[:id]
67 67 id = id.split("/") if id.include?("/")
68 68  
69   - @project ||= current_user.projects.find_by_code(id)
  69 + @project ||= current_user.projects.find_by_path(id)
70 70 @project || render_404
71 71 end
72 72  
... ...
app/models/namespace.rb
1 1 class Namespace < ActiveRecord::Base
2   - attr_accessible :code, :name, :owner_id
  2 + attr_accessible :name, :path
3 3  
4   - has_many :projects
  4 + has_many :projects, dependent: :destroy
5 5 belongs_to :owner, class_name: "User"
6 6  
7 7 validates :name, presence: true, uniqueness: true
8   - validates :code, presence: true, uniqueness: true
  8 + validates :path, uniqueness: true, presence: true, length: { within: 1..255 },
  9 + format: { with: /\A[a-zA-Z][a-zA-Z0-9_\-\.]*\z/,
  10 + message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }
9 11 validates :owner, presence: true
10 12  
11 13 delegate :name, to: :owner, allow_nil: true, prefix: true
... ... @@ -15,11 +17,11 @@ class Namespace &lt; ActiveRecord::Base
15 17 scope :root, where('type IS NULL')
16 18  
17 19 def self.search query
18   - where("name LIKE :query OR code LIKE :query", query: "%#{query}%")
  20 + where("name LIKE :query OR path LIKE :query", query: "%#{query}%")
19 21 end
20 22  
21 23 def to_param
22   - code
  24 + path
23 25 end
24 26  
25 27 def human_name
... ... @@ -27,7 +29,7 @@ class Namespace &lt; ActiveRecord::Base
27 29 end
28 30  
29 31 def ensure_dir_exist
30   - namespace_dir_path = File.join(Gitlab.config.git_base_path, code)
  32 + namespace_dir_path = File.join(Gitlab.config.git_base_path, path)
31 33 Dir.mkdir(namespace_dir_path) unless File.exists?(namespace_dir_path)
32 34 end
33 35 end
... ...
app/models/project.rb
... ... @@ -27,7 +27,7 @@ class Project &lt; ActiveRecord::Base
27 27 include Authority
28 28 include Team
29 29  
30   - attr_accessible :name, :path, :description, :code, :default_branch, :issues_enabled,
  30 + attr_accessible :name, :path, :description, :default_branch, :issues_enabled,
31 31 :wall_enabled, :merge_requests_enabled, :wiki_enabled, as: [:default, :admin]
32 32  
33 33 attr_accessible :namespace_id, as: :admin
... ... @@ -58,16 +58,16 @@ class Project &lt; ActiveRecord::Base
58 58 # Validations
59 59 validates :owner, presence: true
60 60 validates :description, length: { within: 0..2000 }
61   - validates :name, uniqueness: true, presence: true, length: { within: 0..255 }
62   - validates :path, uniqueness: true, presence: true, length: { within: 0..255 },
63   - format: { with: /\A[a-zA-Z][a-zA-Z0-9_\-\.]*\z/,
64   - message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }
65   - validates :code, presence: true, uniqueness: true, length: { within: 1..255 },
  61 + validates :name, presence: true, length: { within: 0..255 }
  62 + validates :path, presence: true, length: { within: 0..255 },
66 63 format: { with: /\A[a-zA-Z][a-zA-Z0-9_\-\.]*\z/,
67 64 message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }
68 65 validates :issues_enabled, :wall_enabled, :merge_requests_enabled,
69 66 :wiki_enabled, inclusion: { in: [true, false] }
70 67  
  68 + validates_uniqueness_of :name, scope: :namespace_id
  69 + validates_uniqueness_of :path, scope: :namespace_id
  70 +
71 71 validate :check_limit, :repo_name
72 72  
73 73 # Scopes
... ... @@ -81,20 +81,23 @@ class Project &lt; ActiveRecord::Base
81 81 end
82 82  
83 83 def search query
84   - where("projects.name LIKE :query OR projects.code LIKE :query OR projects.path LIKE :query", query: "%#{query}%")
  84 + where("projects.name LIKE :query OR projects.path LIKE :query", query: "%#{query}%")
85 85 end
86 86  
87 87 def create_by_user(params, user)
88   - namespace_id = params.delete(:namespace_id) || namespace.try(:id)
  88 + namespace_id = params.delete(:namespace_id)
  89 + namespace_id ||= current_user.namespace_id
89 90  
90 91 project = Project.new params
91 92  
92 93 Project.transaction do
93 94  
94   - # Build gitlab-hq code from GitLab HQ name
  95 + # Parametrize path for project
95 96 #
96   - slug = project.name.dup.parameterize
97   - project.code = project.path = slug
  97 + # Ex.
  98 + # 'GitLab HQ'.parameterize => "gitlab-hq"
  99 + #
  100 + project.path = project.name.dup.parameterize
98 101  
99 102 project.owner = user
100 103 project.namespace_id = namespace_id
... ... @@ -149,14 +152,14 @@ class Project &lt; ActiveRecord::Base
149 152  
150 153 def to_param
151 154 if namespace
152   - namespace.code + "/" + code
  155 + namespace.path + "/" + path
153 156 else
154   - code
  157 + path
155 158 end
156 159 end
157 160  
158 161 def web_url
159   - [Gitlab.config.url, code].join("/")
  162 + [Gitlab.config.url, path].join("/")
160 163 end
161 164  
162 165 def common_notes
... ... @@ -213,7 +216,7 @@ class Project &lt; ActiveRecord::Base
213 216  
214 217 def path_with_namespace
215 218 if namespace
216   - namespace.code + '/' + path
  219 + namespace.path + '/' + path
217 220 else
218 221 path
219 222 end
... ...
app/models/user.rb
... ... @@ -69,7 +69,8 @@ class User &lt; ActiveRecord::Base
69 69 before_save :ensure_authentication_token
70 70 alias_attribute :private_token, :authentication_token
71 71  
72   - delegate :code, to: :namespace, allow_nil: true, prefix: true
  72 + delegate :path, to: :namespace, allow_nil: true, prefix: true
  73 + delegate :id, to: :namespace, allow_nil: true, prefix: true
73 74  
74 75 # Scopes
75 76 scope :not_in_project, ->(project) { where("id not in (:ids)", ids: project.users.map(&:id) ) }
... ... @@ -121,7 +122,7 @@ class User &lt; ActiveRecord::Base
121 122  
122 123 def namespaces
123 124 namespaces = []
124   - namespaces << self.namespace
  125 + namespaces << self.namespace if self.namespace
125 126 namespaces = namespaces + Group.all if admin
126 127 namespaces
127 128 end
... ...
app/observers/project_observer.rb
1 1 class ProjectObserver < ActiveRecord::Observer
2   - def before_save(project)
  2 + def after_save(project)
  3 + project.update_repository
  4 +
3 5 # Move repository if namespace changed
4 6 if project.namespace_id_changed? and not project.new_record?
5 7 move_project(project)
6 8 end
7 9 end
8 10  
9   - def after_save(project)
10   - project.update_repository
11   - end
12   -
13 11 def after_destroy(project)
14 12 log_info("Project \"#{project.name}\" was removed")
15 13  
... ... @@ -27,8 +25,8 @@ class ProjectObserver &lt; ActiveRecord::Observer
27 25 end
28 26  
29 27 def move_project(project)
30   - old_dir = Namespace.find_by_id(project.namespace_id_was).try(:code) || ''
31   - new_dir = Namespace.find_by_id(project.namespace_id).try(:code) || ''
  28 + old_dir = Namespace.find_by_id(project.namespace_id_was).try(:path) || ''
  29 + new_dir = Namespace.find_by_id(project.namespace_id).try(:path) || ''
32 30  
33 31 # Create new dir if missing
34 32 new_dir_path = File.join(Gitlab.config.git_base_path, new_dir)
... ...
app/roles/push_observer.rb
... ... @@ -114,7 +114,7 @@ module PushObserver
114 114 id: commit.id,
115 115 message: commit.safe_message,
116 116 timestamp: commit.date.xmlschema,
117   - url: "#{Gitlab.config.url}/#{code}/commits/#{commit.id}",
  117 + url: "#{Gitlab.config.url}/#{path}/commits/#{commit.id}",
118 118 author: {
119 119 name: commit.author_name,
120 120 email: commit.author_email
... ...
app/roles/repository.rb
... ... @@ -87,7 +87,7 @@ module Repository
87 87 end
88 88  
89 89 def namespace_dir
90   - namespace.try(:code) || ''
  90 + namespace.try(:path) || ''
91 91 end
92 92  
93 93 def update_repository
... ... @@ -164,12 +164,12 @@ module Repository
164 164 return nil unless commit
165 165  
166 166 # Build file path
167   - file_name = self.code + "-" + commit.id.to_s + ".tar.gz"
168   - storage_path = Rails.root.join("tmp", "repositories", self.code)
  167 + file_name = self.path + "-" + commit.id.to_s + ".tar.gz"
  168 + storage_path = Rails.root.join("tmp", "repositories", self.path)
169 169 file_path = File.join(storage_path, file_name)
170 170  
171 171 # Put files into a directory before archiving
172   - prefix = self.code + "/"
  172 + prefix = self.path + "/"
173 173  
174 174 # Create file if not exists
175 175 unless File.exists?(file_path)
... ...
app/views/admin/dashboard/index.html.haml
... ... @@ -27,7 +27,7 @@
27 27 = link_to admin_projects_path do
28 28 %h1= Project.count
29 29 %hr
30   - = link_to 'New Project', new_admin_project_path, class: "btn small"
  30 + = link_to 'New Project', new_project_path, class: "btn small"
31 31 .span4
32 32 .ui-box
33 33 %h5 Users
... ...
app/views/admin/groups/_form.html.haml
... ... @@ -8,12 +8,12 @@
8 8 .input
9 9 = f.text_field :name, placeholder: "Example Group", class: "xxlarge"
10 10 .clearfix
11   - = f.label :code do
  11 + = f.label :path do
12 12 URL
13 13 .input
14 14 .input-prepend
15 15 %span.add-on= web_app_url + 'groups/'
16   - = f.text_field :code, placeholder: "example"
  16 + = f.text_field :path, placeholder: "example"
17 17  
18 18 .form-actions
19 19 = f.submit 'Save group', class: "btn save-btn"
... ...
app/views/admin/projects/_form.html.haml
... ... @@ -18,13 +18,6 @@
18 18 Path
19 19 .input
20 20 = text_field_tag :ppath, @project.path_to_repo, class: "xlarge", disabled: true
21   - .clearfix
22   - = f.label :code do
23   - URL
24   - .input
25   - .input-prepend
26   - %span.add-on= web_app_url
27   - = f.text_field :code, placeholder: "example"
28 21  
29 22 - unless project.new_record?
30 23 .clearfix
... ...
app/views/projects/_form.html.haml
... ... @@ -19,13 +19,6 @@
19 19 .input-prepend
20 20 %strong
21 21 = text_field_tag :ppath, @project.path_to_repo, class: "xlarge", disabled: true
22   - .clearfix
23   - = f.label :code do
24   - URL
25   - .input
26   - .input-prepend
27   - %span.add-on= web_app_url
28   - = f.text_field :code, placeholder: "example"
29 22  
30 23 - unless @project.new_record? || @project.heads.empty?
31 24 .clearfix
... ...
db/fixtures/development/001_admin.rb
1 1 unless User.count > 0
2 2 admin = User.create(
3   - :email => "admin@local.host",
4   - :name => "Administrator",
5   - :password => "5iveL!fe",
6   - :password_confirmation => "5iveL!fe"
  3 + email: "admin@local.host",
  4 + name: "Administrator",
  5 + username: 'root',
  6 + password: "5iveL!fe",
  7 + password_confirmation: "5iveL!fe"
7 8 )
8 9  
9 10 admin.projects_limit = 10000
... ...
db/fixtures/development/003_users.rb
1 1 User.seed(:id, [
2   - { :id => 2, :name => Faker::Internet.user_name, :email => Faker::Internet.email},
3   - { :id => 3, :name => Faker::Internet.user_name, :email => Faker::Internet.email},
4   - { :id => 4, :name => Faker::Internet.user_name, :email => Faker::Internet.email},
5   - { :id => 5, :name => Faker::Internet.user_name, :email => Faker::Internet.email},
6   - { :id => 6, :name => Faker::Internet.user_name, :email => Faker::Internet.email},
7   - { :id => 7, :name => Faker::Internet.user_name, :email => Faker::Internet.email},
8   - { :id => 8, :name => Faker::Internet.user_name, :email => Faker::Internet.email},
9   - { :id => 9, :name => Faker::Internet.user_name, :email => Faker::Internet.email}
  2 + { id: 2, username: Faker::Internet.user_name, name: Faker::Name.name, email: Faker::Internet.email},
  3 + { id: 3, username: Faker::Internet.user_name, name: Faker::Name.name, email: Faker::Internet.email},
  4 + { id: 4, username: Faker::Internet.user_name, name: Faker::Name.name, email: Faker::Internet.email},
  5 + { id: 5, username: Faker::Internet.user_name, name: Faker::Name.name, email: Faker::Internet.email},
  6 + { id: 6, username: Faker::Internet.user_name, name: Faker::Name.name, email: Faker::Internet.email},
  7 + { id: 7, username: Faker::Internet.user_name, name: Faker::Name.name, email: Faker::Internet.email},
  8 + { id: 8, username: Faker::Internet.user_name, name: Faker::Name.name, email: Faker::Internet.email},
  9 + { id: 9, username: Faker::Internet.user_name, name: Faker::Name.name, email: Faker::Internet.email}
10 10 ])
11 11  
... ...
db/fixtures/production/001_admin.rb
1 1 admin = User.create(
2   - :email => "admin@local.host",
3   - :name => "Administrator",
4   - :password => "5iveL!fe",
5   - :password_confirmation => "5iveL!fe"
  2 + email: "admin@local.host",
  3 + name: "Administrator",
  4 + username: 'root',
  5 + password: "5iveL!fe",
  6 + password_confirmation: "5iveL!fe"
6 7 )
7 8  
8 9 admin.projects_limit = 10000
... ...
db/migrate/20121123164910_rename_code_to_path.rb 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +class RenameCodeToPath < ActiveRecord::Migration
  2 + def up
  3 + remove_column :projects, :code
  4 + rename_column :namespaces, :code, :path
  5 + end
  6 +
  7 + def down
  8 + add_column :projects, :code, :string
  9 + rename_column :namespaces, :path, :code
  10 + end
  11 +end
... ...
db/schema.rb
... ... @@ -11,7 +11,7 @@
11 11 #
12 12 # It's strongly recommended to check this file into your version control system.
13 13  
14   -ActiveRecord::Schema.define(:version => 20121123104937) do
  14 +ActiveRecord::Schema.define(:version => 20121123164910) do
15 15  
16 16 create_table "events", :force => true do |t|
17 17 t.string "target_type"
... ... @@ -82,7 +82,7 @@ ActiveRecord::Schema.define(:version =&gt; 20121123104937) do
82 82  
83 83 create_table "namespaces", :force => true do |t|
84 84 t.string "name", :null => false
85   - t.string "code", :null => false
  85 + t.string "path", :null => false
86 86 t.integer "owner_id", :null => false
87 87 t.datetime "created_at", :null => false
88 88 t.datetime "updated_at", :null => false
... ... @@ -111,7 +111,6 @@ ActiveRecord::Schema.define(:version =&gt; 20121123104937) do
111 111 t.datetime "created_at", :null => false
112 112 t.datetime "updated_at", :null => false
113 113 t.boolean "private_flag", :default => true, :null => false
114   - t.string "code"
115 114 t.integer "owner_id"
116 115 t.string "default_branch"
117 116 t.boolean "issues_enabled", :default => true, :null => false
... ...
lib/api/helpers.rb
... ... @@ -6,7 +6,7 @@ module Gitlab
6 6  
7 7 def user_project
8 8 if @project ||= current_user.projects.find_by_id(params[:id]) ||
9   - current_user.projects.find_by_code(params[:id])
  9 + current_user.projects.find_by_path(params[:id])
10 10 else
11 11 not_found!
12 12 end
... ...
lib/gitlab/auth.rb
... ... @@ -34,6 +34,7 @@ module Gitlab
34 34 extern_uid: uid,
35 35 provider: provider,
36 36 name: name,
  37 + username: email.match(/^[^@]*/)[0],
37 38 email: email,
38 39 password: password,
39 40 password_confirmation: password,
... ...
spec/factories.rb
... ... @@ -12,6 +12,7 @@ FactoryGirl.define do
12 12 factory :user, aliases: [:author, :assignee, :owner] do
13 13 email { Faker::Internet.email }
14 14 name
  15 + username 'john'
15 16 password "123456"
16 17 password_confirmation { password }
17 18  
... ...
spec/observers/user_observer_spec.rb
... ... @@ -13,7 +13,7 @@ describe UserObserver do
13 13 end
14 14  
15 15 context 'when a new user is created' do
16   - let(:user) { double(:user, id: 42, password: 'P@ssword!', name: 'John', email: 'u@mail.local') }
  16 + let(:user) { double(:user, id: 42, password: 'P@ssword!', name: 'John', email: 'u@mail.local', username: 'root') }
17 17 let(:notification) { double :notification }
18 18  
19 19 it 'sends an email' do
... ...
spec/routing/admin_routing_spec.rb
... ... @@ -78,14 +78,6 @@ describe Admin::ProjectsController, &quot;routing&quot; do
78 78 get("/admin/projects").should route_to('admin/projects#index')
79 79 end
80 80  
81   - it "to #create" do
82   - post("/admin/projects").should route_to('admin/projects#create')
83   - end
84   -
85   - it "to #new" do
86   - get("/admin/projects/new").should route_to('admin/projects#new')
87   - end
88   -
89 81 it "to #edit" do
90 82 get("/admin/projects/gitlab/edit").should route_to('admin/projects#edit', id: 'gitlab')
91 83 end
... ...