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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 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. class EnumerationsController < ApplicationController
  18. layout 'admin'
  19. self.main_menu = false
  20. before_action :require_admin, :except => :index
  21. before_action :require_admin_or_api_request, :only => :index
  22. before_action :build_new_enumeration, :only => [:new, :create]
  23. before_action :find_enumeration, :only => [:edit, :update, :destroy]
  24. accept_api_auth :index
  25. helper :custom_fields
  26. def index
  27. respond_to do |format|
  28. format.html
  29. format.api {
  30. @klass = Enumeration.get_subclass(params[:type])
  31. if @klass
  32. @enumerations = @klass.shared.sorted.to_a
  33. else
  34. render_404
  35. end
  36. }
  37. end
  38. end
  39. def new
  40. end
  41. def create
  42. if request.post? && @enumeration.save
  43. flash[:notice] = l(:notice_successful_create)
  44. redirect_to enumerations_path
  45. else
  46. render :action => 'new'
  47. end
  48. end
  49. def edit
  50. end
  51. def update
  52. if @enumeration.update_attributes(enumeration_params)
  53. respond_to do |format|
  54. format.html {
  55. flash[:notice] = l(:notice_successful_update)
  56. redirect_to enumerations_path
  57. }
  58. format.js { head 200 }
  59. end
  60. else
  61. respond_to do |format|
  62. format.html { render :action => 'edit' }
  63. format.js { head 422 }
  64. end
  65. end
  66. end
  67. def destroy
  68. if !@enumeration.in_use?
  69. # No associated objects
  70. @enumeration.destroy
  71. redirect_to enumerations_path
  72. return
  73. elsif params[:reassign_to_id].present? && (reassign_to = @enumeration.class.find_by_id(params[:reassign_to_id].to_i))
  74. @enumeration.destroy(reassign_to)
  75. redirect_to enumerations_path
  76. return
  77. end
  78. @enumerations = @enumeration.class.system.to_a - [@enumeration]
  79. end
  80. private
  81. def build_new_enumeration
  82. class_name = params[:enumeration] && params[:enumeration][:type] || params[:type]
  83. @enumeration = Enumeration.new_subclass_instance(class_name)
  84. if @enumeration
  85. @enumeration.attributes = enumeration_params || {}
  86. else
  87. render_404
  88. end
  89. end
  90. def find_enumeration
  91. @enumeration = Enumeration.find(params[:id])
  92. rescue ActiveRecord::RecordNotFound
  93. render_404
  94. end
  95. def enumeration_params
  96. # can't require enumeration on #new action
  97. cf_ids = @enumeration.available_custom_fields.map{|c| c.id.to_s}
  98. params.permit(:enumeration => [:name, :active, :is_default, :position, :custom_field_values => cf_ids])[:enumeration]
  99. end
  100. end