diff --git a/app/controllers/custom_fields_controller.rb b/app/controllers/custom_fields_controller.rb
index 2b848b3df..5121ebc4a 100644
--- a/app/controllers/custom_fields_controller.rb
+++ b/app/controllers/custom_fields_controller.rb
@@ -19,6 +19,8 @@ class CustomFieldsController < ApplicationController
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 @@ class CustomFieldsController < ApplicationController
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
diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb
index 0ad8b4ce0..93e662785 100644
--- a/app/models/custom_field.rb
+++ b/app/models/custom_field.rb
@@ -155,6 +155,22 @@ class CustomField < ActiveRecord::Base
find(:all, :conditions => ["is_for_all=?", true], :order => 'position')
end
+ # Returns an instance of the given subclass name
+ def self.new_subclass_instance(class_name, *args)
+ klass = nil
+ begin
+ klass = class_name.to_s.classify.constantize
+ rescue
+ # invalid class name
+ end
+ unless subclasses.include? klass
+ klass = nil
+ end
+ if klass
+ klass.new(*args)
+ end
+ end
+
def type_name
nil
end
diff --git a/app/views/custom_fields/_form.html.erb b/app/views/custom_fields/_form.html.erb
index 7432a426f..a5abb2031 100644
--- a/app/views/custom_fields/_form.html.erb
+++ b/app/views/custom_fields/_form.html.erb
@@ -81,7 +81,7 @@ function toggle_custom_field_format() {
when "IssueCustomField" %>