mirror of
https://github.com/TracksApp/tracks.git
synced 2025-12-18 00:00:12 +01:00
Move Project#create_from_todo to its own class
The point of this is to keep as many things out of the ActiveRecord objects as possible and use them as just a thin database abstraction layer.
This commit is contained in:
parent
18883c6ecc
commit
883ea2b968
5 changed files with 50 additions and 24 deletions
|
|
@ -776,7 +776,7 @@ class TodosController < ApplicationController
|
||||||
|
|
||||||
def convert_to_project
|
def convert_to_project
|
||||||
todo = current_user.todos.find(params[:id])
|
todo = current_user.todos.find(params[:id])
|
||||||
@project = Project.create_from_todo(todo)
|
@project = ProjectFromTodo.new(todo).create
|
||||||
|
|
||||||
if @project.valid?
|
if @project.valid?
|
||||||
redirect_to project_url(@project)
|
redirect_to project_url(@project)
|
||||||
|
|
|
||||||
|
|
@ -47,21 +47,6 @@ class Project < ActiveRecord::Base
|
||||||
NullProject.new
|
NullProject.new
|
||||||
end
|
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
|
def hide_todos
|
||||||
todos.each do |t|
|
todos.each do |t|
|
||||||
unless t.completed? || t.deferred?
|
unless t.completed? || t.deferred?
|
||||||
|
|
|
||||||
27
lib/project_from_todo.rb
Normal file
27
lib/project_from_todo.rb
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
class ProjectFromTodo
|
||||||
|
attr_reader :todo
|
||||||
|
|
||||||
|
def initialize(todo)
|
||||||
|
@todo = todo
|
||||||
|
end
|
||||||
|
|
||||||
|
def create
|
||||||
|
project = build_project
|
||||||
|
|
||||||
|
if project.valid?
|
||||||
|
todo.destroy
|
||||||
|
project.save!
|
||||||
|
end
|
||||||
|
|
||||||
|
project
|
||||||
|
end
|
||||||
|
|
||||||
|
def build_project
|
||||||
|
project = Project.new.tap do |p|
|
||||||
|
p.name = todo.description
|
||||||
|
p.description = todo.notes
|
||||||
|
p.default_context = todo.context
|
||||||
|
p.user = todo.user
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
22
test/unit/project_from_todo_test.rb
Normal file
22
test/unit/project_from_todo_test.rb
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
require_relative '../test_helper'
|
||||||
|
require_relative '../../lib/project_from_todo'
|
||||||
|
|
||||||
|
class ProjectFromTodoTest < ActiveSupport::TestCase
|
||||||
|
fixtures :todos
|
||||||
|
|
||||||
|
def test_create_project_from_valid_todo
|
||||||
|
todo = todos(:upgrade_rails)
|
||||||
|
project = ProjectFromTodo.new(todo).create
|
||||||
|
assert_equal project.name, todo.description
|
||||||
|
assert_equal project.description, todo.notes
|
||||||
|
assert_equal project.default_context, todo.context
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_invalid_project_from_invalid_todo
|
||||||
|
todo = todos(:upgrade_rails)
|
||||||
|
todo.description = ""
|
||||||
|
project = ProjectFromTodo.new(todo).create
|
||||||
|
assert_not_nil project
|
||||||
|
assert_equal false, project.valid?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -167,14 +167,6 @@ class ProjectTest < ActiveSupport::TestCase
|
||||||
assert_equal 3, @moremoney.todos.not_completed.count
|
assert_equal 3, @moremoney.todos.not_completed.count
|
||||||
end
|
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
|
|
||||||
|
|
||||||
def test_new_record_before_save
|
def test_new_record_before_save
|
||||||
assert !@timemachine.new_record_before_save?, "existing records should not be new_record"
|
assert !@timemachine.new_record_before_save?, "existing records should not be new_record"
|
||||||
p = Project.where(:name => "I do not exist").first_or_create
|
p = Project.where(:name => "I do not exist").first_or_create
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue