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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2012 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_filter :find_model_object, :except => [:index, :new, :create, :close_completed]
  21. before_filter :find_project_from_association, :except => [:index, :new, :create, :close_completed]
  22. before_filter :find_project, :only => [:index, :new, :create, :close_completed]
  23. before_filter :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.find(:all, :order => 'position')
  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 || []
  35. @versions += @project.rolled_up_versions.visible if @with_subprojects
  36. @versions = @versions.uniq.sort
  37. unless params[:completed]
  38. @completed_versions = @versions.select {|version| version.closed? || version.completed? }
  39. @versions -= @completed_versions
  40. end
  41. @issues_by_version = {}
  42. if @selected_tracker_ids.any? && @versions.any?
  43. issues = Issue.visible.all(
  44. :include => [:project, :status, :tracker, :priority, :fixed_version],
  45. :conditions => {:tracker_id => @selected_tracker_ids, :project_id => project_ids, :fixed_version_id => @versions.map(&:id)},
  46. :order => "#{Project.table_name}.lft, #{Tracker.table_name}.position, #{Issue.table_name}.id"
  47. )
  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.all
  54. }
  55. end
  56. end
  57. def show
  58. respond_to do |format|
  59. format.html {
  60. @issues = @version.fixed_issues.visible.find(:all,
  61. :include => [:status, :tracker, :priority],
  62. :order => "#{Tracker.table_name}.position, #{Issue.table_name}.id")
  63. }
  64. format.api
  65. end
  66. end
  67. def new
  68. @version = @project.versions.build
  69. @version.safe_attributes = params[:version]
  70. respond_to do |format|
  71. format.html
  72. format.js do
  73. render :update do |page|
  74. page.replace_html 'ajax-modal', :partial => 'versions/new_modal'
  75. page << "showModal('ajax-modal', '600px');"
  76. page << "Form.Element.focus('version_name');"
  77. end
  78. end
  79. end
  80. end
  81. def create
  82. @version = @project.versions.build
  83. if params[:version]
  84. attributes = params[:version].dup
  85. attributes.delete('sharing') unless attributes.nil? || @version.allowed_sharings.include?(attributes['sharing'])
  86. @version.safe_attributes = attributes
  87. end
  88. if request.post?
  89. if @version.save
  90. respond_to do |format|
  91. format.html do
  92. flash[:notice] = l(:notice_successful_create)
  93. redirect_back_or_default :controller => 'projects', :action => 'settings', :tab => 'versions', :id => @project
  94. end
  95. format.js do
  96. render(:update) {|page|
  97. page << 'hideModal();'
  98. # IE doesn't support the replace_html rjs method for select box options
  99. page.replace "issue_fixed_version_id",
  100. content_tag('select', content_tag('option') + version_options_for_select(@project.shared_versions.open, @version), :id => 'issue_fixed_version_id', :name => 'issue[fixed_version_id]')
  101. }
  102. end
  103. format.api do
  104. render :action => 'show', :status => :created, :location => version_url(@version)
  105. end
  106. end
  107. else
  108. respond_to do |format|
  109. format.html { render :action => 'new' }
  110. format.js do
  111. render :update do |page|
  112. page.replace_html 'ajax-modal', :partial => 'versions/new_modal'
  113. page << "Form.Element.focus('version_name');"
  114. end
  115. end
  116. format.api { render_validation_errors(@version) }
  117. end
  118. end
  119. end
  120. end
  121. def edit
  122. end
  123. def update
  124. if request.put? && params[:version]
  125. attributes = params[:version].dup
  126. attributes.delete('sharing') unless @version.allowed_sharings.include?(attributes['sharing'])
  127. @version.safe_attributes = attributes
  128. if @version.save
  129. respond_to do |format|
  130. format.html {
  131. flash[:notice] = l(:notice_successful_update)
  132. redirect_back_or_default :controller => 'projects', :action => 'settings', :tab => 'versions', :id => @project
  133. }
  134. format.api { head :ok }
  135. end
  136. else
  137. respond_to do |format|
  138. format.html { render :action => 'edit' }
  139. format.api { render_validation_errors(@version) }
  140. end
  141. end
  142. end
  143. end
  144. def close_completed
  145. if request.put?
  146. @project.close_completed_versions
  147. end
  148. redirect_to :controller => 'projects', :action => 'settings', :tab => 'versions', :id => @project
  149. end
  150. def destroy
  151. if @version.fixed_issues.empty?
  152. @version.destroy
  153. respond_to do |format|
  154. format.html { redirect_back_or_default :controller => 'projects', :action => 'settings', :tab => 'versions', :id => @project }
  155. format.api { head :ok }
  156. end
  157. else
  158. respond_to do |format|
  159. format.html {
  160. flash[:error] = l(:notice_unable_delete_version)
  161. redirect_to :controller => 'projects', :action => 'settings', :tab => 'versions', :id => @project
  162. }
  163. format.api { head :unprocessable_entity }
  164. end
  165. end
  166. end
  167. def status_by
  168. respond_to do |format|
  169. format.html { render :action => 'show' }
  170. format.js { render(:update) {|page| page.replace_html 'status_by', render_issue_status_by(@version, params[:status_by])} }
  171. end
  172. end
  173. private
  174. def find_project
  175. @project = Project.find(params[:project_id])
  176. rescue ActiveRecord::RecordNotFound
  177. render_404
  178. end
  179. def retrieve_selected_tracker_ids(selectable_trackers, default_trackers=nil)
  180. if ids = params[:tracker_ids]
  181. @selected_tracker_ids = (ids.is_a? Array) ? ids.collect { |id| id.to_i.to_s } : ids.split('/').collect { |id| id.to_i.to_s }
  182. else
  183. @selected_tracker_ids = (default_trackers || selectable_trackers).collect {|t| t.id.to_s }
  184. end
  185. end
  186. end