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 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2013 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. before_filter :require_admin, :except => :index
  20. before_filter :require_admin_or_api_request, :only => :index
  21. before_filter :build_new_enumeration, :only => [:new, :create]
  22. before_filter :find_enumeration, :only => [:edit, :update, :destroy]
  23. accept_api_auth :index
  24. helper :custom_fields
  25. def index
  26. respond_to do |format|
  27. format.html
  28. format.api {
  29. @klass = Enumeration.get_subclass(params[:type])
  30. if @klass
  31. @enumerations = @klass.shared.sorted.all
  32. else
  33. render_404
  34. end
  35. }
  36. end
  37. end
  38. def new
  39. end
  40. def create
  41. if request.post? && @enumeration.save
  42. flash[:notice] = l(:notice_successful_create)
  43. redirect_to enumerations_path
  44. else
  45. render :action => 'new'
  46. end
  47. end
  48. def edit
  49. end
  50. def update
  51. if request.put? && @enumeration.update_attributes(params[:enumeration])
  52. flash[:notice] = l(:notice_successful_update)
  53. redirect_to enumerations_path
  54. else
  55. render :action => 'edit'
  56. end
  57. end
  58. def destroy
  59. if !@enumeration.in_use?
  60. # No associated objects
  61. @enumeration.destroy
  62. redirect_to enumerations_path
  63. return
  64. elsif params[:reassign_to_id]
  65. if reassign_to = @enumeration.class.find_by_id(params[:reassign_to_id])
  66. @enumeration.destroy(reassign_to)
  67. redirect_to enumerations_path
  68. return
  69. end
  70. end
  71. @enumerations = @enumeration.class.all - [@enumeration]
  72. end
  73. private
  74. def build_new_enumeration
  75. class_name = params[:enumeration] && params[:enumeration][:type] || params[:type]
  76. @enumeration = Enumeration.new_subclass_instance(class_name, params[:enumeration])
  77. if @enumeration.nil?
  78. render_404
  79. end
  80. end
  81. def find_enumeration
  82. @enumeration = Enumeration.find(params[:id])
  83. rescue ActiveRecord::RecordNotFound
  84. render_404
  85. end
  86. end