You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

auto_completes_controller.rb 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class AutoCompletesController < ApplicationController
  19. before_action :find_project
  20. def issues
  21. issues = []
  22. q = (params[:q] || params[:term]).to_s.strip
  23. status = params[:status].to_s
  24. issue_id = params[:issue_id].to_s
  25. if q.present?
  26. scope = Issue.cross_project_scope(@project, params[:scope]).visible
  27. if status.present?
  28. scope = scope.open(status == 'o')
  29. end
  30. if issue_id.present?
  31. scope = scope.where.not(:id => issue_id.to_i)
  32. end
  33. if q.match(/\A#?(\d+)\z/)
  34. issues << scope.find_by_id($1.to_i)
  35. end
  36. issues += scope.like(q).order(:id => :desc).limit(10).to_a
  37. issues.compact!
  38. end
  39. render :json => format_issues_json(issues)
  40. end
  41. private
  42. def find_project
  43. if params[:project_id].present?
  44. @project = Project.find(params[:project_id])
  45. end
  46. rescue ActiveRecord::RecordNotFound
  47. render_404
  48. end
  49. def format_issues_json(issues)
  50. issues.map {|issue| {
  51. 'id' => issue.id,
  52. 'label' => "#{issue.tracker} ##{issue.id}: #{issue.subject.to_s.truncate(60)}",
  53. 'value' => issue.id
  54. }
  55. }
  56. end
  57. end