1 # Redmine - project management software
2 # Copyright (C) 2006-2011 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
18 class EnumerationsController < ApplicationController
19 layout 'admin'
20
21 before_filter :require_admin
22 before_filter :build_new_enumeration, :only => [:new, :create]
23 before_filter :find_enumeration, :only => [:edit, :update, :destroy]
24
25 helper :custom_fields
26
27 def index
28 end
29
30 def new
31 end
32
33 def create
34 if request.post? && @enumeration.save
35 flash[:notice] = l(:notice_successful_create)
36 redirect_to :action => 'index', :type => @enumeration.type
37 else
38 render :action => 'new'
39 end
40 end
41
42 def edit
43 end
44
45 def update
46 if request.put? && @enumeration.update_attributes(params[:enumeration])
47 flash[:notice] = l(:notice_successful_update)
48 redirect_to :action => 'index', :type => @enumeration.type
49 else
50 render :action => 'edit'
51 end
52 end
53
54 def destroy
55 if !@enumeration.in_use?
56 # No associated objects
57 @enumeration.destroy
58 redirect_to :action => 'index'
59 return
60 elsif params[:reassign_to_id]
61 if reassign_to = @enumeration.class.find_by_id(params[:reassign_to_id])
62 @enumeration.destroy(reassign_to)
63 redirect_to :action => 'index'
64 return
65 end
66 end
67 @enumerations = @enumeration.class.all - [@enumeration]
68 end
69
70 private
71
72 def build_new_enumeration
73 class_name = params[:enumeration] && params[:enumeration][:type] || params[:type]
74 @enumeration = Enumeration.new_subclass_instance(class_name, params[:enumeration])
75 if @enumeration.nil?
76 render_404
77 end
78 end
79
80 def find_enumeration
81 @enumeration = Enumeration.find(params[:id])
82 rescue ActiveRecord::RecordNotFound
83 render_404
84 end
85 end