versions_controller.rb 6.06 KB
Newer Older
jplang's avatar
jplang committed
1
# Redmine - project management software
jplang's avatar
jplang committed
2
# Copyright (C) 2006-2016  Jean-Philippe Lang
3 4 5 6 7
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
8
#
9 10 11 12
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
13
#
14 15 16 17 18
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

class VersionsController < ApplicationController
19
  menu_item :roadmap
20
  model_object Version
21 22 23 24
  before_action :find_model_object, :except => [:index, :new, :create, :close_completed]
  before_action :find_project_from_association, :except => [:index, :new, :create, :close_completed]
  before_action :find_project_by_project_id, :only => [:index, :new, :create, :close_completed]
  before_action :authorize
jplang's avatar
jplang committed
25

26
  accept_api_auth :index, :show, :create, :update, :destroy
27

28
  helper :custom_fields
29
  helper :projects
30 31

  def index
jplang's avatar
jplang committed
32 33
    respond_to do |format|
      format.html {
jplang's avatar
jplang committed
34
        @trackers = @project.trackers.sorted.to_a
jplang's avatar
jplang committed
35 36 37
        retrieve_selected_tracker_ids(@trackers, @trackers.select {|t| t.is_in_roadmap?})
        @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1')
        project_ids = @with_subprojects ? @project.self_and_descendants.collect(&:id) : [@project.id]
38

39 40
        @versions = @project.shared_versions.preload(:custom_values)
        @versions += @project.rolled_up_versions.visible.preload(:custom_values) if @with_subprojects
jplang's avatar
jplang committed
41
        @versions = @versions.to_a.uniq.sort
42
        unless params[:completed]
43
          @completed_versions = @versions.select(&:completed?)
44 45
          @versions -= @completed_versions
        end
46

jplang's avatar
jplang committed
47
        @issues_by_version = {}
48
        if @selected_tracker_ids.any? && @versions.any?
49 50 51 52 53
          issues = Issue.visible.
            includes(:project, :tracker).
            preload(:status, :priority, :fixed_version).
            where(:tracker_id => @selected_tracker_ids, :project_id => project_ids, :fixed_version_id => @versions.map(&:id)).
            order("#{Project.table_name}.lft, #{Tracker.table_name}.position, #{Issue.table_name}.id")
54
          @issues_by_version = issues.group_by(&:fixed_version)
jplang's avatar
jplang committed
55 56 57 58
        end
        @versions.reject! {|version| !project_ids.include?(version.project_id) && @issues_by_version[version].blank?}
      }
      format.api {
jplang's avatar
jplang committed
59
        @versions = @project.shared_versions.to_a
jplang's avatar
jplang committed
60
      }
61 62
    end
  end
63

64
  def show
jplang's avatar
jplang committed
65 66
    respond_to do |format|
      format.html {
jplang's avatar
jplang committed
67 68
        @issues = @version.fixed_issues.visible.
          includes(:status, :tracker, :priority).
jplang's avatar
jplang committed
69
          preload(:project).
jplang's avatar
jplang committed
70
          reorder("#{Tracker.table_name}.position, #{Issue.table_name}.id").
jplang's avatar
jplang committed
71
          to_a
jplang's avatar
jplang committed
72 73 74
      }
      format.api
    end
75
  end
76

77
  def new
78 79
    @version = @project.versions.build
    @version.safe_attributes = params[:version]
80 81 82

    respond_to do |format|
      format.html
jplang's avatar
jplang committed
83
      format.js
84
    end
85 86 87 88 89 90 91
  end

  def create
    @version = @project.versions.build
    if params[:version]
      attributes = params[:version].dup
      attributes.delete('sharing') unless attributes.nil? || @version.allowed_sharings.include?(attributes['sharing'])
92
      @version.safe_attributes = attributes
93 94
    end

95 96 97 98 99
    if request.post?
      if @version.save
        respond_to do |format|
          format.html do
            flash[:notice] = l(:notice_successful_create)
jplang's avatar
jplang committed
100
            redirect_back_or_default settings_project_path(@project, :tab => 'versions')
101
          end
jplang's avatar
jplang committed
102
          format.js
jplang's avatar
jplang committed
103
          format.api do
104
            render :action => 'show', :status => :created, :location => version_url(@version)
jplang's avatar
jplang committed
105
          end
106 107 108
        end
      else
        respond_to do |format|
109
          format.html { render :action => 'new' }
jplang's avatar
jplang committed
110
          format.js   { render :action => 'new' }
jplang's avatar
jplang committed
111
          format.api  { render_validation_errors(@version) }
112 113 114 115
        end
      end
    end
  end
116

jplang's avatar
jplang committed
117
  def edit
118
  end
119

120
  def update
jplang's avatar
jplang committed
121
    if params[:version]
122 123
      attributes = params[:version].dup
      attributes.delete('sharing') unless @version.allowed_sharings.include?(attributes['sharing'])
124 125
      @version.safe_attributes = attributes
      if @version.save
jplang's avatar
jplang committed
126 127 128
        respond_to do |format|
          format.html {
            flash[:notice] = l(:notice_successful_update)
jplang's avatar
jplang committed
129
            redirect_back_or_default settings_project_path(@project, :tab => 'versions')
jplang's avatar
jplang committed
130
          }
131
          format.api  { render_api_ok }
jplang's avatar
jplang committed
132
        end
133 134 135
      else
        respond_to do |format|
          format.html { render :action => 'edit' }
jplang's avatar
jplang committed
136
          format.api  { render_validation_errors(@version) }
137
        end
138
      end
jplang's avatar
jplang committed
139 140
    end
  end
141

142
  def close_completed
143
    if request.put?
144 145
      @project.close_completed_versions
    end
jplang's avatar
jplang committed
146
    redirect_to settings_project_path(@project, :tab => 'versions')
147
  end
jplang's avatar
jplang committed
148

149
  def destroy
150
    if @version.deletable?
151
      @version.destroy
jplang's avatar
jplang committed
152
      respond_to do |format|
jplang's avatar
jplang committed
153
        format.html { redirect_back_or_default settings_project_path(@project, :tab => 'versions') }
154
        format.api  { render_api_ok }
jplang's avatar
jplang committed
155
      end
156
    else
jplang's avatar
jplang committed
157 158 159
      respond_to do |format|
        format.html {
          flash[:error] = l(:notice_unable_delete_version)
jplang's avatar
jplang committed
160
          redirect_to settings_project_path(@project, :tab => 'versions')
jplang's avatar
jplang committed
161 162 163
        }
        format.api  { head :unprocessable_entity }
      end
164
    end
165
  end
166

167 168 169
  def status_by
    respond_to do |format|
      format.html { render :action => 'show' }
jplang's avatar
jplang committed
170
      format.js
171 172
    end
  end
173

jplang's avatar
jplang committed
174
  private
175 176 177 178 179 180 181 182

  def retrieve_selected_tracker_ids(selectable_trackers, default_trackers=nil)
    if ids = params[:tracker_ids]
      @selected_tracker_ids = (ids.is_a? Array) ? ids.collect { |id| id.to_i.to_s } : ids.split('/').collect { |id| id.to_i.to_s }
    else
      @selected_tracker_ids = (default_trackers || selectable_trackers).collect {|t| t.id.to_s }
    end
  end
jplang's avatar
jplang committed
183
end