diff --git a/app/helpers/custom_fields_helper.rb b/app/helpers/custom_fields_helper.rb index 18946740..107d0ac8 100644 --- a/app/helpers/custom_fields_helper.rb +++ b/app/helpers/custom_fields_helper.rb @@ -104,4 +104,15 @@ module CustomFieldsHelper def custom_field_formats_for_select Redmine::CustomFieldFormat.as_select end + + # Renders the custom_values in api views + def render_api_custom_values(custom_values, api) + api.array :custom_fields do + custom_values.each do |custom_value| + api.custom_field :id => custom_value.custom_field_id, :name => custom_value.custom_field.name do + api.value custom_value.value + end + end + end unless custom_values.empty? + end end diff --git a/app/views/issues/index.api.rsb b/app/views/issues/index.api.rsb index a63b07a0..f30ee55b 100644 --- a/app/views/issues/index.api.rsb +++ b/app/views/issues/index.api.rsb @@ -19,11 +19,7 @@ api.array :issues do api.done_ratio issue.done_ratio api.estimated_hours issue.estimated_hours - api.array :custom_fields do - issue.custom_field_values.each do |custom_value| - api.custom_field custom_value.value, :id => custom_value.custom_field_id, :name => custom_value.custom_field.name - end - end + render_api_custom_values issue.custom_field_values, api api.created_on issue.created_on api.updated_on issue.updated_on diff --git a/app/views/issues/show.api.rsb b/app/views/issues/show.api.rsb index 001cabe1..02e7fdd2 100644 --- a/app/views/issues/show.api.rsb +++ b/app/views/issues/show.api.rsb @@ -20,11 +20,7 @@ api.issue do api.spent_hours @issue.spent_hours end - api.array :custom_fields do - @issue.custom_field_values.each do |custom_value| - api.custom_field custom_value.value, :id => custom_value.custom_field_id, :name => custom_value.custom_field.name - end - end unless @issue.custom_field_values.empty? + render_api_custom_values @issue.custom_field_values, api api.created_on @issue.created_on api.updated_on @issue.updated_on diff --git a/app/views/projects/index.api.rsb b/app/views/projects/index.api.rsb index e65477a2..4ffee934 100644 --- a/app/views/projects/index.api.rsb +++ b/app/views/projects/index.api.rsb @@ -6,11 +6,9 @@ api.array :projects do api.identifier project.identifier api.description project.description api.parent(:id => project.parent_id, :name => project.parent.name) unless project.parent.nil? - api.array :custom_fields do - project.visible_custom_field_values.each do |custom_value| - api.custom_field custom_value.value, :id => custom_value.custom_field_id, :name => custom_value.custom_field.name - end - end unless project.custom_field_values.empty? + + render_api_custom_values project.visible_custom_field_values, api + api.created_on project.created_on api.updated_on project.updated_on end diff --git a/app/views/projects/show.api.rsb b/app/views/projects/show.api.rsb index 4479e500..dc026371 100644 --- a/app/views/projects/show.api.rsb +++ b/app/views/projects/show.api.rsb @@ -5,11 +5,7 @@ api.project do api.description @project.description api.homepage @project.homepage - api.array :custom_fields do - @project.visible_custom_field_values.each do |custom_value| - api.custom_field custom_value.value, :id => custom_value.custom_field_id, :name => custom_value.custom_field.name - end - end unless @project.custom_field_values.empty? + render_api_custom_values @project.visible_custom_field_values, api api.created_on @project.created_on api.updated_on @project.updated_on diff --git a/app/views/users/index.api.rsb b/app/views/users/index.api.rsb index 815abef2..722b44ac 100644 --- a/app/views/users/index.api.rsb +++ b/app/views/users/index.api.rsb @@ -9,11 +9,7 @@ api.array :users do api.created_on user.created_on api.last_login_on user.last_login_on - api.array :custom_fields do - user.visible_custom_field_values.each do |custom_value| - api.custom_field custom_value.value, :id => custom_value.custom_field_id, :name => custom_value.custom_field.name - end - end unless user.visible_custom_field_values.empty? + render_api_custom_values user.visible_custom_field_values, api end end end diff --git a/app/views/users/show.api.rsb b/app/views/users/show.api.rsb index 6c3dd901..c177d49e 100644 --- a/app/views/users/show.api.rsb +++ b/app/views/users/show.api.rsb @@ -7,11 +7,7 @@ api.user do api.created_on @user.created_on api.last_login_on @user.last_login_on - api.array :custom_fields do - @user.visible_custom_field_values.each do |custom_value| - api.custom_field custom_value.value, :id => custom_value.custom_field_id, :name => custom_value.custom_field.name - end - end unless @user.visible_custom_field_values.empty? + render_api_custom_values @user.visible_custom_field_values, api api.array :memberships do @memberships.each do |membership| diff --git a/test/integration/api_test/issues_test.rb b/test/integration/api_test/issues_test.rb index 0ef9de8e..c57b51ee 100644 --- a/test/integration/api_test/issues_test.rb +++ b/test/integration/api_test/issues_test.rb @@ -91,6 +91,32 @@ class ApiTest::IssuesTest < ActionController::IntegrationTest end context "GET /issues/:id" do + context "with custom fields" do + context ".xml" do + should "display custom fields" do + get '/issues/3.xml' + + assert_tag :tag => 'issue', + :child => { + :tag => 'custom_fields', + :attributes => { :type => 'array' }, + :child => { + :tag => 'custom_field', + :attributes => { :id => '1'}, + :child => { + :tag => 'value', + :content => 'MySQL' + } + } + } + + assert_nothing_raised do + Hash.from_xml(response.body).to_xml + end + end + end + end + context "with subtasks" do setup do @c1 = Issue.generate!(:status_id => 1, :subject => "child c1", :tracker_id => 1, :project_id => 1, :parent_issue_id => 1)