Commit e29ccece332e57c9fb6c532a3680e3b457e3a301

Authored by Dmitriy Zaporozhets
1 parent ced242a2

Namespace model added. Migration to convert exit project/groups

app/models/group.rb
@@ -10,26 +10,7 @@ @@ -10,26 +10,7 @@
10 # updated_at :datetime not null 10 # updated_at :datetime not null
11 # 11 #
12 12
13 -class Group < ActiveRecord::Base  
14 - attr_accessible :code, :name, :owner_id  
15 -  
16 - has_many :projects  
17 - belongs_to :owner, class_name: "User"  
18 -  
19 - validates :name, presence: true, uniqueness: true  
20 - validates :code, presence: true, uniqueness: true  
21 - validates :owner, presence: true  
22 -  
23 - delegate :name, to: :owner, allow_nil: true, prefix: true  
24 -  
25 - def self.search query  
26 - where("name LIKE :query OR code LIKE :query", query: "%#{query}%")  
27 - end  
28 -  
29 - def to_param  
30 - code  
31 - end  
32 - 13 +class Group < Namespace
33 def users 14 def users
34 User.joins(:users_projects).where(users_projects: {project_id: project_ids}).uniq 15 User.joins(:users_projects).where(users_projects: {project_id: project_ids}).uniq
35 end 16 end
app/models/namespace.rb 0 → 100644
@@ -0,0 +1,20 @@ @@ -0,0 +1,20 @@
  1 +class Namespace < ActiveRecord::Base
  2 + attr_accessible :code, :name, :owner_id
  3 +
  4 + has_many :projects
  5 + belongs_to :owner, class_name: "User"
  6 +
  7 + validates :name, presence: true, uniqueness: true
  8 + validates :code, presence: true, uniqueness: true
  9 + validates :owner, presence: true
  10 +
  11 + delegate :name, to: :owner, allow_nil: true, prefix: true
  12 +
  13 + def self.search query
  14 + where("name LIKE :query OR code LIKE :query", query: "%#{query}%")
  15 + end
  16 +
  17 + def to_param
  18 + code
  19 + end
  20 +end
app/models/project.rb
@@ -32,7 +32,8 @@ class Project &lt; ActiveRecord::Base @@ -32,7 +32,8 @@ class Project &lt; ActiveRecord::Base
32 attr_accessor :error_code 32 attr_accessor :error_code
33 33
34 # Relations 34 # Relations
35 - belongs_to :group 35 + belongs_to :group, foreign_key: "namespace_id", conditions: 'type = Group'
  36 + belongs_to :namespace
36 belongs_to :owner, class_name: "User" 37 belongs_to :owner, class_name: "User"
37 has_many :users, through: :users_projects 38 has_many :users, through: :users_projects
38 has_many :events, dependent: :destroy 39 has_many :events, dependent: :destroy
@@ -192,4 +193,12 @@ class Project &lt; ActiveRecord::Base @@ -192,4 +193,12 @@ class Project &lt; ActiveRecord::Base
192 def gitlab_ci? 193 def gitlab_ci?
193 gitlab_ci_service && gitlab_ci_service.active 194 gitlab_ci_service && gitlab_ci_service.active
194 end 195 end
  196 +
  197 + def path_with_namespace
  198 + if namespace
  199 + namespace.code + '/' + path
  200 + else
  201 + path
  202 + end
  203 + end
195 end 204 end
db/migrate/20121122145155_convert_group_to_namespace.rb 0 → 100644
@@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
  1 +class ConvertGroupToNamespace < ActiveRecord::Migration
  2 + def up
  3 + rename_table 'groups', 'namespaces'
  4 + add_column :namespaces, :type, :string, null: true
  5 +
  6 + # Migrate old groups
  7 + Namespace.update_all(type: 'Group')
  8 + end
  9 +
  10 + def down
  11 + raise 'Rollback is not allowed'
  12 + end
  13 +end
db/migrate/20121122150932_add_namespace_id_to_project.rb 0 → 100644
@@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
  1 +class AddNamespaceIdToProject < ActiveRecord::Migration
  2 + def change
  3 + rename_column :projects, :group_id, :namespace_id
  4 + end
  5 +end
@@ -11,7 +11,7 @@ @@ -11,7 +11,7 @@
11 # 11 #
12 # It's strongly recommended to check this file into your version control system. 12 # It's strongly recommended to check this file into your version control system.
13 13
14 -ActiveRecord::Schema.define(:version => 20121120113838) do 14 +ActiveRecord::Schema.define(:version => 20121122150932) do
15 15
16 create_table "events", :force => true do |t| 16 create_table "events", :force => true do |t|
17 t.string "target_type" 17 t.string "target_type"
@@ -25,14 +25,6 @@ ActiveRecord::Schema.define(:version =&gt; 20121120113838) do @@ -25,14 +25,6 @@ ActiveRecord::Schema.define(:version =&gt; 20121120113838) do
25 t.integer "author_id" 25 t.integer "author_id"
26 end 26 end
27 27
28 - create_table "groups", :force => true do |t|  
29 - t.string "name", :null => false  
30 - t.string "code", :null => false  
31 - t.integer "owner_id", :null => false  
32 - t.datetime "created_at", :null => false  
33 - t.datetime "updated_at", :null => false  
34 - end  
35 -  
36 create_table "issues", :force => true do |t| 28 create_table "issues", :force => true do |t|
37 t.string "title" 29 t.string "title"
38 t.integer "assignee_id" 30 t.integer "assignee_id"
@@ -88,6 +80,15 @@ ActiveRecord::Schema.define(:version =&gt; 20121120113838) do @@ -88,6 +80,15 @@ ActiveRecord::Schema.define(:version =&gt; 20121120113838) do
88 t.datetime "updated_at", :null => false 80 t.datetime "updated_at", :null => false
89 end 81 end
90 82
  83 + create_table "namespaces", :force => true do |t|
  84 + t.string "name", :null => false
  85 + t.string "code", :null => false
  86 + t.integer "owner_id", :null => false
  87 + t.datetime "created_at", :null => false
  88 + t.datetime "updated_at", :null => false
  89 + t.string "type"
  90 + end
  91 +
91 create_table "notes", :force => true do |t| 92 create_table "notes", :force => true do |t|
92 t.text "note" 93 t.text "note"
93 t.string "noteable_id" 94 t.string "noteable_id"
@@ -117,7 +118,7 @@ ActiveRecord::Schema.define(:version =&gt; 20121120113838) do @@ -117,7 +118,7 @@ ActiveRecord::Schema.define(:version =&gt; 20121120113838) do
117 t.boolean "wall_enabled", :default => true, :null => false 118 t.boolean "wall_enabled", :default => true, :null => false
118 t.boolean "merge_requests_enabled", :default => true, :null => false 119 t.boolean "merge_requests_enabled", :default => true, :null => false
119 t.boolean "wiki_enabled", :default => true, :null => false 120 t.boolean "wiki_enabled", :default => true, :null => false
120 - t.integer "group_id" 121 + t.integer "namespace_id"
121 end 122 end
122 123
123 create_table "protected_branches", :force => true do |t| 124 create_table "protected_branches", :force => true do |t|