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.

enumerations_controller.rb 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class EnumerationsController < ApplicationController
  19. layout 'admin'
  20. self.main_menu = false
  21. before_action :require_admin, :except => :index
  22. before_action :require_admin_or_api_request, :only => :index
  23. before_action :build_new_enumeration, :only => [:new, :create]
  24. before_action :find_enumeration, :only => [:edit, :update, :destroy]
  25. accept_api_auth :index
  26. helper :custom_fields
  27. def index
  28. respond_to do |format|
  29. format.html
  30. format.api do
  31. @klass = Enumeration.get_subclass(params[:type])
  32. if @klass
  33. @enumerations = @klass.shared.sorted.to_a
  34. else
  35. render_404
  36. end
  37. end
  38. end
  39. end
  40. def new
  41. end
  42. def create
  43. if request.post? && @enumeration.save
  44. flash[:notice] = l(:notice_successful_create)
  45. redirect_to enumerations_path
  46. else
  47. render :action => 'new'
  48. end
  49. end
  50. def edit
  51. end
  52. def update
  53. if @enumeration.update(enumeration_params)
  54. respond_to do |format|
  55. format.html do
  56. flash[:notice] = l(:notice_successful_update)
  57. redirect_to enumerations_path
  58. end
  59. format.js {head 200}
  60. end
  61. else
  62. respond_to do |format|
  63. format.html {render :action => 'edit'}
  64. format.js {head 422}
  65. end
  66. end
  67. end
  68. def destroy
  69. if !@enumeration.in_use?
  70. # No associated objects
  71. @enumeration.destroy
  72. redirect_to enumerations_path
  73. return
  74. elsif params[:reassign_to_id].present? && (reassign_to = @enumeration.class.find_by_id(params[:reassign_to_id].to_i))
  75. @enumeration.destroy(reassign_to)
  76. redirect_to enumerations_path
  77. return
  78. end
  79. @enumerations = @enumeration.class.system.to_a - [@enumeration]
  80. end
  81. private
  82. def build_new_enumeration
  83. class_name = params[:enumeration] && params[:enumeration][:type] || params[:type]
  84. @enumeration = Enumeration.new_subclass_instance(class_name)
  85. if @enumeration
  86. @enumeration.attributes = enumeration_params || {}
  87. else
  88. render_404
  89. end
  90. end
  91. def find_enumeration
  92. @enumeration = Enumeration.find(params[:id])
  93. rescue ActiveRecord::RecordNotFound
  94. render_404
  95. end
  96. def enumeration_params
  97. # can't require enumeration on #new action
  98. cf_ids = @enumeration.available_custom_fields.map {|c| c.multiple? ? {c.id.to_s => []} : c.id.to_s}
  99. params.permit(:enumeration => [:name, :active, :is_default, :position, :custom_field_values => cf_ids])[:enumeration]
  100. end
  101. end