]> source.dussan.org Git - redmine.git/commitdiff
REST API: custom fields definition (#11159).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 29 Sep 2013 10:08:30 +0000 (10:08 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 29 Sep 2013 10:08:30 +0000 (10:08 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@12165 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/custom_fields_controller.rb
app/models/custom_field.rb
app/views/custom_fields/index.api.rsb [new file with mode: 0644]
test/integration/api_test/custom_fields_test.rb [new file with mode: 0644]
test/integration/routing/custom_fields_test.rb

index c20516a1d8f5915520bdb59b7b945118aa4f5883..95d0d507f2fdf6a7ade3040fe64cde607eb25426 100644 (file)
@@ -21,10 +21,18 @@ class CustomFieldsController < ApplicationController
   before_filter :require_admin
   before_filter :build_new_custom_field, :only => [:new, :create]
   before_filter :find_custom_field, :only => [:edit, :update, :destroy]
+  accept_api_auth :index
 
   def index
-    @custom_fields_by_type = CustomField.all.group_by {|f| f.class.name }
-    @tab = params[:tab] || 'IssueCustomField'
+    respond_to do |format|
+      format.html {
+        @custom_fields_by_type = CustomField.all.group_by {|f| f.class.name }
+        @tab = params[:tab] || 'IssueCustomField'
+      }
+      format.api {
+        @custom_fields = CustomField.all
+      }
+    end
   end
 
   def new
index 937f9f4d0b116c1a57ab48218843a9929cce8788..f482973cf536c94f0d427dfcb0b49e73bcc606e8 100644 (file)
@@ -123,8 +123,10 @@ class CustomField < ActiveRecord::Base
         values.each do |value|
           value.force_encoding('UTF-8') if value.respond_to?(:force_encoding)
         end
+        values
+      else
+        []
       end
-      values || []
     end
   end
 
@@ -301,7 +303,7 @@ class CustomField < ActiveRecord::Base
 
   def self.customized_class
     self.name =~ /^(.+)CustomField$/
-    begin; $1.constantize; rescue nil; end
+    $1.constantize rescue nil
   end
 
   # to move in project_custom_field
diff --git a/app/views/custom_fields/index.api.rsb b/app/views/custom_fields/index.api.rsb
new file mode 100644 (file)
index 0000000..e640f33
--- /dev/null
@@ -0,0 +1,42 @@
+api.array :custom_fields do
+  @custom_fields.each do |field|
+    api.custom_field do
+      api.id                field.id
+      api.name              field.name
+      api.customized_type   field.class.customized_class.name.underscore if field.class.customized_class
+      api.field_format      field.field_format
+      api.regexp            field.regexp
+      api.min_length        (field.min_length == 0 ? nil : field.min_length)
+      api.max_length        (field.max_length == 0 ? nil : field.max_length)
+      api.is_required       field.is_required?
+      api.is_filter         field.is_filter?
+      api.searchable        field.searchable
+      api.multiple          field.multiple?
+      api.default_value     field.default_value
+      api.visible           field.visible?
+
+      if field.field_format == 'list'
+        api.array :possible_values do
+          field.possible_values.each do |v|
+            api.possible_value do
+              api.value v
+            end
+          end
+        end
+      end
+
+      if field.is_a?(IssueCustomField)
+        api.trackers do
+          field.trackers.each do |tracker|
+            api.tracker :id => tracker.id, :name => tracker.name
+          end
+        end
+        api.roles do
+          field.roles.each do |role|
+            api.role :id => role.id, :name => role.name
+          end
+        end
+      end
+    end
+  end
+end
diff --git a/test/integration/api_test/custom_fields_test.rb b/test/integration/api_test/custom_fields_test.rb
new file mode 100644 (file)
index 0000000..9613bc6
--- /dev/null
@@ -0,0 +1,43 @@
+# Redmine - project management software
+# Copyright (C) 2006-2013  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
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+require File.expand_path('../../../test_helper', __FILE__)
+
+class Redmine::ApiTest::CustomFieldsTest < Redmine::ApiTest::Base
+  fixtures :users, :custom_fields
+
+  def setup
+    Setting.rest_api_enabled = '1'
+  end
+
+  test "GET /custom_fields.xml should return custom fields" do
+    get '/custom_fields.xml', {}, credentials('admin')
+    assert_response :success
+    assert_equal 'application/xml', response.content_type
+
+    assert_select 'custom_fields' do
+      assert_select 'custom_field' do
+        assert_select 'name', :text => 'Database'
+        assert_select 'id', :text => '2'
+        assert_select 'customized_type', :text => 'issue'
+        assert_select 'possible_values[type=array]' do
+          assert_select 'possible_value>value', :text => 'PostgreSQL'
+        end
+      end
+    end
+  end
+end
index 748c31fece9818fc76b235615a2902908fa8e689..4b1a707de098acfcf4cee0e0cc19a44cedc2ddca 100644 (file)
@@ -44,4 +44,11 @@ class RoutingCustomFieldsTest < ActionController::IntegrationTest
         { :controller => 'custom_fields', :action => 'destroy', :id => '2' }
       )
   end
+
+  def test_custom_fields_api
+    assert_routing(
+        { :method => 'get', :path => "/custom_fields.xml" },
+        { :controller => 'custom_fields', :action => 'index', :format => 'xml' }
+      )
+  end
 end