diff app/controllers/enumerations_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/enumerations_controller.rb	Wed Jan 23 13:11:25 2013 +0000
+++ b/app/controllers/enumerations_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
@@ -18,53 +18,53 @@
 class EnumerationsController < ApplicationController
   layout 'admin'
 
-  before_filter :require_admin
+  before_filter :require_admin, :except => :index
+  before_filter :require_admin_or_api_request, :only => :index
+  before_filter :build_new_enumeration, :only => [:new, :create]
+  before_filter :find_enumeration, :only => [:edit, :update, :destroy]
+  accept_api_auth :index
 
   helper :custom_fields
-  include CustomFieldsHelper
 
   def index
-  end
-
-  verify :method => :post, :only => [ :destroy, :create, :update ],
-         :redirect_to => { :action => :index }
-
-  def new
-    begin
-      @enumeration = params[:type].constantize.new
-    rescue NameError
-      @enumeration = Enumeration.new
+    respond_to do |format|
+      format.html
+      format.api {
+        @klass = Enumeration.get_subclass(params[:type])
+        if @klass
+          @enumerations = @klass.shared.sorted.all
+        else
+          render_404
+        end
+      }
     end
   end
 
+  def new
+  end
+
   def create
-    @enumeration = Enumeration.new(params[:enumeration])
-    @enumeration.type = params[:enumeration][:type]
-    if @enumeration.save
+    if request.post? && @enumeration.save
       flash[:notice] = l(:notice_successful_create)
-      redirect_to :action => 'index', :type => @enumeration.type
+      redirect_to :action => 'index'
     else
       render :action => 'new'
     end
   end
 
   def edit
-    @enumeration = Enumeration.find(params[:id])
   end
 
   def update
-    @enumeration = Enumeration.find(params[:id])
-    @enumeration.type = params[:enumeration][:type] if params[:enumeration][:type]
-    if @enumeration.update_attributes(params[:enumeration])
+    if request.put? && @enumeration.update_attributes(params[:enumeration])
       flash[:notice] = l(:notice_successful_update)
-      redirect_to :action => 'index', :type => @enumeration.type
+      redirect_to :action => 'index'
     else
       render :action => 'edit'
     end
   end
 
   def destroy
-    @enumeration = Enumeration.find(params[:id])
     if !@enumeration.in_use?
       # No associated objects
       @enumeration.destroy
@@ -77,9 +77,22 @@
         return
       end
     end
-    @enumerations = @enumeration.class.find(:all) - [@enumeration]
-  #rescue
-  #  flash[:error] = 'Unable to delete enumeration'
-  #  redirect_to :action => 'index'
+    @enumerations = @enumeration.class.all - [@enumeration]
+  end
+
+  private
+
+  def build_new_enumeration
+    class_name = params[:enumeration] && params[:enumeration][:type] || params[:type]
+    @enumeration = Enumeration.new_subclass_instance(class_name, params[:enumeration])
+    if @enumeration.nil?
+      render_404
+    end
+  end
+
+  def find_enumeration
+    @enumeration = Enumeration.find(params[:id])
+  rescue ActiveRecord::RecordNotFound
+    render_404
   end
 end