From 982f2f8a55fdce24bec8fad7591ca6027dcaa1d6 Mon Sep 17 00:00:00 2001 From: Aaron Pfeifer Date: Thu, 26 Jul 2007 21:49:42 +0000 Subject: [PATCH] Allow subclasses to override the initial state. --- CHANGELOG | 2 ++ lib/has_states.rb | 5 +++-- test/app_root/app/models/motorcycle.rb | 1 + test/unit/has_states_test.rb | 5 +++++ 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 93e0fc5c..84b98123 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Allow subclasses to override the initial state + * Fix problem with migrations using default null when column cannot be null * Moved deadline support into a separate plugin (has_state_deadlines). diff --git a/lib/has_states.rb b/lib/has_states.rb index 9999a3e2..9f24a46b 100644 --- a/lib/has_states.rb +++ b/lib/has_states.rb @@ -99,11 +99,12 @@ def has_states(options) # Save options for referencing later write_inheritable_attribute :active_states, {} write_inheritable_attribute :active_events, {} - write_inheritable_attribute :initial_state_name, options[:initial] + write_inheritable_attribute :initial_state, options[:initial] write_inheritable_attribute :record_state_changes, options[:record_changes] class_inheritable_reader :active_states class_inheritable_reader :active_events + class_inheritable_writer :initial_state class_inheritable_reader :record_state_changes before_create :set_initial_state_id @@ -376,7 +377,7 @@ def self.included(base) #:nodoc: # Gets the name of the initial state that records will be placed in. def initial_state_name - name = self.class.read_inheritable_attribute(:initial_state_name) + name = self.class.read_inheritable_attribute(:initial_state) name = name.call(self) if name.is_a?(Proc) name.to_sym diff --git a/test/app_root/app/models/motorcycle.rb b/test/app_root/app/models/motorcycle.rb index 321a4b9f..9c2dcc2a 100644 --- a/test/app_root/app/models/motorcycle.rb +++ b/test/app_root/app/models/motorcycle.rb @@ -1,2 +1,3 @@ class Motorcycle < Vehicle + self.initial_state = :idling end \ No newline at end of file diff --git a/test/unit/has_states_test.rb b/test/unit/has_states_test.rb index 579e5093..8def348d 100644 --- a/test/unit/has_states_test.rb +++ b/test/unit/has_states_test.rb @@ -21,6 +21,11 @@ def test_should_raise_exception_if_no_initial_state_given assert_raise(PluginAWeek::Has::States::NoInitialState) {Message.has_states({})} end + def test_should_allow_subclasses_to_override_initial_state + assert_equal :idling, Motorcycle.read_inheritable_attribute(:initial_state) + assert_not_equal Motorcycle.read_inheritable_attribute(:initial_state), Vehicle.read_inheritable_attribute(:initial_state) + end + def test_active_states_should_be_initially_empty expected = {} assert_equal expected, Message.active_states