diff --git a/app/models/todo.rb b/app/models/todo.rb index 4f34f9b8..9122afae 100644 --- a/app/models/todo.rb +++ b/app/models/todo.rb @@ -313,7 +313,9 @@ class Todo < ActiveRecord::Base if value.is_a? Context self.original_context=(value) else - self.original_context=(Context.create(value)) + c = Context.find_by_name(value[:name]) + c = Context.create(value) if c.nil? + self.original_context=(c) end end @@ -326,8 +328,10 @@ class Todo < ActiveRecord::Base def project=(value) if value.is_a? Project self.original_project=(value) - elsif !value.nil? - self.original_project=(Project.create(value)) + elsif !(value.nil? || value.is_a?(NullProject)) + p = Project.find_by_name(value[:name]) + p = Project.create(value) if p.nil? + self.original_project=(p) else self.original_project=value end diff --git a/test/integration/todo_xml_api_test.rb b/test/integration/todo_xml_api_test.rb index d1a41a16..bb25050d 100644 --- a/test/integration/todo_xml_api_test.rb +++ b/test/integration/todo_xml_api_test.rb @@ -139,6 +139,24 @@ class TodoXmlApiTest < ActionController::IntegrationTest assert_equal todo.context.name, "@SomeNewContext" end + def test_post_create_todo_with_name_of_existing_context + authenticated_post_xml_to_todo_create " + + this will succeed 4 + #{projects(:timemachine).id} + + #{contexts(:office).name} + +" + + assert_response :success + todo = @user.todos.find_by_description("this will succeed 4") + assert_not_nil todo + assert_not_nil todo.context + assert_equal contexts(:office).name, todo.context.name + end + + def test_post_create_todo_with_new_project authenticated_post_xml_to_todo_create " @@ -156,6 +174,24 @@ class TodoXmlApiTest < ActionController::IntegrationTest assert_equal todo.project.name, "Make even more money" end + def test_post_create_todo_with_name_of_existing_project + authenticated_post_xml_to_todo_create " + + this will succeed 5 + #{contexts(:office).id} + + #{projects(:timemachine).name} + +" + + assert_response :success + todo = @user.todos.find_by_description("this will succeed 5") + assert_not_nil todo + assert_not_nil todo.project + assert_equal projects(:timemachine).name, todo.project.name + assert 1, @user.projects.all(:conditions => ["projects.name = ?", projects(:timemachine).name]).count # no duplication of project + end + def test_post_create_todo_with_wrong_project_and_context_id authenticated_post_xml_to_todo_create "