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.

versions_controller.rb 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 VersionsController < ApplicationController
  19. menu_item :roadmap
  20. model_object Version
  21. before_action :find_model_object, :except => [:index, :new, :create, :close_completed]
  22. before_action :find_project_from_association, :except => [:index, :new, :create, :close_completed]
  23. before_action :find_project_by_project_id, :only => [:index, :new, :create, :close_completed]
  24. before_action :authorize
  25. accept_api_auth :index, :show, :create, :update, :destroy
  26. helper :custom_fields
  27. helper :projects
  28. def index
  29. respond_to do |format|
  30. format.html do
  31. @trackers = @project.trackers.sorted.to_a
  32. retrieve_selected_tracker_ids(@trackers, @trackers.select {|t| t.is_in_roadmap?})
  33. @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1')
  34. project_ids = @with_subprojects ? @project.self_and_descendants.pluck(:id) : [@project.id]
  35. @versions = @project.shared_versions.preload(:custom_values)
  36. @versions += @project.rolled_up_versions.visible.preload(:custom_values) if @with_subprojects
  37. @versions = @versions.to_a.uniq.sort
  38. unless params[:completed]
  39. @completed_versions = @versions.select(&:completed?).reverse
  40. @versions -= @completed_versions
  41. end
  42. @issues_by_version = {}
  43. if @selected_tracker_ids.any? && @versions.any?
  44. issues = Issue.visible.
  45. includes(:project, :tracker).
  46. preload(:status, :priority, :fixed_version).
  47. where(:tracker_id => @selected_tracker_ids, :project_id => project_ids, :fixed_version_id => @versions.map(&:id)).
  48. order("#{Project.table_name}.lft, #{Tracker.table_name}.position, #{Issue.table_name}.id")
  49. @issues_by_version = issues.group_by(&:fixed_version)
  50. end
  51. @versions.reject! {|version| !project_ids.include?(version.project_id) && @issues_by_version[version].blank?}
  52. end
  53. format.api do
  54. @versions = @project.shared_versions.to_a
  55. end
  56. end
  57. end
  58. def show
  59. respond_to do |format|
  60. format.html do
  61. @issues = @version.fixed_issues.visible.
  62. includes(:status, :tracker, :priority).
  63. preload(:project).
  64. reorder("#{Tracker.table_name}.position, #{Issue.table_name}.id").
  65. to_a
  66. end
  67. format.api
  68. end
  69. end
  70. def new
  71. @version = @project.versions.build
  72. @version.safe_attributes = params[:version]
  73. respond_to do |format|
  74. format.html
  75. format.js
  76. end
  77. end
  78. def create
  79. @version = @project.versions.build
  80. if params[:version]
  81. attributes = params[:version].dup
  82. attributes.delete('sharing') unless attributes.nil? || @version.allowed_sharings.include?(attributes['sharing'])
  83. @version.safe_attributes = attributes
  84. end
  85. if request.post?
  86. if @version.save
  87. respond_to do |format|
  88. format.html do
  89. flash[:notice] = l(:notice_successful_create)
  90. redirect_back_or_default settings_project_path(@project, :tab => 'versions')
  91. end
  92. format.js
  93. format.api do
  94. render :action => 'show', :status => :created, :location => version_url(@version)
  95. end
  96. end
  97. else
  98. respond_to do |format|
  99. format.html {render :action => 'new'}
  100. format.js {render :action => 'new'}
  101. format.api {render_validation_errors(@version)}
  102. end
  103. end
  104. end
  105. end
  106. def edit
  107. end
  108. def update
  109. if params[:version]
  110. attributes = params[:version].dup
  111. attributes.delete('sharing') unless @version.allowed_sharings.include?(attributes['sharing'])
  112. @version.safe_attributes = attributes
  113. if @version.save
  114. respond_to do |format|
  115. format.html do
  116. flash[:notice] = l(:notice_successful_update)
  117. redirect_back_or_default settings_project_path(@project, :tab => 'versions')
  118. end
  119. format.api {render_api_ok}
  120. end
  121. else
  122. respond_to do |format|
  123. format.html {render :action => 'edit'}
  124. format.api {render_validation_errors(@version)}
  125. end
  126. end
  127. end
  128. end
  129. def close_completed
  130. if request.put?
  131. @project.close_completed_versions
  132. end
  133. redirect_to settings_project_path(@project, :tab => 'versions')
  134. end
  135. def destroy
  136. if @version.deletable?
  137. @version.destroy
  138. respond_to do |format|
  139. format.html {redirect_back_or_default settings_project_path(@project, :tab => 'versions')}
  140. format.api {render_api_ok}
  141. end
  142. else
  143. respond_to do |format|
  144. format.html do
  145. flash[:error] = l(:notice_unable_delete_version)
  146. redirect_to settings_project_path(@project, :tab => 'versions')
  147. end
  148. format.api {head :unprocessable_entity}
  149. end
  150. end
  151. end
  152. def status_by
  153. respond_to do |format|
  154. format.html {render :action => 'show'}
  155. format.js
  156. end
  157. end
  158. private
  159. def retrieve_selected_tracker_ids(selectable_trackers, default_trackers=nil)
  160. if ids = params[:tracker_ids]
  161. @selected_tracker_ids =
  162. if ids.is_a? Array
  163. ids.collect {|id| id.to_i.to_s}
  164. else
  165. ids.split('/').collect {|id| id.to_i.to_s}
  166. end
  167. else
  168. @selected_tracker_ids =
  169. (default_trackers || selectable_trackers).collect {|t| t.id.to_s}
  170. end
  171. end
  172. end