From 62ff6312b4173f3e5662f7a08d8afb4f8d45cce2 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Wed, 3 Apr 2013 20:03:52 +0400 Subject: [PATCH] #73: Increatse performance for #autocomplete_to_project action --- .../projects/pull_requests_controller.rb | 16 ++++++++++++---- lib/modules/models/owner.rb | 6 +++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/app/controllers/projects/pull_requests_controller.rb b/app/controllers/projects/pull_requests_controller.rb index 5a4e7cbd9..5c9ba0e38 100644 --- a/app/controllers/projects/pull_requests_controller.rb +++ b/app/controllers/projects/pull_requests_controller.rb @@ -115,9 +115,17 @@ class Projects::PullRequestsController < Projects::BaseController end def autocomplete_to_project - items = Project.accessible_by(current_ability, :membered) | @project.ancestors - term = Regexp.new(Regexp.escape params[:term].downcase) - items.select! {|e| term.match(e.name_with_owner.downcase) && e.repo.branches.count > 0} + items = [] + term = params[:term].to_s.strip.downcase + condition = ["lower(concat(owners.uname, '/', projects.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") + end + items = items.uniq{|i| i.id}.select{|e| e.repo.branches.count > 0} render :json => json_for_autocomplete_base(items) end @@ -129,7 +137,7 @@ class Projects::PullRequestsController < Projects::BaseController def json_for_autocomplete_base items items.collect do |project| - hash = {"id" => project.id.to_s, "label" => project.name_with_owner, "value" => project.name_with_owner} + hash = {:id => project.id.to_s, :label => project.name_with_owner, :value => project.name_with_owner} hash[:get_refs_url] = project_refs_list_path(project) hash end diff --git a/lib/modules/models/owner.rb b/lib/modules/models/owner.rb index e88ac3f5f..5108b81c6 100644 --- a/lib/modules/models/owner.rb +++ b/lib/modules/models/owner.rb @@ -5,11 +5,15 @@ 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' + after_create lambda { relations.create :actor_id => owner.id, :actor_type => owner.class.to_s, :role => 'admin' } end def name_with_owner - "#{owner.respond_to?(:uname) ? owner.uname : owner.name}/#{self.name}" + o = owner_type == 'User' ? owner_user : owner_group + "#{o.respond_to?(:uname) ? o.uname : o.name}/#{self.name}" end end end