From e29ccece332e57c9fb6c532a3680e3b457e3a301 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Thu, 22 Nov 2012 21:34:16 +0300 Subject: [PATCH] Namespace model added. Migration to convert exit project/groups --- app/models/group.rb | 21 +------------------ app/models/namespace.rb | 20 ++++++++++++++++++ app/models/project.rb | 11 +++++++++- ...121122145155_convert_group_to_namespace.rb | 13 ++++++++++++ ...21122150932_add_namespace_id_to_project.rb | 5 +++++ db/schema.rb | 21 ++++++++++--------- 6 files changed, 60 insertions(+), 31 deletions(-) create mode 100644 app/models/namespace.rb create mode 100644 db/migrate/20121122145155_convert_group_to_namespace.rb create mode 100644 db/migrate/20121122150932_add_namespace_id_to_project.rb diff --git a/app/models/group.rb b/app/models/group.rb index 1ff6872f..683606fa 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -10,26 +10,7 @@ # updated_at :datetime not null # -class Group < ActiveRecord::Base - attr_accessible :code, :name, :owner_id - - has_many :projects - belongs_to :owner, class_name: "User" - - validates :name, presence: true, uniqueness: true - validates :code, presence: true, uniqueness: true - validates :owner, presence: true - - delegate :name, to: :owner, allow_nil: true, prefix: true - - def self.search query - where("name LIKE :query OR code LIKE :query", query: "%#{query}%") - end - - def to_param - code - end - +class Group < Namespace def users User.joins(:users_projects).where(users_projects: {project_id: project_ids}).uniq end diff --git a/app/models/namespace.rb b/app/models/namespace.rb new file mode 100644 index 00000000..bdf624ef --- /dev/null +++ b/app/models/namespace.rb @@ -0,0 +1,20 @@ +class Namespace < ActiveRecord::Base + attr_accessible :code, :name, :owner_id + + has_many :projects + belongs_to :owner, class_name: "User" + + validates :name, presence: true, uniqueness: true + validates :code, presence: true, uniqueness: true + validates :owner, presence: true + + delegate :name, to: :owner, allow_nil: true, prefix: true + + def self.search query + where("name LIKE :query OR code LIKE :query", query: "%#{query}%") + end + + def to_param + code + end +end diff --git a/app/models/project.rb b/app/models/project.rb index 3cbc9417..ef4905f6 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -32,7 +32,8 @@ class Project < ActiveRecord::Base attr_accessor :error_code # Relations - belongs_to :group + belongs_to :group, foreign_key: "namespace_id", conditions: 'type = Group' + belongs_to :namespace belongs_to :owner, class_name: "User" has_many :users, through: :users_projects has_many :events, dependent: :destroy @@ -192,4 +193,12 @@ class Project < ActiveRecord::Base def gitlab_ci? gitlab_ci_service && gitlab_ci_service.active end + + def path_with_namespace + if namespace + namespace.code + '/' + path + else + path + end + end end diff --git a/db/migrate/20121122145155_convert_group_to_namespace.rb b/db/migrate/20121122145155_convert_group_to_namespace.rb new file mode 100644 index 00000000..fc8b023d --- /dev/null +++ b/db/migrate/20121122145155_convert_group_to_namespace.rb @@ -0,0 +1,13 @@ +class ConvertGroupToNamespace < ActiveRecord::Migration + def up + rename_table 'groups', 'namespaces' + add_column :namespaces, :type, :string, null: true + + # Migrate old groups + Namespace.update_all(type: 'Group') + end + + def down + raise 'Rollback is not allowed' + end +end diff --git a/db/migrate/20121122150932_add_namespace_id_to_project.rb b/db/migrate/20121122150932_add_namespace_id_to_project.rb new file mode 100644 index 00000000..904f3aa3 --- /dev/null +++ b/db/migrate/20121122150932_add_namespace_id_to_project.rb @@ -0,0 +1,5 @@ +class AddNamespaceIdToProject < ActiveRecord::Migration + def change + rename_column :projects, :group_id, :namespace_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 27b1f4aa..90b027d4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20121120113838) do +ActiveRecord::Schema.define(:version => 20121122150932) do create_table "events", :force => true do |t| t.string "target_type" @@ -25,14 +25,6 @@ ActiveRecord::Schema.define(:version => 20121120113838) do t.integer "author_id" end - create_table "groups", :force => true do |t| - t.string "name", :null => false - t.string "code", :null => false - t.integer "owner_id", :null => false - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false - end - create_table "issues", :force => true do |t| t.string "title" t.integer "assignee_id" @@ -88,6 +80,15 @@ ActiveRecord::Schema.define(:version => 20121120113838) do t.datetime "updated_at", :null => false end + create_table "namespaces", :force => true do |t| + t.string "name", :null => false + t.string "code", :null => false + t.integer "owner_id", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + t.string "type" + end + create_table "notes", :force => true do |t| t.text "note" t.string "noteable_id" @@ -117,7 +118,7 @@ ActiveRecord::Schema.define(:version => 20121120113838) do t.boolean "wall_enabled", :default => true, :null => false t.boolean "merge_requests_enabled", :default => true, :null => false t.boolean "wiki_enabled", :default => true, :null => false - t.integer "group_id" + t.integer "namespace_id" end create_table "protected_branches", :force => true do |t|