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.6KB

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