2019-03-16 09:37:35 +00:00
|
|
|
# frozen_string_literal: true
|
2019-03-15 01:32:57 +00:00
|
|
|
|
2011-08-31 12:20:21 +00:00
|
|
|
# Redmine - project management software
|
2024-02-26 22:55:54 +00:00
|
|
|
# Copyright (C) 2006- Jean-Philippe Lang
|
2007-03-12 17:59:02 +00:00
|
|
|
#
|
|
|
|
|
# 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.
|
2011-08-31 12:20:21 +00:00
|
|
|
#
|
2007-03-12 17:59:02 +00:00
|
|
|
# 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.
|
2011-08-31 12:20:21 +00:00
|
|
|
#
|
2007-03-12 17:59:02 +00:00
|
|
|
# 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 EnumerationsController < ApplicationController
|
2009-12-17 18:21:02 +00:00
|
|
|
layout 'admin'
|
2016-11-19 10:30:02 +00:00
|
|
|
self.main_menu = false
|
2011-08-31 12:20:21 +00:00
|
|
|
|
2016-07-14 07:27:31 +00:00
|
|
|
before_action :require_admin, :except => :index
|
|
|
|
|
before_action :require_admin_or_api_request, :only => :index
|
|
|
|
|
before_action :build_new_enumeration, :only => [:new, :create]
|
|
|
|
|
before_action :find_enumeration, :only => [:edit, :update, :destroy]
|
2012-10-17 17:29:27 +00:00
|
|
|
accept_api_auth :index
|
2009-10-21 22:34:22 +00:00
|
|
|
|
|
|
|
|
helper :custom_fields
|
2011-08-31 12:20:21 +00:00
|
|
|
|
2006-06-28 18:11:03 +00:00
|
|
|
def index
|
2012-10-17 17:29:27 +00:00
|
|
|
respond_to do |format|
|
|
|
|
|
format.html
|
2020-11-09 13:25:00 +00:00
|
|
|
format.api do
|
2012-10-17 17:29:27 +00:00
|
|
|
@klass = Enumeration.get_subclass(params[:type])
|
|
|
|
|
if @klass
|
2014-10-22 17:37:16 +00:00
|
|
|
@enumerations = @klass.shared.sorted.to_a
|
2012-10-17 17:29:27 +00:00
|
|
|
else
|
|
|
|
|
render_404
|
|
|
|
|
end
|
2020-11-09 13:25:00 +00:00
|
|
|
end
|
2012-10-17 17:29:27 +00:00
|
|
|
end
|
2006-06-28 18:11:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def new
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create
|
2011-12-11 10:26:12 +00:00
|
|
|
if request.post? && @enumeration.save
|
2006-07-29 19:54:22 +00:00
|
|
|
flash[:notice] = l(:notice_successful_create)
|
2012-12-11 17:51:30 +00:00
|
|
|
redirect_to enumerations_path
|
2006-06-28 18:11:03 +00:00
|
|
|
else
|
|
|
|
|
render :action => 'new'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def edit
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update
|
2019-08-17 08:14:36 +00:00
|
|
|
if @enumeration.update(enumeration_params)
|
2016-04-17 07:40:39 +00:00
|
|
|
respond_to do |format|
|
2020-11-09 13:25:00 +00:00
|
|
|
format.html do
|
2016-04-17 07:40:39 +00:00
|
|
|
flash[:notice] = l(:notice_successful_update)
|
|
|
|
|
redirect_to enumerations_path
|
2020-11-09 13:25:00 +00:00
|
|
|
end
|
2024-05-18 05:56:55 +00:00
|
|
|
format.js {head :ok}
|
2016-04-17 07:40:39 +00:00
|
|
|
end
|
2006-06-28 18:11:03 +00:00
|
|
|
else
|
2016-04-17 07:40:39 +00:00
|
|
|
respond_to do |format|
|
2020-11-19 13:30:54 +00:00
|
|
|
format.html {render :action => 'edit'}
|
2024-06-12 16:09:37 +00:00
|
|
|
format.js {head :unprocessable_content}
|
2016-04-17 07:40:39 +00:00
|
|
|
end
|
2006-06-28 18:11:03 +00:00
|
|
|
end
|
|
|
|
|
end
|
2011-08-31 12:20:21 +00:00
|
|
|
|
2006-06-28 18:11:03 +00:00
|
|
|
def destroy
|
2008-06-17 19:10:54 +00:00
|
|
|
if !@enumeration.in_use?
|
|
|
|
|
# No associated objects
|
|
|
|
|
@enumeration.destroy
|
2012-12-11 17:51:30 +00:00
|
|
|
redirect_to enumerations_path
|
2011-05-01 23:15:03 +00:00
|
|
|
return
|
2013-05-17 18:18:06 +00:00
|
|
|
elsif params[:reassign_to_id].present? && (reassign_to = @enumeration.class.find_by_id(params[:reassign_to_id].to_i))
|
|
|
|
|
@enumeration.destroy(reassign_to)
|
|
|
|
|
redirect_to enumerations_path
|
|
|
|
|
return
|
2008-06-17 19:10:54 +00:00
|
|
|
end
|
2014-10-22 17:37:16 +00:00
|
|
|
@enumerations = @enumeration.class.system.to_a - [@enumeration]
|
2011-12-11 10:26:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def build_new_enumeration
|
|
|
|
|
class_name = params[:enumeration] && params[:enumeration][:type] || params[:type]
|
2018-09-15 06:45:10 +00:00
|
|
|
@enumeration = Enumeration.new_subclass_instance(class_name)
|
|
|
|
|
if @enumeration
|
|
|
|
|
@enumeration.attributes = enumeration_params || {}
|
|
|
|
|
else
|
2011-12-11 10:26:12 +00:00
|
|
|
render_404
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def find_enumeration
|
|
|
|
|
@enumeration = Enumeration.find(params[:id])
|
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
|
render_404
|
2006-06-28 18:11:03 +00:00
|
|
|
end
|
2017-06-03 08:36:39 +00:00
|
|
|
|
|
|
|
|
def enumeration_params
|
|
|
|
|
# can't require enumeration on #new action
|
2020-03-20 02:25:13 +00:00
|
|
|
cf_ids = @enumeration.available_custom_fields.map {|c| c.multiple? ? {c.id.to_s => []} : c.id.to_s}
|
2018-09-15 06:45:10 +00:00
|
|
|
params.permit(:enumeration => [:name, :active, :is_default, :position, :custom_field_values => cf_ids])[:enumeration]
|
2017-06-03 08:36:39 +00:00
|
|
|
end
|
2006-06-28 18:11:03 +00:00
|
|
|
end
|