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.

issue_relations_controller.rb 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 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 IssueRelationsController < ApplicationController
  19. helper :issues
  20. before_action :find_issue, :authorize, :only => [:index, :create]
  21. before_action :find_relation, :only => [:show, :destroy]
  22. accept_api_auth :index, :show, :create, :destroy
  23. def index
  24. @relations = @issue.relations
  25. respond_to do |format|
  26. format.html {head 200}
  27. format.api
  28. end
  29. end
  30. def show
  31. raise Unauthorized unless @relation.visible?
  32. respond_to do |format|
  33. format.html {head 200}
  34. format.api
  35. end
  36. end
  37. def create
  38. saved = false
  39. params_relation = params[:relation]
  40. unsaved_relations = []
  41. relation_issues_to_id.each do |issue_to_id|
  42. params_relation[:issue_to_id] = issue_to_id
  43. @relation = IssueRelation.new
  44. @relation.issue_from = @issue
  45. @relation.safe_attributes = params_relation
  46. @relation.init_journals(User.current)
  47. unless saved = @relation.save
  48. saved = false
  49. unsaved_relations << @relation
  50. end
  51. end
  52. respond_to do |format|
  53. format.html {redirect_to issue_path(@issue)}
  54. format.js do
  55. @relations = @issue.reload.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible?}
  56. @unsaved_relations = unsaved_relations
  57. end
  58. format.api do
  59. if saved
  60. render :action => 'show', :status => :created, :location => relation_url(@relation)
  61. else
  62. render_validation_errors(@relation)
  63. end
  64. end
  65. end
  66. end
  67. def destroy
  68. raise Unauthorized unless @relation.deletable?
  69. @relation.init_journals(User.current)
  70. @relation.destroy
  71. respond_to do |format|
  72. format.html {redirect_to issue_path(@relation.issue_from)}
  73. format.js
  74. format.api {render_api_ok}
  75. end
  76. end
  77. private
  78. def find_issue
  79. @issue = Issue.find(params[:issue_id])
  80. @project = @issue.project
  81. rescue ActiveRecord::RecordNotFound
  82. render_404
  83. end
  84. def find_relation
  85. @relation = IssueRelation.find(params[:id])
  86. rescue ActiveRecord::RecordNotFound
  87. render_404
  88. end
  89. def relation_issues_to_id
  90. params[:relation].require(:issue_to_id).split(',').reject(&:blank?)
  91. rescue ActionController::ParameterMissing => e
  92. # We return a empty array just to loop once and return a validation error
  93. # ToDo: Find a better method to return an error if the param is missing.
  94. ['']
  95. end
  96. end