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 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 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. scope = Issue.cross_project_scope(@project, params[:scope]).visible
  26. scope = scope.open(status == 'o') if status.present?
  27. scope = scope.where.not(:id => issue_id.to_i) if issue_id.present?
  28. if q.present?
  29. if q =~ /\A#?(\d+)\z/
  30. issues << scope.find_by(:id => $1.to_i)
  31. end
  32. issues += scope.like(q).order(:id => :desc).limit(10).to_a
  33. issues.compact!
  34. else
  35. issues += scope.order(:id => :desc).limit(10).to_a
  36. end
  37. render :json => format_issues_json(issues)
  38. end
  39. def wiki_pages
  40. q = params[:q].to_s.strip
  41. wiki = Wiki.find_by(project: @project)
  42. if wiki.nil? || !User.current.allowed_to?(:view_wiki_pages, @project)
  43. render json: []
  44. return
  45. end
  46. scope = wiki.pages.reorder(id: :desc)
  47. wiki_pages =
  48. if q.present?
  49. scope.where("LOWER(#{WikiPage.table_name}.title) LIKE LOWER(?)", "%#{q}%").limit(10).to_a
  50. else
  51. scope.limit(10).to_a
  52. end
  53. render json: format_wiki_pages_json(wiki_pages)
  54. end
  55. private
  56. def find_project
  57. if params[:project_id].present?
  58. @project = Project.find(params[:project_id])
  59. end
  60. rescue ActiveRecord::RecordNotFound
  61. render_404
  62. end
  63. def format_issues_json(issues)
  64. issues.map do |issue|
  65. {
  66. 'id' => issue.id,
  67. 'label' => "#{issue.tracker} ##{issue.id}: #{issue.subject.to_s.truncate(255)}",
  68. 'value' => issue.id
  69. }
  70. end
  71. end
  72. def format_wiki_pages_json(wiki_pages)
  73. wiki_pages.map do |wiki_page|
  74. {
  75. 'id' => wiki_page.id,
  76. 'label' => wiki_page.title.to_s.truncate(255),
  77. 'value' => wiki_page.title
  78. }
  79. end
  80. end
  81. end