From 807cb1e75940394ef38b302d5d4a7201590e5cf6 Mon Sep 17 00:00:00 2001 From: lukemelia Date: Sat, 18 Nov 2006 05:01:23 +0000 Subject: [PATCH] Working with acts_as_state_machine, it's tricky to create an object with a state other than the one you've defined as the initial state. This caused a bug where deferred actions were not being created with a :deferred state. Fixed this. Fixes #400. git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@349 a4c988fc-2ded-0310-b66e-134b36920a42 --- tracks/app/controllers/deferred_controller.rb | 3 +-- tracks/app/models/todo.rb | 8 +++++++ .../functional/deferred_controller_test.rb | 22 +++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 tracks/test/functional/deferred_controller_test.rb diff --git a/tracks/app/controllers/deferred_controller.rb b/tracks/app/controllers/deferred_controller.rb index 4f191ff8..35c8bfd2 100644 --- a/tracks/app/controllers/deferred_controller.rb +++ b/tracks/app/controllers/deferred_controller.rb @@ -20,8 +20,7 @@ class DeferredController < ApplicationController def create @source_view = 'deferred' - @item = Todo.new - @item.defer! + @item = Todo.new_deferred @item.attributes = params["todo"] if params["todo"]["show_from"] @item.show_from = parse_date_per_user_prefs(params["todo"]["show_from"]) diff --git a/tracks/app/models/todo.rb b/tracks/app/models/todo.rb index 82e51bae..66f54df3 100644 --- a/tracks/app/models/todo.rb +++ b/tracks/app/models/todo.rb @@ -63,6 +63,14 @@ class Todo < ActiveRecord::Base original_project.nil? ? Project.null_object : original_project end + def self.new_deferred + todo = self.new + def todo.set_initial_state + self.state = 'deferred' + end + todo + end + def self.not_done( id=id ) self.find(:all, :conditions =>[ "done = ? AND context_id = ?", false, id], :order =>"due IS NULL, due ASC, created_at ASC") end diff --git a/tracks/test/functional/deferred_controller_test.rb b/tracks/test/functional/deferred_controller_test.rb new file mode 100644 index 00000000..5e5a1253 --- /dev/null +++ b/tracks/test/functional/deferred_controller_test.rb @@ -0,0 +1,22 @@ +require File.dirname(__FILE__) + '/../test_helper' +require 'deferred_controller' + +# Re-raise errors caught by the controller. +class DeferredController; def rescue_action(e) raise e end; end + +class DeferredControllerTest < Test::Unit::TestCase + fixtures :users, :preferences, :projects, :contexts, :todos + + def setup + @controller = DeferredController.new + @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new + end + + def test_create + @request.session['user_id'] = users(:admin_user).id + xhr :post, :create, :todo => {:description => 'deferred controller test create', :notes => 'notes', :context_id => 1, :project_id => 1, :due => '', :show_from => '11/30/2030'} + t = Todo.find_by_description('deferred controller test create') + assert_equal :deferred, t.current_state.to_sym + end + +end