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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 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. begin
  48. saved = @relation.save
  49. rescue ActiveRecord::RecordNotUnique
  50. @relation.errors.add :base, :taken
  51. end
  52. unsaved_relations << @relation unless saved
  53. end
  54. respond_to do |format|
  55. format.html {redirect_to issue_path(@issue)}
  56. format.js do
  57. @relations = @issue.reload.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible?}
  58. @unsaved_relations = unsaved_relations
  59. end
  60. format.api do
  61. if saved
  62. render :action => 'show', :status => :created, :location => relation_url(@relation)
  63. else
  64. render_validation_errors(@relation)
  65. end
  66. end
  67. end
  68. end
  69. def destroy
  70. raise Unauthorized unless @relation.deletable?
  71. @relation.init_journals(User.current)
  72. @relation.destroy
  73. respond_to do |format|
  74. format.html {redirect_to issue_path(@relation.issue_from)}
  75. format.js
  76. format.api {render_api_ok}
  77. end
  78. end
  79. private
  80. def find_issue
  81. @issue = Issue.find(params[:issue_id])
  82. @project = @issue.project
  83. rescue ActiveRecord::RecordNotFound
  84. render_404
  85. end
  86. def find_relation
  87. @relation = IssueRelation.find(params[:id])
  88. rescue ActiveRecord::RecordNotFound
  89. render_404
  90. end
  91. def relation_issues_to_id
  92. issue_to_id = params[:relation].require(:issue_to_id)
  93. case issue_to_id
  94. when String
  95. issue_to_id = issue_to_id.split(',').reject(&:blank?)
  96. when Integer
  97. issue_to_id = [issue_to_id]
  98. end
  99. issue_to_id
  100. rescue ActionController::ParameterMissing => e
  101. # We return a empty array just to loop once and return a validation error
  102. # ToDo: Find a better method to return an error if the param is missing.
  103. ['']
  104. end
  105. end