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_fields_controller.rb 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 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 CustomFieldsController < ApplicationController
  19. layout 'admin'
  20. self.main_menu = false
  21. before_action :require_admin
  22. before_action :build_new_custom_field, :only => [:new, :create]
  23. before_action :find_custom_field, :only => [:edit, :update, :destroy]
  24. accept_api_auth :index
  25. def index
  26. respond_to do |format|
  27. format.html {
  28. @custom_fields_by_type = CustomField.all.group_by {|f| f.class.name }
  29. @custom_fields_projects_count =
  30. IssueCustomField.where(is_for_all: false).joins(:projects).group(:custom_field_id).count
  31. }
  32. format.api {
  33. @custom_fields = CustomField.all
  34. }
  35. end
  36. end
  37. def new
  38. @custom_field.field_format = 'string' if @custom_field.field_format.blank?
  39. @custom_field.default_value = nil
  40. end
  41. def create
  42. if @custom_field.save
  43. flash[:notice] = l(:notice_successful_create)
  44. call_hook(:controller_custom_fields_new_after_save, :params => params, :custom_field => @custom_field)
  45. if params[:continue]
  46. redirect_to new_custom_field_path({:type => @custom_field.type})
  47. else
  48. redirect_to edit_custom_field_path(@custom_field)
  49. end
  50. else
  51. render :action => 'new'
  52. end
  53. end
  54. def edit
  55. end
  56. def update
  57. @custom_field.safe_attributes = params[:custom_field]
  58. if @custom_field.save
  59. call_hook(:controller_custom_fields_edit_after_save, :params => params, :custom_field => @custom_field)
  60. respond_to do |format|
  61. format.html {
  62. flash[:notice] = l(:notice_successful_update)
  63. redirect_back_or_default edit_custom_field_path(@custom_field)
  64. }
  65. format.js { head 200 }
  66. end
  67. else
  68. respond_to do |format|
  69. format.html { render :action => 'edit' }
  70. format.js { head 422 }
  71. end
  72. end
  73. end
  74. def destroy
  75. begin
  76. if @custom_field.destroy
  77. flash[:notice] = l(:notice_successful_delete)
  78. end
  79. rescue
  80. flash[:error] = l(:error_can_not_delete_custom_field)
  81. end
  82. redirect_to custom_fields_path(:tab => @custom_field.class.name)
  83. end
  84. private
  85. def build_new_custom_field
  86. @custom_field = CustomField.new_subclass_instance(params[:type])
  87. if @custom_field.nil?
  88. render :action => 'select_type'
  89. else
  90. @custom_field.safe_attributes = params[:custom_field]
  91. end
  92. end
  93. def find_custom_field
  94. @custom_field = CustomField.find(params[:id])
  95. rescue ActiveRecord::RecordNotFound
  96. render_404
  97. end
  98. end