From 9dcd08b86879d77e203cf893d8a604a53bf025db Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Thu, 4 Apr 2013 01:06:13 +0400 Subject: [PATCH] #73: add owner_uname into Project --- .../projects/pull_requests_controller.rb | 8 ++----- app/models/platform.rb | 1 - app/models/project.rb | 6 +++++- ...03202853_add_name_with_owner_to_project.rb | 21 +++++++++++++++++++ db/schema.rb | 3 ++- lib/modules/models/owner.rb | 9 +++----- 6 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 db/migrate/20130403202853_add_name_with_owner_to_project.rb diff --git a/app/controllers/projects/pull_requests_controller.rb b/app/controllers/projects/pull_requests_controller.rb index 5c9ba0e38..6701bc12d 100644 --- a/app/controllers/projects/pull_requests_controller.rb +++ b/app/controllers/projects/pull_requests_controller.rb @@ -117,13 +117,9 @@ class Projects::PullRequestsController < Projects::BaseController def autocomplete_to_project items = [] term = params[:term].to_s.strip.downcase - condition = ["lower(concat(owners.uname, '/', projects.name)) ILIKE ?", "%#{term}%"] + condition = ["lower(concat(owner_uname, '/', name)) ILIKE ?", "%#{term}%"] [Project.accessible_by(current_ability, :membered), @project.ancestors].each do |p| - p = p.where(condition) - items.concat p.where(:owner_type => 'User').includes(:owner_user). - joins("INNER JOIN users as owners ON projects.owner_id = owners.id") - items.concat p.where(:owner_type => 'Group').includes(:owner_group). - joins("INNER JOIN groups as owners ON projects.owner_id = owners.id") + items.concat p.where(condition) end items = items.uniq{|i| i.id}.select{|e| e.repo.branches.count > 0} render :json => json_for_autocomplete_base(items) diff --git a/app/models/platform.rb b/app/models/platform.rb index 29d4f8d2f..7c4d425be 100644 --- a/app/models/platform.rb +++ b/app/models/platform.rb @@ -19,7 +19,6 @@ class Platform < ActiveRecord::Base has_many :mass_builds validates :description, :presence => true - validates :owner, :presence => true validates :visibility, :presence => true, :inclusion => {:in => VISIBILITIES} validates :name, :uniqueness => {:case_sensitive => false}, :presence => true, :format => { :with => /\A[a-zA-Z0-9_\-\.]+\z/ } validates :distrib_type, :presence => true, :inclusion => {:in => APP_CONFIG['distr_types']} diff --git a/app/models/project.rb b/app/models/project.rb index 6d5d5e8f2..2e25b4230 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -28,7 +28,6 @@ class Project < ActiveRecord::Base validates :name, :uniqueness => {:scope => [:owner_id, :owner_type], :case_sensitive => false}, :presence => true, :format => {:with => /\A#{NAME_REGEXP}\z/, :message => I18n.t("activerecord.errors.project.uname")} - validates :owner, :presence => true validates :maintainer_id, :presence => true, :unless => :new_record? validates :visibility, :presence => true, :inclusion => {:in => VISIBILITIES} validate { errors.add(:base, :can_have_less_or_equal, :count => MAX_OWN_PROJECTS) if owner.projects.size >= MAX_OWN_PROJECTS } @@ -58,6 +57,7 @@ class Project < ActiveRecord::Base } before_validation :truncate_name, :on => :create + before_save lambda { self.owner_uname = owner.uname if owner_id_changed? || owner_type_changed? } before_create :set_maintainer after_save :attach_to_personal_repository after_update :set_new_git_head @@ -81,6 +81,10 @@ class Project < ActiveRecord::Base end end + def name_with_owner + "#{owner_uname}/#{name}" + end + def to_param name end diff --git a/db/migrate/20130403202853_add_name_with_owner_to_project.rb b/db/migrate/20130403202853_add_name_with_owner_to_project.rb new file mode 100644 index 000000000..7119ac232 --- /dev/null +++ b/db/migrate/20130403202853_add_name_with_owner_to_project.rb @@ -0,0 +1,21 @@ +class AddNameWithOwnerToProject < ActiveRecord::Migration + def up + add_column :projects, :owner_uname, :string + + execute <<-SQL + UPDATE projects SET owner_uname = owners.uname + FROM users as owners + WHERE projects.owner_type = 'User' AND projects.owner_id = owners.id + SQL + + execute <<-SQL + UPDATE projects SET owner_uname = owners.uname + FROM groups as owners + WHERE projects.owner_type = 'Group' AND projects.owner_id = owners.id + SQL + end + + def down + remove_column :projects, :owner_uname + end +end diff --git a/db/schema.rb b/db/schema.rb index 723c833d8..7462f4f00 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 => 20130328112110) do +ActiveRecord::Schema.define(:version => 20130403202853) do create_table "activity_feeds", :force => true do |t| t.integer "user_id", :null => false @@ -387,6 +387,7 @@ ActiveRecord::Schema.define(:version => 20130328112110) do t.integer "build_count", :default => 0, :null => false t.integer "maintainer_id" t.boolean "publish_i686_into_x86_64", :default => false + t.string "owner_uname" end add_index "projects", ["owner_id", "name", "owner_type"], :name => "index_projects_on_name_and_owner_id_and_owner_type", :unique => true, :case_sensitive => false diff --git a/lib/modules/models/owner.rb b/lib/modules/models/owner.rb index 5108b81c6..24ac4340c 100644 --- a/lib/modules/models/owner.rb +++ b/lib/modules/models/owner.rb @@ -5,16 +5,13 @@ module Modules extend ActiveSupport::Concern included do - belongs_to :owner_user, :class_name => 'User', :foreign_key => 'owner_id' - belongs_to :owner_group, :class_name => 'Group', :foreign_key => 'owner_id' + belongs_to :owner_user, :class_name => 'User', :foreign_key => 'owner_id' + belongs_to :owner_group, :class_name => 'Group', :foreign_key => 'owner_id' + validates :owner, :presence => true after_create lambda { relations.create :actor_id => owner.id, :actor_type => owner.class.to_s, :role => 'admin' } end - def name_with_owner - o = owner_type == 'User' ? owner_user : owner_group - "#{o.respond_to?(:uname) ? o.uname : o.name}/#{self.name}" - end end end end