issue_categories_controller.rb 3.66 KB
Newer Older
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
# 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.

jplang's avatar
jplang committed
18
class IssueCategoriesController < ApplicationController
19
  menu_item :settings
20
  model_object IssueCategory
21 22 23 24
  before_action :find_model_object, :except => [:index, :new, :create]
  before_action :find_project_from_association, :except => [:index, :new, :create]
  before_action :find_project_by_project_id, :only => [:index, :new, :create]
  before_action :authorize
25
  accept_api_auth :index, :show, :create, :update, :destroy
26

27 28
  def index
    respond_to do |format|
jplang's avatar
jplang committed
29
      format.html { redirect_to_settings_in_projects }
jplang's avatar
jplang committed
30
      format.api { @categories = @project.issue_categories.to_a }
31 32 33 34 35
    end
  end

  def show
    respond_to do |format|
jplang's avatar
jplang committed
36
      format.html { redirect_to_settings_in_projects }
37 38 39
      format.api
    end
  end
40

41
  def new
42 43
    @category = @project.issue_categories.build
    @category.safe_attributes = params[:issue_category]
44 45 46

    respond_to do |format|
      format.html
jplang's avatar
jplang committed
47
      format.js
48
    end
49 50 51
  end

  def create
52 53
    @category = @project.issue_categories.build
    @category.safe_attributes = params[:issue_category]
54 55 56 57
    if @category.save
      respond_to do |format|
        format.html do
          flash[:notice] = l(:notice_successful_create)
jplang's avatar
jplang committed
58
          redirect_to_settings_in_projects
59
        end
jplang's avatar
jplang committed
60
        format.js
61
        format.api { render :action => 'show', :status => :created, :location => issue_category_path(@category) }
62 63 64 65
      end
    else
      respond_to do |format|
        format.html { render :action => 'new'}
jplang's avatar
jplang committed
66
        format.js   { render :action => 'new'}
67
        format.api { render_validation_errors(@category) }
68 69 70
      end
    end
  end
71

jplang's avatar
jplang committed
72
  def edit
73 74 75
  end

  def update
76 77
    @category.safe_attributes = params[:issue_category]
    if @category.save
78 79 80
      respond_to do |format|
        format.html {
          flash[:notice] = l(:notice_successful_update)
jplang's avatar
jplang committed
81
          redirect_to_settings_in_projects
82
        }
83
        format.api { render_api_ok }
84
      end
85
    else
86 87 88 89
      respond_to do |format|
        format.html { render :action => 'edit' }
        format.api { render_validation_errors(@category) }
      end
jplang's avatar
jplang committed
90 91 92 93
    end
  end

  def destroy
94
    @issue_count = @category.issues.size
95
    if @issue_count == 0 || params[:todo] || api_request?
96 97 98 99
      reassign_to = nil
      if params[:reassign_to_id] && (params[:todo] == 'reassign' || params[:todo].blank?)
        reassign_to = @project.issue_categories.find_by_id(params[:reassign_to_id])
      end
100
      @category.destroy(reassign_to)
101
      respond_to do |format|
jplang's avatar
jplang committed
102
        format.html { redirect_to_settings_in_projects }
103
        format.api { render_api_ok }
104
      end
105
      return
106 107
    end
    @categories = @project.issue_categories - [@category]
108
  end
jplang's avatar
jplang committed
109

jplang's avatar
jplang committed
110 111 112 113 114 115
  private

  def redirect_to_settings_in_projects
    redirect_to settings_project_path(@project, :tab => 'categories')
  end

116 117 118 119 120
  # Wrap ApplicationController's find_model_object method to set
  # @category instead of just @issue_category
  def find_model_object
    super
    @category = @object
121
  end
jplang's avatar
jplang committed
122
end