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

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