From b01c7878d18f6570854b4ce5e3f2e4651fa06235 Mon Sep 17 00:00:00 2001 From: Matt Rogers Date: Tue, 14 Aug 2012 19:31:34 -0500 Subject: [PATCH] Push conversion of a todo to a project down. Refactor the conversion of a todo to a project and move it from the TodosController to the Project model. --- app/controllers/todos_controller.rb | 11 ++++------- app/models/project.rb | 15 +++++++++++++++ test/unit/project_test.rb | 8 ++++++++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/app/controllers/todos_controller.rb b/app/controllers/todos_controller.rb index 3f3c7f65..c88c7e05 100644 --- a/app/controllers/todos_controller.rb +++ b/app/controllers/todos_controller.rb @@ -900,13 +900,10 @@ class TodosController < ApplicationController end def convert_to_project - @todo = current_user.todos.find_by_id(params[:id]) - @project = current_user.projects.new(:name => @todo.description, :description => @todo.notes, - :default_context => @todo.context) - - unless @project.invalid? - @todo.destroy - @project.save! + todo = current_user.todos.find_by_id(params[:id]) + @project = Project.create_from_todo(todo) + + if @project.valid? redirect_to project_url(@project) else flash[:error] = "Could not create project from todo: #{@project.errors.full_messages[0]}" diff --git a/app/models/project.rb b/app/models/project.rb index 32681008..e5e6205e 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -47,6 +47,21 @@ class Project < ActiveRecord::Base NullProject.new end + def self.create_from_todo(todo) + project = Project.new(:name => todo.description, + :description => todo.notes, + :default_context => todo.context) + + project.user = todo.user + + if project.valid? + todo.destroy + project.save! + end + + project + end + def hide_todos todos.each do |t| unless t.completed? || t.deferred? diff --git a/test/unit/project_test.rb b/test/unit/project_test.rb index 53e178ef..4de18967 100644 --- a/test/unit/project_test.rb +++ b/test/unit/project_test.rb @@ -166,5 +166,13 @@ class ProjectTest < ActiveSupport::TestCase @moremoney.todos[0].complete! assert_equal 3, @moremoney.todos.not_completed.count end + + def test_convert_from_todo + todo = todos(:upgrade_rails) + project = Project.create_from_todo(todo) + assert_equal project.name, todo.description + assert_equal project.description, todo.notes + assert_equal project.default_context, todo.context + end end