#73: Increatse performance for #autocomplete_to_project action

This commit is contained in:
Vokhmin Alexey V 2013-04-03 20:03:52 +04:00
parent 7be2d6f6bc
commit 62ff6312b4
2 changed files with 17 additions and 5 deletions

View File

@ -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

View File

@ -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