Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

versions_controller.rb 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2016 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class VersionsController < ApplicationController
  18. menu_item :roadmap
  19. model_object Version
  20. before_action :find_model_object, :except => [:index, :new, :create, :close_completed]
  21. before_action :find_project_from_association, :except => [:index, :new, :create, :close_completed]
  22. before_action :find_project_by_project_id, :only => [:index, :new, :create, :close_completed]
  23. before_action :authorize
  24. accept_api_auth :index, :show, :create, :update, :destroy
  25. helper :custom_fields
  26. helper :projects
  27. def index
  28. respond_to do |format|
  29. format.html {
  30. @trackers = @project.trackers.sorted.to_a
  31. retrieve_selected_tracker_ids(@trackers, @trackers.select {|t| t.is_in_roadmap?})
  32. @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1')
  33. project_ids = @with_subprojects ? @project.self_and_descendants.collect(&:id) : [@project.id]
  34. @versions = @project.shared_versions.preload(:custom_values)
  35. @versions += @project.rolled_up_versions.visible.preload(:custom_values) if @with_subprojects
  36. @versions = @versions.to_a.uniq.sort
  37. unless params[:completed]
  38. @completed_versions = @versions.select(&:completed?).reverse
  39. @versions -= @completed_versions
  40. end
  41. @issues_by_version = {}
  42. if @selected_tracker_ids.any? && @versions.any?
  43. issues = Issue.visible.
  44. includes(:project, :tracker).
  45. preload(:status, :priority, :fixed_version).
  46. where(:tracker_id => @selected_tracker_ids, :project_id => project_ids, :fixed_version_id => @versions.map(&:id)).
  47. order("#{Project.table_name}.lft, #{Tracker.table_name}.position, #{Issue.table_name}.id")
  48. @issues_by_version = issues.group_by(&:fixed_version)
  49. end
  50. @versions.reject! {|version| !project_ids.include?(version.project_id) && @issues_by_version[version].blank?}
  51. }
  52. format.api {
  53. @versions = @project.shared_versions.to_a
  54. }
  55. end
  56. end
  57. def show
  58. respond_to do |format|
  59. format.html {
  60. @issues = @version.fixed_issues.visible.
  61. includes(:status, :tracker, :priority).
  62. preload(:project).
  63. reorder("#{Tracker.table_name}.position, #{Issue.table_name}.id").
  64. to_a
  65. }
  66. format.api
  67. end
  68. end
  69. def new
  70. @version = @project.versions.build
  71. @version.safe_attributes = params[:version]
  72. respond_to do |format|
  73. format.html
  74. format.js
  75. end
  76. end
  77. def create
  78. @version = @project.versions.build
  79. if params[:version]
  80. attributes = params[:version].dup
  81. attributes.delete('sharing') unless attributes.nil? || @version.allowed_sharings.include?(attributes['sharing'])
  82. @version.safe_attributes = attributes
  83. end
  84. if request.post?
  85. if @version.save
  86. respond_to do |format|
  87. format.html do
  88. flash[:notice] = l(:notice_successful_create)
  89. redirect_back_or_default settings_project_path(@project, :tab => 'versions')
  90. end
  91. format.js
  92. format.api do
  93. render :action => 'show', :status => :created, :location => version_url(@version)
  94. end
  95. end
  96. else
  97. respond_to do |format|
  98. format.html { render :action => 'new' }
  99. format.js { render :action => 'new' }
  100. format.api { render_validation_errors(@version) }
  101. end
  102. end
  103. end
  104. end
  105. def edit
  106. end
  107. def update
  108. if params[:version]
  109. attributes = params[:version].dup
  110. attributes.delete('sharing') unless @version.allowed_sharings.include?(attributes['sharing'])
  111. @version.safe_attributes = attributes
  112. if @version.save
  113. respond_to do |format|
  114. format.html {
  115. flash[:notice] = l(:notice_successful_update)
  116. redirect_back_or_default settings_project_path(@project, :tab => 'versions')
  117. }
  118. format.api { render_api_ok }
  119. end
  120. else
  121. respond_to do |format|
  122. format.html { render :action => 'edit' }
  123. format.api { render_validation_errors(@version) }
  124. end
  125. end
  126. end
  127. end
  128. def close_completed
  129. if request.put?
  130. @project.close_completed_versions
  131. end
  132. redirect_to settings_project_path(@project, :tab => 'versions')
  133. end
  134. def destroy
  135. if @version.deletable?
  136. @version.destroy
  137. respond_to do |format|
  138. format.html { redirect_back_or_default settings_project_path(@project, :tab => 'versions') }
  139. format.api { render_api_ok }
  140. end
  141. else
  142. respond_to do |format|
  143. format.html {
  144. flash[:error] = l(:notice_unable_delete_version)
  145. redirect_to settings_project_path(@project, :tab => 'versions')
  146. }
  147. format.api { head :unprocessable_entity }
  148. end
  149. end
  150. end
  151. def status_by
  152. respond_to do |format|
  153. format.html { render :action => 'show' }
  154. format.js
  155. end
  156. end
  157. private
  158. def retrieve_selected_tracker_ids(selectable_trackers, default_trackers=nil)
  159. if ids = params[:tracker_ids]
  160. @selected_tracker_ids = (ids.is_a? Array) ? ids.collect { |id| id.to_i.to_s } : ids.split('/').collect { |id| id.to_i.to_s }
  161. else
  162. @selected_tracker_ids = (default_trackers || selectable_trackers).collect {|t| t.id.to_s }
  163. end
  164. end
  165. end