##// END OF EJS Templates
Makes enumerations available through the REST API....
Jean-Philippe Lang -
r10453:d62b90db7327
parent child
Show More
@@ -0,0 +1,9
1 api.array @klass.name.underscore.pluralize do
2 @enumerations.each do |enumeration|
3 api.__send__ @klass.name.underscore do
4 api.id enumeration.id
5 api.name enumeration.name
6 api.is_default enumeration.is_default
7 end
8 end
9 end
@@ -0,0 +1,44
1 # Redmine - project management software
2 # Copyright (C) 2006-2012 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
18 require File.expand_path('../../../test_helper', __FILE__)
19
20 class ApiTest::EnumerationsTest < ActionController::IntegrationTest
21 fixtures :enumerations
22
23 def setup
24 Setting.rest_api_enabled = '1'
25 end
26
27 context "/enumerations/issue_priorities" do
28 context "GET" do
29
30 should "return priorities" do
31 get '/enumerations/issue_priorities.xml'
32
33 assert_response :success
34 assert_equal 'application/xml', response.content_type
35 assert_select 'issue_priorities[type=array]' do
36 assert_select 'issue_priority' do
37 assert_select 'id', :text => '6'
38 assert_select 'name', :text => 'High'
39 end
40 end
41 end
42 end
43 end
44 end
@@ -18,13 +18,26
18 class EnumerationsController < ApplicationController
18 class EnumerationsController < ApplicationController
19 layout 'admin'
19 layout 'admin'
20
20
21 before_filter :require_admin
21 before_filter :require_admin, :except => :index
22 before_filter :require_admin_or_api_request, :only => :index
22 before_filter :build_new_enumeration, :only => [:new, :create]
23 before_filter :build_new_enumeration, :only => [:new, :create]
23 before_filter :find_enumeration, :only => [:edit, :update, :destroy]
24 before_filter :find_enumeration, :only => [:edit, :update, :destroy]
25 accept_api_auth :index
24
26
25 helper :custom_fields
27 helper :custom_fields
26
28
27 def index
29 def index
30 respond_to do |format|
31 format.html
32 format.api {
33 @klass = Enumeration.get_subclass(params[:type])
34 if @klass
35 @enumerations = @klass.shared.sorted.all
36 else
37 render_404
38 end
39 }
40 end
28 end
41 end
29
42
30 def new
43 def new
@@ -33,7 +46,7 class EnumerationsController < ApplicationController
33 def create
46 def create
34 if request.post? && @enumeration.save
47 if request.post? && @enumeration.save
35 flash[:notice] = l(:notice_successful_create)
48 flash[:notice] = l(:notice_successful_create)
36 redirect_to :action => 'index', :type => @enumeration.type
49 redirect_to :action => 'index'
37 else
50 else
38 render :action => 'new'
51 render :action => 'new'
39 end
52 end
@@ -45,7 +58,7 class EnumerationsController < ApplicationController
45 def update
58 def update
46 if request.put? && @enumeration.update_attributes(params[:enumeration])
59 if request.put? && @enumeration.update_attributes(params[:enumeration])
47 flash[:notice] = l(:notice_successful_update)
60 flash[:notice] = l(:notice_successful_update)
48 redirect_to :action => 'index', :type => @enumeration.type
61 redirect_to :action => 'index'
49 else
62 else
50 render :action => 'edit'
63 render :action => 'edit'
51 end
64 end
@@ -36,6 +36,7 class Enumeration < ActiveRecord::Base
36 validates_length_of :name, :maximum => 30
36 validates_length_of :name, :maximum => 30
37
37
38 scope :shared, where(:project_id => nil)
38 scope :shared, where(:project_id => nil)
39 scope :sorted, order("#{table_name}.position ASC")
39 scope :active, where(:active => true)
40 scope :active, where(:active => true)
40 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
41 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
41
42
@@ -98,7 +98,7 RedmineApp::Application.routes.draw do
98 match 'copy', :via => [:get, :post]
98 match 'copy', :via => [:get, :post]
99 end
99 end
100
100
101 resources :memberships, :shallow => true, :controller => 'members', :only => [:index, :show, :create, :update, :destroy] do
101 resources :memberships, :shallow => true, :controller => 'members', :only => [:index, :show, :new, :create, :update, :destroy] do
102 collection do
102 collection do
103 get 'autocomplete'
103 get 'autocomplete'
104 end
104 end
@@ -295,6 +295,7 RedmineApp::Application.routes.draw do
295 end
295 end
296 end
296 end
297 resources :enumerations, :except => :show
297 resources :enumerations, :except => :show
298 match 'enumerations/:type', :to => 'enumerations#index', :via => :get
298
299
299 get 'projects/:id/search', :controller => 'search', :action => 'index'
300 get 'projects/:id/search', :controller => 'search', :action => 'index'
300 get 'search', :controller => 'search', :action => 'index'
301 get 'search', :controller => 'search', :action => 'index'
@@ -22,8 +22,7 module Redmine
22 end
22 end
23
23
24 module ClassMethods
24 module ClassMethods
25 # Returns an instance of the given subclass name
25 def get_subclass(class_name)
26 def new_subclass_instance(class_name, *args)
27 klass = nil
26 klass = nil
28 begin
27 begin
29 klass = class_name.to_s.classify.constantize
28 klass = class_name.to_s.classify.constantize
@@ -33,6 +32,12 module Redmine
33 unless subclasses.include? klass
32 unless subclasses.include? klass
34 klass = nil
33 klass = nil
35 end
34 end
35 klass
36 end
37
38 # Returns an instance of the given subclass name
39 def new_subclass_instance(class_name, *args)
40 klass = get_subclass(class_name)
36 if klass
41 if klass
37 klass.new(*args)
42 klass.new(*args)
38 end
43 end
@@ -30,6 +30,12 class EnumerationsControllerTest < ActionController::TestCase
30 assert_template 'index'
30 assert_template 'index'
31 end
31 end
32
32
33 def test_index_should_require_admin
34 @request.session[:user_id] = nil
35 get :index
36 assert_response 302
37 end
38
33 def test_new
39 def test_new
34 get :new, :type => 'IssuePriority'
40 get :new, :type => 'IssuePriority'
35 assert_response :success
41 assert_response :success
@@ -48,7 +54,7 class EnumerationsControllerTest < ActionController::TestCase
48 assert_difference 'IssuePriority.count' do
54 assert_difference 'IssuePriority.count' do
49 post :create, :enumeration => {:type => 'IssuePriority', :name => 'Lowest'}
55 post :create, :enumeration => {:type => 'IssuePriority', :name => 'Lowest'}
50 end
56 end
51 assert_redirected_to '/enumerations?type=IssuePriority'
57 assert_redirected_to '/enumerations'
52 e = IssuePriority.find_by_name('Lowest')
58 e = IssuePriority.find_by_name('Lowest')
53 assert_not_nil e
59 assert_not_nil e
54 end
60 end
@@ -77,7 +83,7 class EnumerationsControllerTest < ActionController::TestCase
77 assert_no_difference 'IssuePriority.count' do
83 assert_no_difference 'IssuePriority.count' do
78 put :update, :id => 6, :enumeration => {:type => 'IssuePriority', :name => 'New name'}
84 put :update, :id => 6, :enumeration => {:type => 'IssuePriority', :name => 'New name'}
79 end
85 end
80 assert_redirected_to '/enumerations?type=IssuePriority'
86 assert_redirected_to '/enumerations'
81 e = IssuePriority.find(6)
87 e = IssuePriority.find(6)
82 assert_equal 'New name', e.name
88 assert_equal 'New name', e.name
83 end
89 end
@@ -43,5 +43,9 class RoutingEnumerationsTest < ActionController::IntegrationTest
43 { :method => 'delete', :path => "/enumerations/2" },
43 { :method => 'delete', :path => "/enumerations/2" },
44 { :controller => 'enumerations', :action => 'destroy', :id => '2' }
44 { :controller => 'enumerations', :action => 'destroy', :id => '2' }
45 )
45 )
46 assert_routing(
47 { :method => 'get', :path => "/enumerations/issue_priorities.xml" },
48 { :controller => 'enumerations', :action => 'index', :type => 'issue_priorities', :format => 'xml' }
49 )
46 end
50 end
47 end
51 end
General Comments 0
You need to be logged in to leave comments. Login now