From: Jean-Philippe Lang Date: Fri, 10 Dec 2010 10:12:19 +0000 (+0000) Subject: Fixed: error when serializing back objects with custom fields using ActiveResource... X-Git-Tag: 1.1.0~116 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0e19aa4362a0524d900044fc9dc69e177337eca0;p=redmine.git Fixed: error when serializing back objects with custom fields using ActiveResource (#6403). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4480 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/helpers/custom_fields_helper.rb b/app/helpers/custom_fields_helper.rb index 189467409..107d0ac87 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 a63b07a09..f30ee55bb 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 001cabe11..02e7fdd29 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 e65477a23..4ffee934f 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 4479e500f..dc0263712 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 815abef2b..722b44ac1 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 6c3dd9018..c177d49e9 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 0ef9de8e2..c57b51eea 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)