|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +class SubjectAutocompleteController < ApplicationController |
| 3 | + |
| 4 | + if Rails::VERSION::MAJOR >= 4 |
| 5 | + before_action :find_project, :init |
| 6 | + else |
| 7 | + before_filter :find_project, :init |
| 8 | + end |
| 9 | + |
| 10 | + def init |
| 11 | + @issue_status_closed = IssueStatus.where('is_closed=?', true).pluck :id |
| 12 | + @issue_status_open = IssueStatus.select('id').where('is_closed=?', false).pluck :id |
| 13 | + end |
| 14 | + |
| 15 | + def get_matches |
| 16 | + # autocomplete for the issue subject field. |
| 17 | + limit_count = 15 |
| 18 | + default_closed_past_days = 30 |
| 19 | + # load closed tickets, show issue id |
| 20 | + @issues = [] |
| 21 | + q = params[:term].to_s.strip |
| 22 | + if q.present? |
| 23 | + if @project.nil? |
| 24 | + scope = Issue.visible |
| 25 | + else |
| 26 | + if @project.parent |
| 27 | + project_siblings = Project.where(:parent_id => @project.parent.id).map{|e| e.id } |
| 28 | + project_siblings_children = Project.where(:parent_id => project_siblings).map{|e| e.id } |
| 29 | + projects = project_siblings + project_siblings_children << @project.parent[:id] |
| 30 | + else |
| 31 | + project_children = Project.where(:parent_id => @project.id).map{|e| e.id } |
| 32 | + projects = project_children << @project.id |
| 33 | + end |
| 34 | + scope = Issue.where(:project_id => projects).visible |
| 35 | + end |
| 36 | + if q.match(/\A#?(\d+)\z/) |
| 37 | + @issues << scope.find_by_id($1.to_i) |
| 38 | + end |
| 39 | + past_days = params[:closed_past_days] ? params[:closed_past_days].to_i : default_closed_past_days |
| 40 | + time_now = DateTime.now |
| 41 | + # use ruby regexp. not all databases support regexp in sql |
| 42 | + q = q.gsub(/[^a-zA-Z0-9# ]/, "").split(" ").map{|e| |
| 43 | + e = Issue.connection.quote("%#{e}%") |
| 44 | + "lower(#{Issue.table_name}.subject) like #{e}"}.join(" and ") |
| 45 | + @issues += scope |
| 46 | + .where("#{q} and " + |
| 47 | + "(issues.status_id in(?) or (issues.status_id in(?) and issues.updated_on between ? and ?))", |
| 48 | + @issue_status_open, @issue_status_closed, time_now - past_days, time_now) |
| 49 | + .order("#{Issue.table_name}.id desc") |
| 50 | + .limit(limit_count) |
| 51 | + @issues.compact! |
| 52 | + versions = {} |
| 53 | + Version.select("id,name").each{|e| versions[e.id] = e.name } |
| 54 | + end |
| 55 | + render :json => @issues.map {|e| |
| 56 | + label = "##{e[:id]} #{e[:subject]}" |
| 57 | + if e.fixed_version_id then label = "#{versions[e.fixed_version_id]} » #{label}" end |
| 58 | + { |
| 59 | + "label" => label, |
| 60 | + "value" => "", |
| 61 | + "issue_url" => url_for(:controller => "issues", :action => "show", :id => e[:id]), |
| 62 | + "is_closed" => e.closed? |
| 63 | + } |
| 64 | + } |
| 65 | + end |
| 66 | + |
| 67 | + private |
| 68 | + |
| 69 | + def find_project |
| 70 | + if params[:project_id].present? |
| 71 | + @project = Project.find(params[:project_id]) |
| 72 | + end |
| 73 | + rescue ActiveRecord::RecordNotFound |
| 74 | + render_404 |
| 75 | + end |
| 76 | +end |
0 commit comments