diff app/controllers/custom_fields_controller.rb @ 1338:25603efa57b5

Merge from live branch
author Chris Cannam
date Thu, 20 Jun 2013 13:14:14 +0100
parents 433d4f72a19b
children 622f24f53b42 261b3d9a4903
line wrap: on
line diff
--- a/app/controllers/custom_fields_controller.rb	Wed Jan 23 13:11:25 2013 +0000
+++ b/app/controllers/custom_fields_controller.rb	Thu Jun 20 13:14:14 2013 +0100
@@ -1,5 +1,5 @@
 # Redmine - project management software
-# Copyright (C) 2006-2011  Jean-Philippe Lang
+# Copyright (C) 2006-2012  Jean-Philippe Lang
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -19,6 +19,8 @@
   layout 'admin'
 
   before_filter :require_admin
+  before_filter :build_new_custom_field, :only => [:new, :create]
+  before_filter :find_custom_field, :only => [:edit, :update, :destroy]
 
   def index
     @custom_fields_by_type = CustomField.find(:all).group_by {|f| f.class.name }
@@ -26,39 +28,51 @@
   end
 
   def new
-    @custom_field = begin
-      if params[:type].to_s.match(/.+CustomField$/)
-        params[:type].to_s.constantize.new(params[:custom_field])
-      end
-    rescue
-    end
-    (redirect_to(:action => 'index'); return) unless @custom_field.is_a?(CustomField)
+  end
 
+  def create
     if request.post? and @custom_field.save
       flash[:notice] = l(:notice_successful_create)
       call_hook(:controller_custom_fields_new_after_save, :params => params, :custom_field => @custom_field)
       redirect_to :action => 'index', :tab => @custom_field.class.name
     else
-      @trackers = Tracker.find(:all, :order => 'position')
+      render :action => 'new'
     end
   end
 
   def edit
-    @custom_field = CustomField.find(params[:id])
-    if request.post? and @custom_field.update_attributes(params[:custom_field])
+  end
+
+  def update
+    if request.put? and @custom_field.update_attributes(params[:custom_field])
       flash[:notice] = l(:notice_successful_update)
       call_hook(:controller_custom_fields_edit_after_save, :params => params, :custom_field => @custom_field)
       redirect_to :action => 'index', :tab => @custom_field.class.name
     else
-      @trackers = Tracker.find(:all, :order => 'position')
+      render :action => 'edit'
     end
   end
 
   def destroy
-    @custom_field = CustomField.find(params[:id]).destroy
+    @custom_field.destroy
     redirect_to :action => 'index', :tab => @custom_field.class.name
   rescue
     flash[:error] = l(:error_can_not_delete_custom_field)
     redirect_to :action => 'index'
   end
+
+  private
+
+  def build_new_custom_field
+    @custom_field = CustomField.new_subclass_instance(params[:type], params[:custom_field])
+    if @custom_field.nil?
+      render_404
+    end
+  end
+
+  def find_custom_field
+    @custom_field = CustomField.find(params[:id])
+  rescue ActiveRecord::RecordNotFound
+    render_404
+  end
 end