diff --git a/lib/acts_as_wizard.rb b/lib/acts_as_wizard.rb index 24d3153..96aa4a7 100644 --- a/lib/acts_as_wizard.rb +++ b/lib/acts_as_wizard.rb @@ -36,7 +36,7 @@ def acts_as_wizard(*opts) opts.each do |opt| has_one opt, :dependent => :destroy - state opt, :after => :current_page + state opt end event :next do @@ -52,11 +52,6 @@ def acts_as_wizard(*opts) end module InstanceMethods - # return the model for the current wizard page - def current_page - @current_page ||= find_page(self.get_current_wizard_step) - end - # returns a symbol for the current wizard page def get_current_wizard_step current_state @@ -95,11 +90,6 @@ def get_wizard_page def switch_wizard_page(direction) send(direction) end - - private - def find_page(state) - page_class.send("find_by_#{self.class.to_s.underscore}_id",self.id) - end end module ClassMethods diff --git a/test/acts_as_wizard_test.rb b/test/acts_as_wizard_test.rb index cc73d8f..5e14fbe 100644 --- a/test/acts_as_wizard_test.rb +++ b/test/acts_as_wizard_test.rb @@ -23,13 +23,17 @@ class MainModel < ActiveRecord::Base - acts_as_wizard :first_page + acts_as_wizard :first_page, :second_page end class FirstPage < ActiveRecord::Base acts_as_wizard_page :main_model end +class SecondPage < ActiveRecord::Base + acts_as_wizard_page :main_model +end + class EmptyModel < ActiveRecord::Base end @@ -53,13 +57,6 @@ def test_no_errors EmptyModel.acts_as_wizard(:foo) end - def test_current_page_raises_error_if_model_does_not_exist - EmptyModel.acts_as_wizard(:foo) - @bunk_model = EmptyModel.new - e = assert_raise(NoMethodError) { @bunk_model.current_page } - assert_equal("undefined method `to_sym' for nil:NilClass", e.message) - end - def test_get_wizard_page_returns_a_new_first_page_when_there_is_no_first_page @page = @main_model.get_wizard_page assert_equal(FirstPage, @page.class) @@ -73,4 +70,30 @@ def test_get_wizard_page_returns_the_first_page_when_there_is_a_first_page assert_equal(@expected_page, @page) end + def test_get_current_wizard_step_returns_first_page_on_new_model + assert_equal(:first_page, @main_model.get_current_wizard_step) + end + + def test_get_current_wizard_step_returns_second_page_after_next_called + @main_model.next! + assert_equal(:second_page, @main_model.get_current_wizard_step) + end + + def test_get_current_wizard_step_returns_first_page_after_next_called_and_then_previous_called + @main_model.next! + @main_model.previous! + assert_equal(:first_page, @main_model.get_current_wizard_step) + end + + def test_get_current_wizard_step_returns_first_page_if_previous_called_on_first_page + @main_model.previous! + assert_equal(:first_page, @main_model.get_current_wizard_step) + end + + def test_get_current_wizard_step_returns_second_page_after_next_called_and_next_called_again + @main_model.next! + @main_model.next! + assert_equal(:second_page, @main_model.get_current_wizard_step) + end + end diff --git a/test/schema.rb b/test/schema.rb index 243cac6..cda6ab4 100644 --- a/test/schema.rb +++ b/test/schema.rb @@ -9,5 +9,9 @@ create_table :first_pages, :force => true do |t| t.integer :main_model_id + end + + create_table :second_pages, :force => true do |t| + t.integer :main_model_id end end \ No newline at end of file