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.

custom_field_enumerations_controller.rb 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 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 CustomFieldEnumerationsController < ApplicationController
  19. layout 'admin'
  20. self.main_menu = false
  21. before_action :require_admin
  22. before_action :find_custom_field
  23. before_action :find_enumeration, :only => :destroy
  24. helper :custom_fields
  25. def index
  26. @values = @custom_field.enumerations.order(:position)
  27. end
  28. def create
  29. @value = @custom_field.enumerations.build
  30. @value.attributes = enumeration_params
  31. @value.save
  32. respond_to do |format|
  33. format.html {redirect_to custom_field_enumerations_path(@custom_field)}
  34. format.js
  35. end
  36. end
  37. def update_each
  38. saved = CustomFieldEnumeration.update_each(@custom_field, update_each_params)
  39. if saved
  40. flash[:notice] = l(:notice_successful_update)
  41. end
  42. redirect_to :action => 'index'
  43. end
  44. def destroy
  45. reassign_to = @custom_field.enumerations.find_by_id(params[:reassign_to_id])
  46. if reassign_to.nil? && @value.in_use?
  47. @enumerations = @custom_field.enumerations - [@value]
  48. render :action => 'destroy'
  49. return
  50. end
  51. @value.destroy(reassign_to)
  52. redirect_to custom_field_enumerations_path(@custom_field)
  53. end
  54. private
  55. def find_custom_field
  56. @custom_field = CustomField.find(params[:custom_field_id])
  57. rescue ActiveRecord::RecordNotFound
  58. render_404
  59. end
  60. def find_enumeration
  61. @value = @custom_field.enumerations.find(params[:id])
  62. rescue ActiveRecord::RecordNotFound
  63. render_404
  64. end
  65. def enumeration_params
  66. params.require(:custom_field_enumeration).permit(:name, :active, :position)
  67. end
  68. def update_each_params
  69. # params.require(:custom_field_enumerations).permit(:name, :active, :position) does not work here with param like this:
  70. # "custom_field_enumerations":{"0":{"name": ...}, "1":{"name...}}
  71. params.permit(:custom_field_enumerations => [:name, :active, :position]).require(:custom_field_enumerations)
  72. end
  73. end